设备未收到 Firebase 云消息通知

新手上路,请多包涵

我在使用 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>

原文由 xec86 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 867
2 个回答

您已将服务放置在应用程序标签之外。将底部更改为此。

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

</application>

原文由 Tristan Richard 发布,翻译遵循 CC BY-SA 4.0 许可协议

我必须纠正2个错误,之后问题就解决了。

参考官方文档给出了这个很好的片段,它有助于确认订阅状态。

  Firebase.messaging.subscribeToTopic(TOPIC)
                    .addOnCompleteListener { task ->
                        var topicSubscriptionStatus = "Subscribed"
                        if (!task.isSuccessful) {
                            topicSubscriptionStatus = "Subscribe failed"
                        }
                        Log.d(TAG, topicSubscriptionStatus)
                        Toast.makeText(baseContext, topicSubscriptionStatus, Toast.LENGTH_SHORT).show()
                    }

  1. 我的主题名称有一个错字“/topic/”,而不是正确的“/topics/”。纠正此问题后,服务立即开始接收消息

  2. 我必须在 PendingIntent 上添加标志“FLAG_IMMUTABLE”(logcat 中的错误提到这是 S 需要的)

(我在提拉米苏)

原文由 Basu R 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题