Cordova推送通知(PhoneGap推送插件)未触发ecb回调(onNotificationAPN)

Cordova推送通知(PhoneGap推送插件)未触发ecb回调(onNotificationAPN),cordova,phonegap-pushplugin,Cordova,Phonegap Pushplugin,我正在与我的Cordova/Phonegap应用程序一起使用。不幸的是,当收到推送通知时,JavaScript中的ecb回调从未被触发,我无法处理推送通知(即使应用程序在前台运行时也无法处理) 我正在使用演示中的: pushNotification.register(tokenHandler, errorHandler, {"badge": "true", "sound": "true", "alert": "true", "ecb": "onNotificationAPN"}); 注册成功,

我正在与我的Cordova/Phonegap应用程序一起使用。不幸的是,当收到推送通知时,JavaScript中的ecb回调从未被触发,我无法处理推送通知(即使应用程序在前台运行时也无法处理)

我正在使用演示中的:

pushNotification.register(tokenHandler, errorHandler, {"badge": "true", "sound": "true", "alert": "true", "ecb": "onNotificationAPN"});
注册成功,但从未触发以下回调:

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

问题在于您定义回调函数的方式,导致推送插件对回调函数的求值(即通过
[webView stringByEvaluatingJavaScriptFromString
)失败,因为它不会意识到这一点

如果您将回调函数定义为全局对象,则插件将在每次新通知到达时正确触发回调:

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

对于Android,你可以用同样的方式定义你的
通知GCM
回调。

Mobiletainment的答案为我解决了这个问题!我整个下午都在寻找解决方案,尽管是GCM回调,但功能是一样的

我想升级Mobiletainment,但我是新来的,它不允许我,但谢谢

对于任何可能正在搜索GCM解决方案的人(Google Verse中有很多这样的解决方案,但没有很多好的答案),我的代码终于奏效了:

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

如果您正在与azure push一起使用。下面是工作代码。(phonegap的pushplugin)

函数initpushNotifications(){
//警报(“通知设置”);
试一试{
mobileServiceClient=新的WindowsAzure.mobileServiceClient('https://mobbacktest.azure-mobile.net/“,”rvahpxhkoesgildlyzphbllvsvqxl66');
pushNotification=window.plugins.pushNotification;
如果(device.platform=='android'| | device.platform=='android'||
device.platform==“amazon fireos”)
{
//安卓
register(successHandler,errorHandler,{“senderID”:“724086851305”,“ecb”:“onNotification”});//必需!
}
其他的
{
//IOS
注册(tokenHandler,errorHandler,{“badge”:“true”,“sound”:“true”,“alert”:“true”,“ecb”:“onNotificationAPN”});//必需!
}
}
捕捉(错误){
txt=“此页面上有错误。\n\n”;
txt+=“错误描述:”+err.message+“\n\n”;
警报(txt);
}
}
//处理iOS的APNS通知
onNotificationAPN=函数(e){
如果(如警报){
$(“#应用程序状态ul”).append(“
  • 推送通知:”+e.alert+“
  • ”); //显示警报还需要org.apache.cordova.dialogs插件 navigator.notification.alert(e.alert); } 如果(如声音){ //播放声音还需要org.apache.cordova.media插件 var snd=新媒体(如声音); snd.play(); } 如果(如徽章){ pushNotification.setApplicationBadgeNumber(successHandler,e.badge); } } //处理Android的GCM通知 onNotification=函数(e){ //警报(“gcm”); 开关(如事件){ “已登记”案件: 如果(e.regid.length>0){ //您的GCM推送服务器需要知道regID才能推送到此设备 //这里是您可能希望发送regID供以后使用的地方。 //警报('step 1 Azure!'+e.regid); if(移动服务客户端){ //警报('步骤2!'); //模板注册。 var template=“{\”数据\“:{\”消息\“:\”$(消息)\“}}”; //注册通知。 mobileServiceClient.push.gcm.registerTemplate(e.regid, “myTemplate”,模板,空) .done(函数(){ //警报('已向Azure注册模板!'); }).失败(功能(错误){ //警报('向Azure注册失败:'+错误); }); } console.log(“regID=“+e.regID”); } 打破 案例“信息”: //如果设置了此标志,则此通知发生在我们在前台时。 //你可能想要播放一个声音来引起用户的注意,弹出一个对话框,等等。 如果(如前景){ //在Android上,soundname不在有效负载范围内。 //在Amazon FireOS上,所有自定义属性都包含在有效负载中 var soundfile=e.soundname | | e.payload.sound; //如果通知包含声音名称,请播放它。 //播放声音还需要org.apache.cordova.media插件 var my_media=新媒体(“/res/”+beep.wav); 警报(“信息:+e.Message”); 我的媒体播放(); } else{//否则,我们被启动是因为用户触摸了通知托盘中的通知。 如果(如coldstart){ } 否则{ } } 打破 案例“错误”: 打破 违约: 打破 } } 函数标记处理程序(结果) { //$(“#应用程序状态ul”).append(“
  • 标记:“+result+”
  • ”); //您的iOS推送服务器需要知道令牌才能推送到此设备 //这里是您可能希望向其发送令牌以供以后使用的地方。 } 函数successHandler(结果){ 警惕(“成功
        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);
        }