Cordova + Angular + PushPlugin - 没有触发回调

时间:2015-07-08 17:59:40

标签: javascript android angularjs cordova phonegap-pushplugin

我见过多个相似的项目,但没有人在同一个环境中(有角度,不使用离子)。我正在使用cordova 5.0.0(cordova --version在cmd中显示5.0.0,在设备插件模块中我看到3.6.4)

我有一个基于角度的应用程序,它在Cordova应用程序中运行。我正在尝试使用PushPlugin添加推送通知。

我能够(在主模块内部)调用推送插件的register方法,并在Android(目前只有我测试的设备)中调用"成功"处理程序被调用。无论我在何处放置回调函数,都不会调用ecb。

在我的app.js中:

angular.module('myApp', [...]).config().run(['$rootScope',..., 
function($rootScope,...) {
    // etc etc...
    document.addEventListener("deviceready", function(){
        var pushNotification = window.plugins.pushNotification;
        pushnotification.register(
            successHandler,
            errorHandler,
            {
                "senderID":"<sender_id>",
                "ecb":"window.onNotification"
            });
    });

    // this is invoked
    function successHandler (result) {
        alert('result = ' + result);
    };

    function errorHandler (error) {
        alert('error = ' + error);
    };

    // option 1:
    function onNotification(e) {...};

    // option 2:
    var onNotification = function(e) {...};

    // option 3 
    // (tried below and above the call to register
    // though I believe it doesn't matter):
    window.onNotification = function(e) {...};
}]);

// option 4:
var onNotification = function(e) {...};

到目前为止,它们都不起作用。我认为我做的范围有问题,但我不确定是什么。

是否与范围有关? 它可能是别的吗?什么?如何诊断?

编辑: 我检查了logcat,有些事情没有意义:

I/chromium       ( 3981): [INFO:CONSOLE(217)] "registering with GCM", source: file:///android_asset/www/js/app.js (217)
V/PushPlugin     ( 3981): execute: action=register
V/PushPlugin     ( 3981): execute: data=[{"senderID":"SENDER_ID","ecb":"window.onNotification"}]
V/PushPlugin     ( 3981): execute: jo={"senderID":"SENDER_ID","ecb":"window.onNotification"}
V/PushPlugin     ( 3981): execute: ECB=window.onNotification senderID=SENDER_ID
D/GCMRegistrar   ( 3981): resetting backoff for com.my.app
V/GCMRegistrar   ( 3981): Registering app com.my.app of senders SENDER_ID
W/ActivityManager( 1254): Unable to start service Intent {act=com.google.android.c2dm.intent.REGISTER pkg=com.google.android.gsf (has extras) } U=0: not found

我看到的插件版本(cordova插件列表)是2.4.0。为什么c2dm意图?难道它不能使用新的GCM吗?

1 个答案:

答案 0 :(得分:1)

事实证明,这是模拟器的一个问题。

上面的所有注释对于通知事件回调(ecb)的范围是正确的,仍然是,导致我最终让代码在emulattor中工作的原因是logcat打印输出。关于因缺少注册服务意图而无法启动注册服务意图的警告(请参阅问题中的日志摘录的最后一行)是实际问题。

搜索该警告,我发现了这个讨论:not notification on Android with GCM - hmedney在2015-07-01的回答建议在模拟器中使用不同的设备目标。更改模拟器以使用“Google API(x86系统映像)(Google Inc.) - API级别19”后,将调用onNotification函数,我会在日志中看到注册ID。