[英]Phonegap PushPlugin message event gets called twice
我目前正在為Android和iOS開發Phonegap 3.0應用程序。 我添加了PushPlugin ,除了2件事,幾乎所有東西在android上都能正常工作:
1.當我收到推送通知並且我的應用當前不在前台時,該消息會顯示在我的通知欄中。 單擊通知后,應用程序即會啟動,並且通知消息會顯示兩次。 所顯示的消息是帶有通知數據的簡單javascript警報,我在“ onNotificationGCM”消息事件中添加了該通知數據。
當通知添加到通知欄中時,第一次觸發此事件;當我單擊通知並啟動應用程序時,第二次觸發。 因此,帶有我的消息的警報功能被調用兩次,並顯示2個警報。
這是我的代碼的簡短片段:
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方法。 有誰知道這可能是原因,為什么即使應用程序未運行也不會顯示通知?
好的,所以我想出了我的第一點。 我不再使用警報功能,而是使用了使用通知插件的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日志並注意到,當應用關閉時,我發送了一個新的Notification,該應用收到了該通知,但插件以某種方式處理了該錯誤,因此不會顯示它。 也許很快就會有修復。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.