本机脚本。 nativescript-local-notifications 总是创建两个通知

Nativescript. nativescript-local-notifications always creates two notifications

我使用 nativescript-local-notifications 和 nativescript-plugin-firebase。

我想在每次服务器向我发送消息时创建通知。这是代码:

firebase.init({
        storageBucket: 'gs://shao-by-partner-firebase.appspot.com',
        persist: true, // optional, default false


        onPushTokenReceivedCallback: function (token) {
            Config.device_token = token;
            console.log("Firebase plugin received a push token: " + token);
        },
        onMessageReceivedCallback: function (message) {
                  LocalNotifications.schedule([{
                        id: 1,
                        title: 'The title',
                        body: 'Recurs every minute until cancelled',
                        ticker: 'The ticker',
                        badge: 1,
                        smallIcon: 'res://heart.png'
                      }]).then(
                          function() {
                            console.log("Notification scheduled");
                          },
                          function(error) {
                            console.log("scheduling error: " + error);
                          }
                      )
        }
    }).then(
        function (result) {
            console.log("Firebase is ready");
        },
        function (error) {
            console.log("firebase.init error: " + error);
        }
    );
}

但总是创建两个通知而不是一个。并且第一个通知是空的。第二个通知是我创建的。怎么了?

您的 onMessageReceived 中没有任何内容来确定消息是否有任何内容。 您可以尝试向 onMessageReceived 函数添加条件语句并检查消息是否有内容。例如

if (message.title){
   //send local notification
}

我在这里可能是错的,但是您使用两个插件来处理远程通知?

nativescript-plugin-firebase 已经为您处理了通知,我认为您不需要本地通知插件。

在您发布的示例中,您收到的是原始通知(空白通知),加上您在 onMessageReceivedCallback 中创建的本地通知。

https://github.com/EddyVerbruggen/nativescript-plugin-firebase/blob/master/docs/MESSAGING.md#handling-a-notification

我找到了解决方案。每次,当我创建 LocalNotification 时,我都会删除 ID 为 0 的通知: LocalNotifications.cancel (0); 但我还是不知道,它是从哪里来的。