2014-12-03 81 views
1

我在android中遇到PushPlugin問題。一直在尋找解決方案的時間,但仍然沒有成功。
當我收到推送通知時,ecb未被觸發。
它在註冊表中成功觸發,但是當我關閉應用程序並獲得通知時,則通過點擊通知打開應用程序,但未調用該應用程序。Phonegap PushPlugin未在android上觸發ecb

這裏是我的代碼:

var Utility = { 
    registerPushNotification : function() { 
     if(typeof device != 'undefined') 
     { 
      // checking if device token is exist 
      var deviceToken = dbUtil.getValue('deviceToken'); 
      if(deviceToken==null) 
      { 
       if (device.platform == 'android' || device.platform == 'Android' || device.platform == "amazon-fireos"){ 
        window.plugins.pushNotification.register(
         function(result) { 
          alert('result = ' + result); 
         }, 
         function(error) { 
          alert('error = ' + error); 
         }, 
         { 
          "senderID":'my_project_number', 
          "ecb":"Utility.onNotification" 
         } 
        ); 
       } else if (device.platform == 'iOS'){ 
        window.plugins.pushNotification.register(
        function(result) { 
         // Your iOS push server needs to know the token before it can push to this device 
         // here is where you might want to send it the token for later use. 
         // alert('device token = ' + result); 
         dbUtil.setValue('deviceToken', result); 
         // upload deviceToken to server 
         Utility.registerDevice(result,app.CONSTANTS.TYPE_APNS); 
        }, 
        function(error) { 
         // alert('error = ' + error); 
         pageUtil.drawToast('error getting device token'); 
        }, 
        { 
         "badge":"true", 
         "sound":"true", 
         "alert":"true", 
         "ecb":"Utility.onNotificationAPN" 
        }); 
       }    
      } 
     } 
    }, 
    onNotificationAPN : function (event) { 
     if (event.alert) 
     { 
      navigator.notification.alert(event.alert); 
     } 
     if (event.sound) 
     { 
      var snd = new Media(event.sound); 
      snd.play(); 
     } 
     if (event.badge) 
     { 
      // window.plugins.pushNotification.setApplicationIconBadgeNumber(successHandler, errorHandler, event.badge); 
     } 
    }, 
    // Android and Amazon Fire OS 
    onNotification : function(e) { 
     switch(e.event) 
     { 
      case 'registered': 
      { 
       if (e.regid.length > 0) 
       { 
        // Your GCM push server needs to know the regID before it can push to this device 
        // here is where you might want to send it the regID for later use. 
        // alert(e.regid); 
        // console.log("regID = " + e.regid); 
        dbUtil.setValue('deviceToken', e.regid); 
        // upload deviceToken to server 
        Utility.registerDevice(e.regid,app.CONSTANTS.TYPE_GCM); 
       } 
       break;    
      } 
      case 'message': 
      { 
       // if this flag is set, this notification happened while we were in the foreground. 
       // you might want to play a sound to get the user's attention, throw up a dialog, etc. 
       if (e.foreground) 
       { 
        // on Android soundname is outside the payload. 
        // On Amazon FireOS all custom attributes are contained within payload 
        var soundfile = e.soundname || e.payload.sound; 
        // if the notification contains a soundname, play it. 
        var my_media = new Media("/android_asset/www/"+ soundfile); 
        my_media.play(); 
        alert(e.payload.message); 
       } 
       else 
       { 
        pageUtil.loadingFullPage(true,e.payload.message); 
        // otherwise we were launched because the user touched a notification in the notification tray. 
        if (e.coldstart) 
        { 
         // $("#app-status-ul").append('<li>--COLDSTART NOTIFICATION--' + '</li>'); 
        } 
        else 
        { 
         // $("#app-status-ul").append('<li>--BACKGROUND NOTIFICATION--' + '</li>'); 
        } 
       } 
       break; 
      } 
      case 'error': 
      { 
       alert('error getting google notification'); 
       break; 
      } 
      default: 
      { 
       break; 
      } 
     } 
    }, 
    ..... 
}; 

這裏是我註冊的ecb叫,我可以得到regID

Utility.registerPushNotification(); 

但情況e.event = 「消息」 是從未打過電話

注:

window.plugins.pushNotification.register只調用一次。每次啓動應用程序時,我是否需要撥打register

回答

0

嘗試把 Utility.registerPushNotification()deviceready事件中。

相關問題