Cordova + Angular + PushPlugin - 未触发回调

Cordova + Angular + PushPlugin - Callback not fired

我见过多个相似的项目,但没有一个是在相同的上下文中(使用 angular,不使用 ionic)。我正在使用 cordova 5.0.0(cmd 中的 cordova --version 显示 5.0.0,在设备插件模块中我看到 3.6.4)

我有一个基于 angular 的应用程序,它在 Cordova 应用程序中运行。我正在尝试使用 PushPlugin.

添加推送通知

我能够(在主模块内)调用推送插件的 register 方法,在 Android(目前我测试的唯一设备)中,"success" 处理程序是叫。不管我把回调函数放在哪里,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) {...};

到目前为止,none 个有效。我假设我在范围上做错了,但我不确定是什么。

范围内的东西吗? 会不会是别的东西?什么?如何诊断?

编辑: 我检查了 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 吗?

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

上面关于通知事件回调 (ecb) 范围的所有评论都是正确的,但是,让我最终让代码在模拟器中工作的是 logcat 打印输出。由于缺少注册服务意图而无法启动的警告(请参阅问题中日志摘录的最后一行)是实际问题。

谷歌搜索该警告,我发现了这个讨论:not notification on Android with GCM - hmedney 在 2015 年 7 月 1 日的回答建议在模拟器中使用不同的设备目标。将模拟器更改为使用 "Google APIs (x86 System Image)(Google Inc.) - API Level 19" 后,调用了 onNotification 函数,我在日志中看到了注册 ID。