gpt4 book ai didi

javascript - Cordova + Angular + PushPlugin - 未触发回调

转载 作者:行者123 更新时间:2023-11-29 20:37:33 26 4
gpt4 key购买 nike

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

我有一个在 Cordova 应用程序中运行的基于 Angular 应用程序。我正在尝试使用 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。为什么 Intent 是c2dm?它不应该使用较新的 GCM 吗?

最佳答案

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

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

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

关于javascript - Cordova + Angular + PushPlugin - 未触发回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31300230/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com