android - 当应用程序在 Firebase 后台运行时如何处理通知

标签 android firebase firebase-cloud-messaging

这是我的 list :

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

<service android:name=".fcm.PshycoFirebaseInstanceIDService">
    <intent-filter>
        <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
    </intent-filter>
</service>

当应用程序在后台运行并且有通知到达时,默认通知就会出现,并且不会运行我的 onMessageReceived 代码。

这是我的onMessageReceived代码。如果我的应用程序在前台运行,而不是在后台运行,则会调用此函数。当应用程序也在后台运行时,如何运行此代码?

// [START receive_message]
@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.
    data = remoteMessage.getData();
    String title = remoteMessage.getNotification().getTitle();
    String message = remoteMessage.getNotification().getBody();
    String imageUrl = (String) data.get("image");
    String action = (String) data.get("action");
    Log.i(TAG, "onMessageReceived: title : "+title);
    Log.i(TAG, "onMessageReceived: message : "+message);
    Log.i(TAG, "onMessageReceived: imageUrl : "+imageUrl);
    Log.i(TAG, "onMessageReceived: action : "+action);

    if (imageUrl == null) {
        sendNotification(title,message,action);
    } else {
        new BigPictureNotification(this,title,message,imageUrl,action);
    }
}
// [END receive_message]

最佳答案

1。为什么会发生这种情况?

FCM(Firebase 云消息传递)中有两种类型的消息:

  1. 显示消息:这些消息会触发 onMessageReceived()仅当您的应用处于前台时回调
  2. 数据消息:这些消息会触发 onMessageReceived()如果您的应用处于前台/后台/已终止状态,则甚至回调

NOTE: Firebase team have not developed a UI to send data-messages to your devices, yet. You should use your server for sending this type!



2。怎么做?

要实现此目的,您必须执行 POST请求以下 URL:

POST https://fcm.googleapis.com/fcm/send

标题

  • key : Content-Type值: application/json
  • key : Authorization值: key=<your-server-key>

使用主题的正文

{
    "to": "/topics/my_topic",
    "data": {
        "my_custom_key": "my_custom_value",
        "my_custom_key2": true
     }
}

或者如果您想将其发送到特定设备

{
    "data": {
        "my_custom_key": "my_custom_value",
        "my_custom_key2": true
     },
    "registration_ids": ["{device-token}","{device2-token}","{device3-token}"]
}


NOTE: Be sure you're not adding JSON key notification
NOTE: To get your server key, you can find it in the firebase console: Your project -> settings -> Project settings -> Cloud messaging -> Server Key

3。如何处理推送通知消息?

这是处理收到消息的方式:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) { 
     Map<String, String> data = remoteMessage.getData();
     String myCustomKey = data.get("my_custom_key");

     // Manage data
}

关于android - 当应用程序在 Firebase 后台运行时如何处理通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58190295/

相关文章:

android - 应用程序打开时未收到 Firebase Messaging Android 通知

android - TextView 椭圆高度明智

android - Android 是否支持 .dat 扩展文件?

android - 如果使用 webview 完成身份验证,如何使用 Intent 过滤器处理 OAuth URL 回调?

javascript - 如何将返回的 Firebase 元素数组绑定(bind)到我的 AngularJS View (通过 Controller )?

python - 按 map 中的值进行 firebase 查询

ios - 使用google帐户登录后如何执行segue?

java - 无法从服务器向 FCM url 发送请求(相同的代码在本地电脑上工作)

android - 如何通过 Android 手机访问我的本地主机?

android - 是否可以手动注册到 Firebase 云消息传递?