2013-10-09 70 views
1

我目前正在爲android和iOS開發一個Phonegap 3.0應用程序。我加入了PushPlugin,幾乎一切正常,在Android上,除了兩件事情:Phonegap PushPlugin消息事件被調用兩次

1. 當我收到一個推送通知和我的應用程序是不是當前在前臺的消息在我的通知欄上顯示。只要我點擊通知,應用程序就會啓動,通知消息會顯示兩次。顯示的消息是一個簡單的javascript警報,其中包含通知數據,我在「onNotificationGCM」消息事件中添加了該警報。

第一次觸發此事件,當通知欄中添加通知時,第二次觸發通知並啓動我的應用程序。因此,帶有我的消息的警報功能被調用兩次,並顯示2條警報。

這裏是S一小段從我的代碼:

onNotificationGCM: function (e) { 
    switch(e.event) 
    { 
     case 'registered': 
      if (e.regid.length > 0) 
      { 
       console.log('Regid ' + e.regid); 
      } 
     break; 

     case 'message': 
      // this is the actual push notification. its format depends on the data model from the push server 
      console.log('Entered message'); 
      alert('message = '+e.message); 
     break; 
    } 
} 

所以我的問題是,我如何防止這種情況,當我打開我的應用程序通知只顯示一次?

2. 我也有這個問題,這已經張貼在GitHub的回購問題:當我離開我的應用程序(不通過設置中的「管理應用程序」菜單Issue

),我無法收到任何推送通知。我試圖在啓動時啓動我的應用程序,但這不起作用。但是當我啓動應用程序時,顯示所有通知。

也許有人已經知道了一些解決方法。

我還注意到,PushPlugin使用不推薦的GCM方法。有誰知道這是否可能是原因,爲什麼即使應用程序沒有運行,通知也不會顯示?

回答

1

好吧,所以我想我自己的第一點。我不再使用警報功能,而是使用了使用通知插件的cordova.exec()函數。在這個我引用了一個回調函數,如果單擊警報按鈕。之後,我添加了一個小標誌,指示警報是否已被查看並單擊。只要國旗說,該消息尚未確認,則不會顯示其他通知。並且,只有當應用程序處於後臺時,通知纔會顯示一次。以下是簡短的代碼段:

var confirmedGcmNotification = true; 

... 

onNotificationGCM: function (e) { 
    switch(e.event) 
    { 
     case 'message': 
      // this is the actual push notification. its format depends on the data model from the push server 
      console.log('Entered message');     

      if (e.foreground) 
      { 
       // When app is already open 
       cordova.exec(null, null, 'Notification', 'alert', [e.message, 'Notification', 'OK']); 
      } 
      else 
      { // otherwise we were launched because the user touched a notification in the notification tray. 
       if (e.coldstart) 
       { 
        console.log('coldstart entered'); 
        cordova.exec(null, null, 'Notification', 'alert', [e.message, 'Notification', 'OK']); 
       } 
       else 
       { 
        console.log('Background entered'); 
        if(confirmedGcmNotification) { 
         confirmedGcmNotification = false; 
         cordova.exec(PushSupport.gcmNotificationBackgroundAlert, null, 'Notification', 'alert', [e.message, 'Notification', 'OK']); 
        } 
       } 
      } 
     break; 
    } 
}, 

gcmNotificationBackgroundAlert: function() { 
    confirmedGcmNotification = true; 
}, 

第二點有點不同。我還沒有解決方案,但我檢查了Android日誌,並注意到,當應用程序關閉時,我發出一個新的通知,應用程序收到通知,但插件處理它不知何故錯誤,並不會顯示它。也許很快就會有修復。

+0

感謝您的支持。我希望你很快就能找到其他部分的解決方案。 – aharris88

+0

謝謝,這對我有用。我無法複製你的第二個問題。你看到了什麼設備? – TWilly