0

我在<gap:plugin name="com.phonegap.plugins.pushplugin" />config.xml 中添加了。当我运行该应用程序时,即使手机未连接到互联网,它也会显示警报“回调成功!结果 = OK”,然后我按 OK,没有其他反应。

onNotificationGCM 事件不会触发,因此它永远不会返回 regid。

function showAlert(message, title) {
    if (navigator.notification) {
        navigator.notification.alert(message, null, title, 'OK');
    } else {
        alert(title ? (title + ": " + message) : message);
    }
}   

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
    $(document).ready(function () {navigator.splashscreen.hide();});    

    try {
        var pushNotification = window.plugins.pushNotification;
        pushNotification.register(successHandler, errorHandler, {"senderID":"659859389520","ecb":"onNotificationGCM"});
    } catch (ex) {
        showAlert(ex, 'push error');
    }

    setTimeout(function() {
        navigator.splashscreen.hide();
    }, 3000);

}

function successHandler(result) {
    showAlert('Callback Success! Result = '+result, "Success");
}

function errorHandler(error) {
    showAlert(error, "Error");
}

function onNotificationGCM(e) {

    switch( e.event )
    {
        case 'registered':
        if ( e.regid.length > 0 )
        {
            console.log("Regid " + e.regid);
            showAlert('registration id = '+e.regid);
        }
        break;

        case 'message':

        showAlert('message = '+e.message+' msgcnt = '+e.msgcnt, "Message");
        break;

        case 'error':
        showAlert('GCM error = '+e.msg, "Error");
        break;

        default:
        showAlert('An unknown GCM event has occurred', "Unknown Event");
        break;
    }
}

在 ADB 日志中,GCM 工作得非常好。它完美地返回了一个 regid,但 sendJavascript 没有调用我的 onNotificationGCM 函数。因此它不起作用。为什么?我在这里做错了什么?

05-22 01:28:27.407: V/PushPlugin(21372): sendJavascript: javascript:onNotificationGCM({"regid":"APb91bG...Sqg4YP","event":"registered"})

4

1 回答 1

0

解决了这个问题。在调用 onNotificationGCM 之前,我正在从 index.html 重定向到另一个页面。因此它根本没有被调用。

为了测试,我将重定向代码放在 onNotificationGCM 中,它运行良好。

于 2014-05-23T01:45:03.850 回答