【问题标题】:Cordova Push Plugin: onNotificationGMC is not fired and cannot obtain regIDCordova Push Plugin:onNotificationGMC 未触发且无法获取 regID
【发布时间】:2014-10-29 04:07:38
【问题描述】:

大家好,我正在开发一个 Cordova Hybrid 应用程序,它需要 Android 和 iOS 的推送通知服务才能工作,因此我安装了 cordova 插件“PushPlugin”。

这是我的代码

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

function deviceready() {
    try {
        pushNotification = window.plugins.pushNotification;
        pushNotification.unregister(successHandler, errorHandler);
        pushNotification.register(
            successHandler,
            errorHandler, {
                "senderID": "7645XXXXXXXX",
                "ecb": "onNotificationGCM"
            });

        function successHandler(data) {
            alert("SUCCESS: " + JSON.stringify(data));
        };

        function errorHandler(e) {
            alert("ERROR" + e);
        }


        function onNotificationGCM(e) {
            alert("Done")
        }

    } catch (e) {
        alert(e);
    }
}

当我运行我的应用程序时,我希望有两个警报:succesHandler 一个和 onNotificationGCM 一个,但它只会触发 succesHandler 一个说:“OK”...有了这个问题,我什至无法访问 regID 参数将存储在我的服务器中...

谁能解释一下如何获得 regID.. 我所有的工作都依赖于此

我正在装有 Android 4.4.2 的 Galaxy S4 Mini 上测试此应用。

【问题讨论】:

    标签: android ios cordova push-notification google-cloud-messaging


    【解决方案1】:

    在离子框架中,您有一个现成的插件:http://ngcordova.com/docs/plugins/pushNotifications/

    这是一个适用于 android 设备的工作代码示例:

    module.run(function($cordovaPush) {
    
    var androidConfig = {
        "senderID": "replace_with_sender_id",
        "ecb": "replace_with_the_name_of_function that will return you the regid"
    };
    
    document.addEventListener("deviceready", function(){
    $cordovaPush.register(config).then(function(result) {
      // Success
    }, function(err) {
      // Error
    })
    
    window.function replace_with_ecb(notification) { //notification.regid
      switch(notification.event) {
        case 'registered':
          if (notification.regid.length > 0 ) {
            alert('registration ID = ' + notification.regid);
          }
          break;
    
        case 'message':
          // this is the actual push notification. its format depends on the data model from the push server
          alert('message = ' + notification.message + ' msgCount = ' + notification.msgcnt);
          break;
    
        case 'error':
          alert('GCM error = ' + notification.msg);
          break;
    
        default:
          alert('An unknown GCM event has occurred');
          break;
      }
    };
    
    }, false);
    });
    

    此代码仅适用于真实设备(不是模拟器)

    【讨论】:

    • 我得到了 regID,但是当我在上面提交消息时,它显示“InvalidRegistration”,我检查了项目编号(发件人 id)并激活了 GCM API .. 并查看了服务器 api 密钥.. 全部好的.. 但它说.. invalidRegistration Error.. 有什么帮助吗?
    【解决方案2】:

    我遇到了同样的问题。如果您使用的是 AngularJS + IonicFramework,则不必这样做:

    使用 onDeviceReady 函数创建工厂后,创建 onNotificationGCM 函数。像这样的:

    app.factory('PushProcessingService', function () { ..

    });

    函数 onNotificationGCM(e) { }

    我在我的工厂内创建 onNotificationGCM。这解决了我的问题。希望对你有帮助。

    【讨论】:

    • 我遇到了同样的问题,并听取了您的建议。不幸的是,它没有像我预期的那样工作。我必须在哪里调用 PushProcessingService?我把它放在这里 app.run(function($ionicPlatform, PushProcessingService) {PushProcessingService.initialize(); .... }
    【解决方案3】:

    已修复

    我将 onNotificationGCM 移动到一个空的脚本标签中,如下所示:

    <script>
    function onNotificationGCM(result) {
        alert(result.regid);
    }
    </script>
    

    现在它给你 regID :)

    【讨论】:

    • 我必须将onNotificationGCMonNotificationAPNS 等回调函数附加到window。似乎是某种范围界定问题。
    • 我得到了 regID,但是当我在上面提交消息时,它显示“InvalidRegistration”,我检查了项目编号(发件人 ID),还激活了 GCM API.. 并查看了服务器 api 密钥.. 全部好的.. 但它说.. invalidRegistration Error.. 有什么帮助吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-08
    • 2016-02-13
    • 2023-03-14
    • 1970-01-01
    相关资源
    最近更新 更多