当应用处于后台状态时,无法从推送通知中获取标题和消息

时间:2020-10-17 21:58:27

标签: android firebase react-native firebase-cloud-messaging react-native-firebase

我正在尝试构建react-native应用程序,该应用程序具有使用react-native-push-notification实现的推送通知。现在,当应用程序处于“前景”状态时,我能够处理notification.title和notification.message并在应用程序的警报中显示(通过,PushNotification.configure的onNotification)。当应用程序处于“后台”状态时,我能够收到通知,当我单击它时,该应用程序就会打开或进入前台。在这里,我想获取要显示的相同的notification.title和notification.message,但在通知JSON中看不到这些字段(notification.data也为空)。以下是PushNotification.configure的onNotification中的代码:

    onNotification: function (notification) {
        console.log('NOTIFICATION:', JSON.stringify(notification));
        if (notification.foreground) {
          if(notification.userInteraction) {
            Alert.alert("Title:" + notification.title + " Message:" + notification.message);
          } else {
            PushNotification.localNotification({
              id: notification.id,
              autoCancel: true,
              title: notification.title,
              message: notification.message,
              vibrate: true,
              vibration: 300,
              playSound: true,
              soundName: 'default',
            });
            PushNotification.cancelLocalNotifications({id: notification.id});
          }
        } else {
          console.log("notification.data:", JSON.stringify(notification.data));
          if(notification.userInteraction) {
            console.log("Showing modal with notification details...");
          }
        }
      },

感谢您抽出宝贵时间对此进行研究。

0 个答案:

没有答案