react-native-fcm应用程序在被杀死时不显示数据类型的通知

时间:2018-04-20 01:19:14

标签: android react-native-fcm

我使用Firebase云消息传递(FCM)HTTP Legacy API protocol将JSON中的推送通知发送到 Android移动设备。对于客户端,我使用react-native-fcm库。 目的是在应用程序处于3种状态时将通知发送到特定设备: 1)跑步 2)后台运行 3)被杀 根据FCM的文档,有3种不同类型的消息可以通过FCM服务发送: 1)通知(具有预定义字段) 2)数据(设置你想要的任何字段) 3)混合(通知+数据)。 使用react-native-fcm在客户端侦听传入消息的事件的逻辑是下一个:

this.notificationEmitterSubscription = FCM.on(FCMEvent.Notification, notif => {
      if(notif && notif.fcm){
        //received from Firebase
        if(!notif.local_notification && notif.title){
          let badge = parseInt(notif.badge);
          FCM.setBadgeNumber(badge);
          this.showNotification(notif.title, notif.body, badge);
        }
        //notification is clicked
        if(notif.opened_from_tray){
          FCM.setBadgeNumber(0);
          this.executeNavigateAction(notif.fcm.action); //this method just navigates user to a particular screen in the application
        }
      }
    });

显示通知方法以这种方式实现:

showNotification(title, body, badge) {
    FCM.presentLocalNotification({
      body: body,
      priority: "high",
      title: title,
      sound: "default", 
      large_icon: "ic_launcher",// Android only
      icon: "ic_launcher",
      show_in_foreground :true, /* notification when app is in foreground (local & remote)*/
      vibrate: 300, /* Android only default: 300, no vibration if you pass null*/
      lights: true, // Android only, LED blinking (default false)
      badge: badge,
      local: true,
      click_action: NAV_SCREEN_NAME
    });
  }

notif.title,notif.body和notif.badge是通过FCM API发送消息的数据部分中设置的字段。换句话说,消息以(3)混合形式发送:

{
   "registration_ids" : ["FCM_device_token_1", "FCM_device_token_2"],
   "notification" :
   {
      "title" : "fcm notification message title",
      "body" : "fcm notification message body",
      "badge" : 111
   },
   "data" :
   {
      "title" : "fcm data message title",
      "body" : "fcm data message body",
      "badge" : 222
   }
}

如果邮件是作为(1)通知发送的(没有"数据"部分在邮件中,在这种情况下,在阅读字段时需要进行一些更改,以便更改notif.title - > notif.fcm.title,但这不是问题中的要点)或 mixed(3)然后当应用程序是时,不会触发通知的监听器(2)背景运行和(3)杀死。因此,未设置徽章编号。但是,尽管未调用方法showNotification(标题,正文,徽章)(因为未触发事件监听器),显示消息。似乎react-native-fcm具有针对这种情况的内部实现,以在应用程序未运行时自动显示(1)通知和(3)混合消息。换句话说,侦听器被调用(1)通知和(3)混合消息仅当应用程序是(1)运行并且当应用程序在(2)背景中时不被调用或(3)被杀死并且不显示徽章编号。但是,消息本身适用于所有情况。

另一种方法是发送(2)数据消息。这种类型的FCM消息触发应用程序的所有状态的监听器(notificationEmitterSubscription):(1)运行和(2)后台运行和(3)被杀。因此,徽章编号在所有这些状态中设置。但是,尽管每当收到数据FCM消息时都会调用方法showNotification(title,body,badge),但方法 FCM.presentLocalNotification不会在应用程序被杀死时显示消息

因此,简而言之,我有一个问题。

如何: 当(1)通知或(3)收到混合消息并且应用程序处于(2)后台运行或(3)被杀时,显示徽章编号 当应用程序被(3)杀死时显示(2)数据消息?

谢谢!

1 个答案:

答案 0 :(得分:0)

已找到解决方案。声明是:如果应用程序被终止,则没有代码在运行,因此消息将被处理并显示在代码之外。必须以下一格式设置消息,以显示已终止状态:

{
   "registration_ids" : ["FCM_token_1", "FCM_token_2"],
   "data" : 
   {
      "custom_notification" :
      {
         "title" : "FCM test title",
         "body" : "FCM test body"
      },
      badge : 1
   }
}

在通知处理程序中的react-native应用程序中,通知作为notif.custom_notification属性的json值接收。所以,代码看起来像这样:

this.notificationEmitterSubscription = FCM.on(FCMEvent.Notification, notif => {
      if(notif && notif.fcm){
        //received from Firebase
        if(!notif.local_notification && notif.custom_notification){
          let message = JSON.parse(notif.custom_notification);
          let body = message.body;
          let title = message.title;
          let badge = parseInt(notif.badge);
          FCM.setBadgeNumber(badge);
          this.showNotification(title, body, badge);
        }
        //notification is clicked
        if(notif.opened_from_tray){
          FCM.setBadgeNumber(0);
          this.executeNavigateAction(notif.fcm.action);
        }
      }
    });

这个问题可以解决为一个问题。

相关问题