0

我对开发 android 应用程序比较陌生。我正在使用带有 phonegap 的 pushwoosh 来尝试接收推送通知。我按照指示遵循了两者的设置。我现在面临的问题是,当我从我的 pushwoosh 帐户发送消息时,弹出的警报不会显示消息,但实际上显示“null”。

这是我收到的控制台消息:

05-13 20:08:26.568: D/dalvikvm(914): GC_CONCURRENT freed 273K, 21% free 3296K/4144K, paused 5ms+2ms, total 56ms
05-13 20:08:26.568: D/dalvikvm(914): WAIT_FOR_CONCURRENT_GC blocked 32ms
05-13 20:08:26.739: I/Choreographer(914): Skipped 37 frames!  The application may be doing too much work on its main thread.
05-13 20:16:36.978: V/GCMBroadcastReceiver(914): onReceive: com.google.android.c2dm.intent.RECEIVE
05-13 20:16:36.978: V/GCMBroadcastReceiver(914): GCM IntentService class: com.arellomobile.android.push.PushGCMIntentService
05-13 20:16:36.978: V/GCMBaseIntentService(914): Acquiring wakelock
05-13 20:16:37.117: I/GCMIntentService(914): Received message
05-13 20:16:37.188: V/GCMBaseIntentService(914): Releasing wakelock
05-13 20:16:37.308: E/doOnMessageReceive(914): message is: {"message":"Hello","foregroud":true,"from":"41772830302","collapse_key":"do_not_collapse","onStart":false}
05-13 20:16:37.379: D/CordovaLog(914): user data: undefined
05-13 20:16:37.379: W/Web Console(914): user data: undefined at file:///android_asset/www/PushNotification.js:147
05-13 20:16:37.579: D/dalvikvm(914): GC_CONCURRENT freed 290K, 19% free 3395K/4144K, paused 5ms+3ms, total 106ms

您可以看到控制台显示了原始消息“Hello”。

下面是我的 PushNotification.js:

function initPushwoosh()
{
    var pushNotification = window.plugins.pushNotification;
    pushNotification.onDeviceReady();

    pushNotification.registerDevice({ projectid: "PROJECT_ID", appid : "APP_ID" },
        function(status) {
            var pushToken = status;
            console.warn('push token: ' + pushToken);
        },
        function(status) {
            console.warn(JSON.stringify(['failed to register ', status]));
        }
    );

    document.addEventListener('push-notification', function(event) {
        var title = event.notification.title;
        var userData = event.notification.userdata;

        console.warn('user data: ' + JSON.stringify(userData));
        navigator.notification.alert(title);
    });
}

function init() {
document.addEventListener("deviceready", initPushwoosh, true);
}

出于某种原因,我的用户数据未定义。什么可能导致此问题?

谢谢。

4

1 回答 1

2

这是因为您没有使用从 JSON 响应接收到的消息值。尝试这个。

document.addEventListener('push-notification', function(event) {
    var title = event.notification.title;
    var userData = event.notification.userdata;
    var msg = event.notification.message;

    console.warn('user data: ' + JSON.stringify(userData));
    navigator.notification.alert(msg);
});
于 2013-05-18T19:11:29.057 回答