javascript - 推送通知cordova android中 undefined reference 错误onnotification

标签 javascript android cordova push-notification

使用 Cordova (ionicframework+angularjs) 构建以使用推送通知请求 Android 设备。注册时 successHandler() 被触发并显示('ok')消息结果! 为什么 onNotification() 方法没有被触发??。

var pushNotification;
    document.addEventListener("deviceready", onDeviceReady, false);
    function onDeviceReady() {

        pushNotification = window.plugins.pushNotification;
        setupNotificationsForandroid();
    }
   //begin setup
    function setupNotificationsForandroid() {
     //  alert("inside setup");
        if ( device.platform == 'android' || device.platform == 'Android' || device.platform == "amazon-fireos" ){

            pushNotification.register(
            successHandler,
            errorHandler,
            {
                "senderID":"856763042820",
                "ecb":"onNotification"
            });
        } else {
            pushNotification.register(
            tokenHandler,
            errorHandler,
            {
                "badge":"true",
                "sound":"true",
                "alert":"true",
                "ecb":"onNotificationAPN"
            });
        }
    }
    function successHandler(result){

        //alert("success"+result);

    }
    function errorHandler(){

        alert("error");
    }
    // Android
    function onNotification(e) {
        //alert("inside onnotification");
        switch( e.event )
        {
        case 'registered':
              if ( e.regid.length > 0 ){
               //set up the server call for storing registraion ids
                 alert(e.regid);
              }

        break;

        case 'message':
                // if this flag is set, this notification happened while we were in the foreground.
            if(e.foreground){
                var soundfile = e.soundname || e.payload.sound;
                var my_media = new Media("android/assets/www/"+ soundfile);
                my_media.play();
            }else{
               // otherwise we were launched because the user touched a notification in the notification tray.
            }


            break;

        case 'error':
               console.log("Error"+e.msg);
               break;

        default:
            console.log("An unknown event");
            return;
      }
    }   

这是我的控制台消息:

09-22 11:38:46.151: I/Web Console(24662): processMessage failed: Stack: ReferenceError: onNotification is not defined
09-22 11:38:46.151: I/Web Console(24662):     at eval (eval at processMessage (file:///android_asset/www/cordova.js:1006:26), <anonymous>:1:1)
09-22 11:38:46.151: I/Web Console(24662):     at processMessage (file:///android_asset/www/cordova.js:1006:13)
09-22 11:38:46.151: I/Web Console(24662):     at Function.androidExec.processMessages (file:///android_asset/www/cordova.js:1076:13)
09-22 11:38:46.151: I/Web Console(24662):     at pollOnce (file:///android_asset/www/cordova.js:944:17)
09-22 11:38:46.151: I/Web Console(24662):     at pollOnceFromOnlineEvent (file:///android_asset/www/cordova.js:939:5) at file:///android_asset/www/cordova.js:1045
09-22 11:38:46.151: D/CordovaLog(24662): file:///android_asset/www/cordova.js: Line 1046 : processMessage failed: Message: Jjavascript:onNotification({"regid":"APA91bHDLg9BZl-eicx3tS-MjVGy-mcufmbRc-EpCuKzb9b_tPddG125jyxY-OhVR5vul6az-eJ2nZ0PEiOVPqP2KjClaUwAKTBQx5gsIluI0jsGIrpCvNQdUbtKUBxNNH0DH94RHuGpjU29xhTS5Cl8qIgH1MlJHw","event":"registered"})

基于此 processMessage failed: Stack: ReferenceError: onNotification is not defined 控制台消息我认为是 onNotification() 的问题。请帮忙

最佳答案

onNotification 必须在 window 范围内。

window.onNotification = function(){

}

关于javascript - 推送通知cordova android中 undefined reference 错误onnotification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25967691/

相关文章:

javascript - 无法在 JS 而不是 HTML 中初始化 ng-model

javascript - 我想要一个切换 div 元素,以将底层内容与 z-index 重叠。我怎样才能做到这一点?

android - 应用程序是否应该处于运行状态才能触发 Intent ?

angularjs - Cordova + Angular : How to test?

cordova - Amazon S3 - POST 请求的存储桶策略?

javascript - TinyMCE:格式的自定义字符?

javascript - 使用 ObjLoader three.js 后如何计算边界框

android - android opennlp构建错误:无法访问找不到java.nio.file.Path的路径类文件

c# - MonoGame 与 Unity3D

angular - 无法添加任务 ':processDebugGoogleServices',因为同名任务已存在