首页 > 解决方案 > 无法使用“node fcm-notification”显示 FCM 通知

问题描述

我正在开发一个视频通话应用程序,当有来电时,用户会通过通知收到通知,并且能够单击通知开始通话。现在,当应用程序关闭或在后台运行时会推送通知,但在前台打开应用程序时不会推送。

此外,当应用程序处于后台或未运行时单击通知只会打开应用程序而不会启动视频通话。我可以开始通话的唯一方法是在应用程序处于后台/未运行时接收通知,手动打开应用程序,然后单击通知。

这是我处理通知发送的代码:

var fcm = require('fcm-notification');
var serverKey = require('./fcmKey.json');
var FCM = new fcm(serverKey);

const payloadBody = {
     room,
     caller,
     language,
     receiver
}

var message = {
     token : deviceToken,
     data : payloadBody,
     notificaton: {
         title : 'MyApp',
         body: 'Somebody is calling'
}

FCM.send(message, function(err, response) {
    if(err) {
        console.error('Notification error: ${JSON.stringify(err)}');
    } else {
        console.error('Notification success: ${JSON.stringify(response)}');
    }
}

我的清单还包含这样的 fcm 服务:

<service android:name="com.evollu.react.fcm.MessagingService" android:enabled="true" android:exported="true">
  <intent-filter>
      <action android:name="com.google.firebase.MESSAGING_EVENT"/>
  </intent-filter>
</service>

标签: androidnode.jsfirebasepush-notificationfirebase-cloud-messaging

解决方案


您所描述的前台和后台行为正是通知类型消息应该如何工作。如果您想控制向用户显示的通知的行为,您应该从消息中删除通知有效负载,并自己构建整个事物以响应数据有效负载。您将在您创建的 FirebaseMessagingService 子类中收到该消息。


推荐阅读