2014-04-03 108 views
8

我使用Cordova Push Notifications Plugin 1.3.4與我的科爾多瓦/ Phonegap應用程序。不幸的是,當接收到推送通知時,我的JavaScript中的ecb回調從未被觸發,我無法處理推送通知(即使應用程序在前臺運行時也不行)。科爾多瓦推送通知(PhoneGap PushPlugin)不觸發ecb回調(onNotificationAPN)

我使用的是從演示的example code

pushNotification.register(tokenHandler, errorHandler, {"badge": "true", "sound": "true", "alert": "true", "ecb": "onNotificationAPN"}); 

註冊成功,但下面的回調是永遠不會觸發:

function onNotificationAPN (event) { 
    if (event.alert) 
    { 
     navigator.notification.alert(event.alert); 
    } 
} 

回答

9

問題是你定義了回調的方式功能,導致Push Plugin評估您的回調(即通過[webView stringByEvaluatingJavaScriptFromString)失敗,因爲它不會意識到它。

如果你定義的回調函數作爲一個全球性的對象,而不是,這個插件將正確觸發你的回調每一個新的通知到達時間:

onNotificationAPN = function(event) { 
    if (event.alert) 
    { 
     navigator.notification.alert(event.alert); 
    }; 
}; 

對於Android,您可以定義您的onNotificationGCM回調以同樣的方式。

+2

不確定「parse time definition」是什麼意思。 'onNotificationAPN'必須全局定義,因爲這是'stringByEvaluatingJavaScriptFromString'評估javascript中的對象。 – artemave

+0

是的沒錯,這就是我的意思。我喜歡你放置它的方式,+1這個 – Mobiletainment

+0

無論你寫什麼,它都沒有任何意義。我不得不破譯你說的話,然後@artemave說這個id必須在全球範圍內定義。最後,讓我的設備註冊。謝謝! – AdityaSaxena

2

Mobiletainment的答案爲我解決了它!我一直在搜索整個下午的解決方案,雖然GCM回調,但功能是一樣的。

我會upvote Mobiletainment,但我是新來的,它不會讓我,但謝謝!

對於任何人可以尋找一個GCM溶液(其中有沒有很多很好的回答了許多在Googleverse)這裏是我的代碼,終於摸索:

// handle GCM notifications for Android 
      onNotificationGCM = function(e){ 
       alert('onGCM'); 
       switch(e.event) 
       { 
        case 'registered': 
         if (e.regid.length > 0) 
         { 
          console.log("Regid " + e.regid); 
          //Do registration work here... 
         } 
        break; 

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

        case 'error': 
         alert('GCM error = '+e.msg); 
        break; 

        default: 
         alert('An unknown GCM event has occurred'); 
         break; 
       } 
      }  
1

如果使用了蔚藍色的推動。下面是工作代碼(pushplugin for phonegap)

function InitpushNotificaions() { 
     // alert("Notification setup"); 
     try { 


      mobileServiceClient = new WindowsAzure.MobileServiceClient('https://mobbacktest.azure-mobile.net/', 'RvahPxHKoEsGiLdlCYZpHBllvSVQxl66'); 
      pushNotification = window.plugins.pushNotification; 
      if (device.platform == 'android' || device.platform == 'Android' || 
        device.platform == 'amazon-fireos') 
      { 
       // AndroId 

       pushNotification.register(successHandler, errorHandler, { "senderID": "724086851305", "ecb": "onNotification" });  // required! 
      } 
      else 
      { 
       //IOS 
       pushNotification.register(tokenHandler, errorHandler, { "badge": "true", "sound": "true", "alert": "true", "ecb": "onNotificationAPN" }); // required! 
      } 
     } 
     catch (err) { 
      txt = "There was an error on this page.\n\n"; 
      txt += "Error description: " + err.message + "\n\n"; 
      alert(txt); 
     } 




    } 


    // handle APNS notifications for iOS 
onNotificationAPN=function (e) { 
     if (e.alert) { 
      $("#app-status-ul").append('<li>push-notification: ' + e.alert + '</li>'); 
      // showing an alert also requires the org.apache.cordova.dialogs plugin 
      navigator.notification.alert(e.alert); 
     } 

     if (e.sound) { 
      // playing a sound also requires the org.apache.cordova.media plugin 
      var snd = new Media(e.sound); 
      snd.play(); 
     } 

     if (e.badge) { 
      pushNotification.setApplicationIconBadgeNumber(successHandler, e.badge); 
     } 
    } 

    //handle GCM notifications for Android 
    onNotification = function (e) { 
     // alert("gcm"); 
     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('step 1 Azure! ' + e.regid); 
        if (mobileServiceClient) { 
         // alert('step 2 Azure!'); 
         // Template registration. 
         var template = "{ \"data\" : {\"message\":\"$(message)\"}}"; 
         // Register for notifications. 
         mobileServiceClient.push.gcm.registerTemplate(e.regid, 
          "myTemplate", template, null) 
          .done(function() { 
          // alert('Registered template with Azure!'); 
          }).fail(function (error) { 
           //alert('Failed registering with Azure: ' + error); 
          }); 
        } 



        console.log("regID = " + e.regid); 
       } 
       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. 
        // playing a sound also requires the org.apache.cordova.media plugin 
        var my_media = new Media("/res/" + beep.wav); 
        alert("Message:"+e.message); 
        my_media.play(); 
       } 
       else { // otherwise we were launched because the user touched a notification in the notification tray. 
        if (e.coldstart) { 

        } 
        else { 

        } 
       } 


       break; 

      case 'error': 
       break; 

      default: 
       break; 
     } 
    } 


    function tokenHandler(result) 
    { 
     // $("#app-status-ul").append('<li>token: '+ result +'</li>'); 
     // 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. 
    } 

    function successHandler(result) { 
     alert("Success handler:" + result); 
    // $("#app-status-ul").append('<li>success:'+ result +'</li>'); 
    } 

    function errorHandler(error) { 
     alert("Error handler:" + error); 
    } 
相關問題