简体   繁体   English

Ionic Push Android推送通知返回“未定义”消息

[英]Ionic Push Android push notification returning “undefined” message

I've been trying to get Android push notifications working for my app for a little while now (iOS already completed) and have everything sorted out besides just getting the notification to actually show up on the Android device. 我一直试图让Android推送通知在我的应用程序上运行一段时间(iOS已经完成),除了只是通知实际显示在Android设备上之外,还要整理所有内容。

Registering the device id's, and pushing to the GCM server all seem to be working fine, but when I test what the message in the response back from GCM is returning I keep getting undefined . 注册设备ID,并推送到GCM服务器似乎都工作正常,但当我测试从GCM返回的响应中的消息返回时,我一直undefined

All responses when pushing the message to GCM are success, correct device id's, a message id associated with it etc. Anyone able to point me in the right direction? 将消息推送到GCM时的所有响应都是成功的,正确的设备ID,与之关联的消息ID等等。任何人都能指出正确的方向吗? Below you will see the code snippet with just a sample "alert" being used to display what is coming back that will end up being used as the notification in the "push". 下面您将看到代码片段,其中只有一个示例“警报”用于显示将返回的内容,最终将用作“推送”中的通知。

This alert 这个警报

alert('message = ' + e.message + ' payload message: ' + e.payload.message +
       ' e payload msgcnt: ' + e.payload.msgcnt + ' e.msg: ' + e.msg);

doesn't seem to be getting anything back to display the push. 似乎没有得到任何回来显示推动。

function onDeviceReady() {
    console.log('deviceready');
    try {
        pushNotification = window.plugins.pushNotification;
        if (device.platform == 'android' || device.platform == 'Android' || device.platform == 'amazon-fireos') {
            console.log('PN register');
            pushNotification.register(successHandler, errorHandler, {
                "senderID": "177718756870",
                "ecb": "onNotification"
            });     // required!
            console.log('after PN register');

        } else {
            console.log('PN register');
            pushNotification.register(tokenHandler, errorHandler, {
                "badge": "true",
                "sound": "true",
                "alert": "true",
                "ecb": "onNotificationAPN"
            }); // required!
            console.log('after PN register');
        }
    }
    catch (err) {
        txt = "There was an error on this page.\n\n";
        txt += "Error description: " + err.message + "\n\n";
        console.log("ERROR", txt);

    }

}

var pushNotification;

// handle GCM notifications for Android
window.onNotification = function(e) {

    console.log('EVENT RECEIVED ' + e.event)
    console.log("regID BEFORE CHECKS = " + e.regid);

    switch( e.event )
    {

        case 'registered':
            if ( e.regid.length > 0)
            {
                console.log("regID = " + e.regid);

                var data =
                {
                    'device_id': e.regid,
                    'platform': device.platform,
                    'os_version': device.version,
                    'app_version': lawnmowerConfig.versionString,
                    'device_model': device.model
                };

                localStorage.setItem('push_data', JSON.stringify(data));
            }
            break;

       case 'message':
           console.log('Inside case message: ' + e.regid)

           if (e.foreground)
           {
               // Add something to play a sound once working
           }
           else
           {    
                if (e.coldstart) {
                    console.log("coldstart");

                }
                else {

                    console.log("not coldstart");

                }
           }

           alert('message = ' + e.message + ' payload message: ' + e.payload.message + ' e payload msgcnt: ' + e.payload.msgcnt + ' e.msg: ' + e.msg);

           break;

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

           break;

           default:
                // Testing using these alerts instead
                alert('An unknown GCM event has occurred');

           break;
    }
};

function tokenHandler (result) {
    console.log('push token handler');
    console.log(result);
    var data =
    {
        'device_id': result,
        'platform': device.platform,
        'os_version': device.version,
        'app_version': lawnmowerConfig.versionString,
        'device_model': device.model
    };
    localStorage.setItem('push_data', JSON.stringify(data));
}

function successHandler (result) {

    console.log('success handler push success');
    console.log("result: " + result);
}

function errorHandler (error) {
    console.log('push error');
}

document.addEventListener('deviceready', onDeviceReady, true);

Registering the device id's, and pushing to the GCM server all seem to be working fine, but when I test what the message in the response back from GCM is returning I keep getting undefined . 注册设备ID,并推送到GCM服务器似乎都工作正常,但当我测试从GCM返回的响应中的消息返回时,我一直undefined

It means that when you are testing "what the message .... is" you are referencing a variable not yet defined. 这意味着当您测试“消息....是什么”时,您正在引用尚未定义的变量。 In this line: 在这一行:

alert('message = ' + e.message + ' payload message: ' + e.payload.message +
      ' e payload msgcnt: ' + e.payload.msgcnt + ' e.msg: ' + e.msg);

there is no variable e.message . 没有变量e.message The data that you send from your server is attached to e.payload and value of e.event is set to message . 您从服务器发送的数据附加到e.payloade.event值设置为message I think your problem can be solved if you remove e.message . 我认为如果你删除e.message可以解决你的问题。 Something like: 就像是:

alert('event = ' + e.event + ' payload message: ' + e.payload.message +
      ' e payload msgcnt: ' + e.payload.msgcnt + ' e.msg: ' + e.msg);

Note To detect variable issues (scope and/or declaration), debug one variable at a time. 注意要检测变量问题(范围和/或声明),请一次调试一个变量。 This will help you to precisely identify a problem and trace it to its origin. 这将帮助您精确识别问题并将其跟踪到其原点。

I would advise you to use 我建议你使用

alert("Payload message: " + e.payload.message);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM