首页 文章

设备未收到Firebase Cloud 消息传递通知

提问于
浏览
45

我遇到FireBase Cloud Messaging的问题,我从设备获取令牌并通过Google Firebase通知控制台发送通知测试但是,通知永远不会记录,也不会推送到Android虚拟设备 . FCM的文档几乎就是我在下面的代码,除了使用firebase进行推送通知之外你还需要做什么 . 我已经完成了文档中指定的所有设置信息(build.gradle添加,安装google play服务等等),但仍然没有生成消息 . 我按下通知后立即收到一次性错误,指出“I / FA:找不到标签管理器,因此不会被使用”,但这是唯一输出的数据,我没有找到与标签相关的任何内容经理要求和谷歌上的FCM . 我没有收到对logcat或设备的推送通知的代码有什么问题?请让我知道任何有用的进一步信息 . 谢谢 .

NotificationGenie.java

public class NotificationGenie extends FirebaseMessagingService {

private static final String TAG = "Firebase_MSG";

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    // TODO(developer): Handle FCM messages here.
    // If the application is in the foreground handle both data and notification messages here.
    // Also if you intend on generating your own notifications as a result of a received FCM
    // message, here is where that should be initiated. See sendNotification method below.
    sendNotification(remoteMessage.getNotification().getBody());
    Log.d(TAG, "From: " + remoteMessage.getFrom());
    Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
}

private void sendNotification(String messageBody) {
    Intent intent = new Intent(this, PostLoginActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.tf2spyprofile)
            .setContentTitle("FCM Message")
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.test.movile_android">

<!-- To auto-complete the email text field in the login form with the user's emails -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".LoginActivity"
        android:label="@string/title_activity_login">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".DashBoardActivity"
        android:label="@string/title_activity_dash_board"
        android:theme="@style/AppTheme.NoActionBar">
    </activity>

    <activity
        android:name=".NewOrderActivity"
        android:label="@string/title_activity_dash_board"
        android:theme="@style/AppTheme.NoActionBar">
    </activity>

    <activity
        android:name=".PostLoginActivity"
        android:label="@string/title_activity_dash_board"
        android:theme="@style/AppTheme.NoActionBar">
    </activity>
</application>

<service
    android:name=".NotificationGenie">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT"/>
    </intent-filter>
</service>

15 回答

  • 1

    您已将服务放在应用程序标记之外 . 改变底部 .

    <service
        android:name=".NotificationGenie">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>
    </service>
    
    </application>
    
  • 0

    我有同样的问题,它通过在我的服务中定义enabled,导出为true来解决

    <service
                android:name=".MyFirebaseMessagingService"
                android:enabled="true"
                android:exported="true">
                <intent-filter>
                    <action android:name="com.google.firebase.MESSAGING_EVENT"/>
                </intent-filter>
            </service>
    
  • 0

    "You must also keep in mind that to receive Messages sent from Firebase Console, App must be in background, not started neither hidden." ---根据@Laurent Russier的评论 .

    我从来没有从Firebase收到任何消息,直到我将我的应用程序放在后台 .

    这仅适用于模拟器的USB连接,您也可以在前台获得通知

  • 34

    在Overriden方法中调用 super.OnMessageReceived() . 这对我有用!最后!

  • 9

    我遇到了设备未收到的Firebase Cloud 消息传递问题 .

    在我的案例中,Firebase控制台项目中定义的包名称与我在Android项目的Manifest&Gradle上定义的包名称不同 .

    结果我正确地收到了令牌,但根本没有收到任何消息 .

    要进行sumarize,Firebase控制台包名称和Manifest&Gradle必须匹配 .

    您还必须记住,要接收从Firebase控制台发送的消息,应用程序必须位于后台,不能同时启动 .

  • 49

    我有一个类似的问题,但在我的情况下,我错过了我的Gradle构建中的google-services plugin(我正在将Firebase添加到现有项目中) .

    我将以下内容添加到我的根 build.gradle

    classpath 'com.google.gms:google-services:3.1.0'
    

    以及我的 app -level build.gradle 的结尾:

    apply plugin: 'com.google.gms.google-services'
    

    然后,我必须从Firebase控制台(最初导入现有的Google Cloud项目)下载 google-services.json 文件并将其复制到我的 app 目录 .

  • 4

    我遇到了同样的问题,但我的旧版GCM的清单中仍然有一些碎片 . 当我从我的清单中取出以下权限时,它修复了错误 . com.google.android.c2dm.permission.RECEIVE

  • 5

    如果您刚刚将FCM添加到现有应用程序,则不会调用 onTokenRefresh() . 我通过卸载应用程序并再次安装它来运行它 .

  • 0

    您需要遵循以下清单代码(必须在清单中有服务)

    <?xml version="1.0" encoding="utf-8"?>
        <manifest xmlns:android="http://schemas.android.com/apk/res/android"
            package="soapusingretrofitvolley.firebase">
            <uses-permission android:name="android.permission.INTERNET"/>
            <application
                android:allowBackup="true"
                android:icon="@mipmap/ic_launcher"
                android:label="@string/app_name"
                android:supportsRtl="true"
                android:theme="@style/AppTheme">
                <activity android:name=".MainActivity">
                    <intent-filter>
                        <action android:name="android.intent.action.MAIN" />
    
                        <category android:name="android.intent.category.LAUNCHER" />
                    </intent-filter>
                </activity>
    
                <service
                    android:name=".MyFirebaseMessagingService">
                    <intent-filter>
                        <action android:name="com.google.firebase.MESSAGING_EVENT"/>
                    </intent-filter>
                </service>
    
                <service
                    android:name=".MyFirebaseInstanceIDService">
                    <intent-filter>
                        <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
                    </intent-filter>
                </service>
            </application>
    
        </manifest>
    
  • 7
    <activity android:name=".activity.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!-- Firebase Notifications -->
        <service android:name=".service.MyFirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>
    
        <service android:name=".service.MyFirebaseInstanceIDService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
            </intent-filter>
        </service>
    
  • 2
    private void sendNotification(String message) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        int requestCode = 0;
        PendingIntent pendingIntent = PendingIntent.getActivity(this, requestCode, intent, PendingIntent.FLAG_ONE_SHOT);
        Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.URI_COLUMN_INDEX);
        NotificationCompat.Builder noBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentText(message)
                .setColor(getResources().getColor(R.color.colorPrimaryDark))
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("FCM Message")
                .setContentText("hello").setLargeIcon(((BitmapDrawable) getResources().getDrawable(R.drawable.dog)).getBitmap())
                .setStyle(new NotificationCompat.BigPictureStyle()
                        .bigPicture(((BitmapDrawable) getResources().getDrawable(R.drawable.dog)).getBitmap()))
                .setAutoCancel(true)
                .setSound(sound)
                .setContentIntent(pendingIntent);
    
        NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, noBuilder.build());
    
  • 7

    复制设备令牌时,可能会复制空间,请仔细检查是否复制了正确的令牌

  • 0

    我一直在处理整个帖子和其他人以及教程视频,但无法解决我没有收到消息的问题,但注册令牌却起作用 .

    在那之前,我只是在模拟器上测试应用程序 . 在物理手机上试用后,它立即工作,无需事先对项目进行任何更改 .

  • 22

    我有这个问题,我检查了我的代码 . 我试图修复这里提到的所有内容 . 最后问题是如此愚蠢 . 我的设备的 Sync 已关闭 . 这是我没有按预期收到通知的唯一原因 .

    enter image description here

  • -2

    也许是从连接我改变了从以太网到无线的连接,它没有做任何其他事情

相关问题