在Firebase中,如何在应用程序处于后台时处理通知。

12 浏览
0 Comments

在Firebase中,如何在应用程序处于后台时处理通知。

这是我的清单文件:

    
        
    


    
        
    

当应用程序在后台运行并且收到通知时,就会出现默认通知,并且不会运行我的 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]

admin 更改状态以发布 2023年5月22日
0
0 Comments

\n\n为了使Firebase库在以下情况下调用您的onMessageReceived():\n\n

    \n

  1. 应用程序在前台
  2. \n

  3. 应用程序在后台
  4. \n

  5. 应用程序已被杀死
  6. \n

\n\n您不能将JSON键notification放入向Firebase API的请求中,而应该使用data,请参见下文。\n\n以下消息将不会在您的应用程序在后台或被杀死时调用您的onMessageReceived(),而您无法自定义通知。\n\n

{
   "to": "/topics/journal",
   "notification": {
       "title" : "title",
       "text": "data!",
       "icon": "ic_notification"
    }
}

\n\n相反,使用以下内容将起作用:\n\n

{
  "to": "/topics/dev_journal",
   "data": {
       "text":"text",
       "title":"",
       "line1":"Journal",
       "line2":"刊物"
   }
} 

\n\n基本上,消息作为RemoteMessage参数和您的数据对象作为Map 发送,然后您可以像这里的片段一样在 onMessageReceived 中管理通知。\n\n

@Override
public void onMessageReceived(RemoteMessage remoteMessage) { 
     Map data = remoteMessage.getData();
     //you can get your text message here.
     String text= data.get("text");
     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
        // optional, this is to make beautiful icon
             .setLargeIcon(BitmapFactory.decodeResource(
                                    getResources(), R.mipmap.ic_launcher))  
        .setSmallIcon(smallIcon)  //mandatory
      .......
    /*You can read more on notification here:
    https://developer.android.com/training/notify-user/build-notification.html
    https://www.youtube.com/watch?v=-iog_fmm6mE
    */
}

0
0 Comments

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

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

  1. 显示消息:只有在应用程序处于前台时,这些消息才会触发onMessageReceived()回调
  2. 数据消息:这些消息在应用程序处于前台/后台/关闭时都会触发onMessageReceived()回调

注意: Firebase团队尚未开发用于向设备发送数据消息的UI。您应该使用您的服务器来发送这种类型的消息!

2. 怎么做?

要实现这一点,您必须向以下URL执行POST请求:

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

标头

  • 键: Content-Type值: application/json
  • 键: Authorization值: 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}"]
}

注意: 确保您不添加JSON键notification以避免任何问题。

注意: 要获取您的服务器密钥,您可以在Firebase控制台中找到它:您的项目->设置->项目设置->云消息传递->服务器密钥

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

以下是处理接收到的消息的方法:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) { 
     Map data = remoteMessage.getData();
     String myCustomKey = data.get("my_custom_key");
     // Manage data
}

0