2

我已按照本教程进行操作,并且有以下代码:

onDeviceReady 我执行:

var pushNotification = window.plugins.pushNotification;
pushNotification.register(app.successHandler, app.errorHandler,{"senderID":"824841663931","ecb":"app.onNotificationGCM"});

处理程序:

// result contains any message sent from the plugin call
successHandler: function(result) {
    alert('Callback Success! Result = '+result)
},

errorHandler:function(error) {
    alert(error);
},

onNotificationGCM: function(e) {
        switch( e.event )
        {
            case 'registered':
                if ( e.regid.length > 0 )
                {
                    console.log("Regid " + e.regid);
                    alert('registration id = '+e.regid);
                    localStorage.regid = e.regid
                }
            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;
        }
    }

如果我的设备在我第一次打开应用程序时连接到互联网,则此代码可以完美运行。

如果我的设备未连接,则使用“OK”调用successHandler,并且永远不会调用onNotificationGCM。这是正常的吗?

我原以为注册会失败并使用 e.event = 'error' 调用 errorHandler 或 onNotificationGCM,这样我就可以推迟注册,但这并没有发生。

我会很感激任何帮助,谢谢。

4

1 回答 1

1
  1. 登记

    当你打电话时pushNotification.register()

    • successHandlerecb如果解析您的and没有错误,则调用senderID。您senderID和应用程序已在GCMRegistrar注册。
    • errorHandler仅当解析ecbor时出现 JSON 错误时才调用senderID

    一旦您的设置正确,您errorHandler可能永远不会被调用。

    docs开始,您必须在register每次启动应用程序时调用该方法。这不会创建重复注册。该插件将自行管理所有这些。

  2. onNotificationGCM

    仅在以下情况下调用:

    • 您会收到来自 GCM 的通知。
    • 您的应用程序已成功注册GCMRegistrar
  3. GCM注册器

    • senderID如果已经注册,则返回注册 ID(用于您的应用程序和)。
    • 如果没有,它会注册您的应用程序,senderID然后返回注册 ID。这需要 Internet 连接 (AFAIK)

    所以当你第一次调用pushNotification.register时,如果你没有联网,PushPlugin 会返回,OK但是 GCMRegistrar 没有注册 ID 可以返回给你。

于 2015-04-08T15:44:26.263 回答