2016-11-25 60 views
2

我使用nativescript-local-notifications和nativescript-plugin-firebase。Nativescript。 nativescript-local-notifications總是創建兩個通知

我想在服務器向我發送消息時每次創建通知。這裏是代碼:

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); 
     } 
    ); 
} 

但始終創建​​一個的兩個通知代替。並且第一個通知是空的。第二個通知是我創建的。怎麼了?

enter image description here

回答

0

我可能在這裏錯了,但是您使用兩個插件來處理遠程通知?

nativescript-plugin-firebase已經爲你處理通知,我不認爲你需要本地通知插件。

在您發佈的示例中,您將收到原始通知(空白的通知),其中包含您在onMessageReceivedCallback中創建的本地通知。

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

+0

可能你是對的,但是當我刪除LocalNotifications.schedule(...)時,兩個通知都不會創建。 –

+0

是的,你是完全正確的,很抱歉這麼長的答案 –

0

的是沒有在你的onMessageReceived,以確定是否有消息的任何內容。 您可以嘗試向onMessageReceived函數添加條件語句並檢查消息是否包含內容。例如

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

我檢查一下。但這並不能解決問題。 –

0

我找到了解決方案。每次創建LocalNotification時,我都會刪除ID爲0的通知: LocalNotifications.cancel(0); 但我仍然不知道,它來自哪裏。