首页 文章

Cordova Push Plugin:onNotificationGMC未被触发且无法获取regID

提问于
浏览
5

大家好我'm developing a cordova Hybrid app that requires the Push Notification Service of Android and iOS to work and so I'已经安装了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上测试这个应用程序 .

3 回答

  • 3

    固定

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

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

    现在它给你regID :)

  • 0

    我有同样的问题 . 如果您使用的是AngularJS IonicFramework,则不必执行此操作:

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

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

    });

    函数onNotificationGCM(e){}

    我在工厂内创建了onNotificationGCM . 这解决了我的问题 . 我希望它可以帮助你 .

  • 0

    在离子框架中,您有一个现成的插件: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);
    });
    

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

相关问题