gpt4 book ai didi

cordova - 推送插件 : Can't find variable: onNotificationAPN

转载 作者:行者123 更新时间:2023-12-01 02:18:55 26 4
gpt4 key购买 nike

我正在使用使用推送通知的 Cordova/Phonegap 构建一个 iOS 应用程序。

我正在使用 PushPlugin在客户端应用程序中实现通知。

我已经设置了 APNS 方面,当我在应用程序暂停或关闭时发送通知时,通知会正确显示。但是,当我在应用程序中时,插件会引发错误:

Can't find variable: onNotificationAPN

当我查看 Xcode 输出时,我看到通知实际上已发送:
2014-03-02 23:12:58.746 EASP 2014[5792:60b] Notification received
2014-03-02 23:12:58.747 EASP 2014[5792:60b] Msg: {"alert":"testing...",foreground:"1"}

但是在应用程序中什么也没有发生,我被 onNotificationAPN 错误困住了。

我已经尝试了一切来调试这个,但我被卡住了。知道为什么会这样吗?

这是我用来设置通知的代码:
  // SET UP PUSH NOTIFICATIONS
var pushNotification;
pushNotification = window.plugins.pushNotification;

if ( device.platform == 'android' || device.platform == 'Android' ) {
pushNotification.register(
successHandler,
errorHandler, {
"senderID":"<xxxxx>",
"ecb":"onNotificationGCM"
}
);
}
else {
pushNotification.register(
tokenHandler,
errorHandler, {
"badge":"false",
"sound":"false",
"alert":"true",
"ecb":"onNotificationAPN"
}
);
}

// result contains any message sent from the plugin call
function successHandler (result) {
console.log('result = ' + result);
navigator.notification.alert(
result,
onConfirm,
'Title of app',
'Dismiss'
);
}

// result contains any error description text returned from the plugin call
function errorHandler (error) {
console.log('error = ' + error);
}

function tokenHandler (result) {

var uuid = device.uuid;
var platform = device.platform;
console.log(platform);
if (platform == 'iOS'){
var os = 'ios';
} else {
var os = 'android';
}
hash = result+'<title of app>';
hash = md5(hash);
var xmlHttp = null;

xmlHttp = new XMLHttpRequest();

var url = 'https://<notification server>/?token='+result+'&id='+uuid+'&hash='+hash+'&os='+os;

xmlHttp.open( "GET", url, false );

xmlHttp.send( null );
console.log(xmlHttp.responseText);
return xmlHttp.responseText;
}

// iOS
function onNotificationAPN (event) {
console.log(event);
if ( event.alert ) {
navigator.notification.alert(event.alert);
//alert(event.alert);
}

if ( event.sound ) {
var snd = new Media(event.sound);
snd.play();
}

if ( event.badge ) {
pushNotification.setApplicationIconBadgeNumber(successHandler, errorHandler, event.badge);
}

}

function receivedEvent(id) {
navigator.notification.alert(
id,
onConfirm,
'<title of app>',
'Dismiss'
);
}

function onConfirm(buttonIndex,id) {
}

最佳答案

经过更多的挖掘,设法让它工作。

这是现在的代码:

  // SET UP PUSH NOTIFICATIONS
var addCallback = function addCallback(key, callback) {
if (window.pushCallbacks === undefined) {
window.pushCallbacks = {}
}
window.pushCallbacks[key] = callback;
};


var pushNotification;
pushNotification = window.plugins.pushNotification;

if ( device.platform == 'android' || device.platform == 'Android' ) {
pushNotification.register(
successHandler,
errorHandler, {
"senderID":"<xxxxx>",
"ecb":"onNotificationGCM"
}
);
}
else {
pushNotification.register(
tokenHandler,
errorHandler, {
"badge":"true",
"sound":"true",
"alert":"true",
"ecb":"pushCallbacks.onNotificationAPN"
}
);
}

// result contains any message sent from the plugin call
function successHandler (result) {
console.log('result = ' + result);
navigator.notification.alert(
result,
onConfirm,
'<title of app>',
'Dismiss'
);
}

// result contains any error description text returned from the plugin call
function errorHandler (error) {
console.log('error = ' + error);
}

function tokenHandler (result) {

var uuid = device.uuid;
var platform = device.platform;
console.log(platform);
if (platform == 'iOS'){
var os = 'ios';
} else {
var os = 'android';
}
hash = result+'<title of app>';
hash = md5(hash);
var xmlHttp = null;

xmlHttp = new XMLHttpRequest();

var url = '<title of app>/?token='+result+'&id='+uuid+'&hash='+hash+'&os='+os;
console.log('URL IS: '+url);

xmlHttp.open( "GET", url, false );

xmlHttp.send( null );
console.log(xmlHttp.responseText);
addCallback('onNotificationAPN', onNotificationAPN);
return xmlHttp.responseText;

}

// iOS
function onNotificationAPN (event) {
if ( event.alert ) {
navigator.notification.alert(event.alert);
}

if ( event.sound ) {
var snd = new Media(event.sound);
snd.play();
}

if ( event.badge ) {
pushNotification.setApplicationIconBadgeNumber(successHandler, errorHandler, event.badge);
}

}

function receivedEvent(id) {
navigator.notification.alert(
id,
onConfirm,
'<title of app>',
'Dismiss'
);
}

function onConfirm(buttonIndex,id) {
}

所以基本上添加的是
  var addCallback = function addCallback(key, callback) {
if (window.pushCallbacks === undefined) {
window.pushCallbacks = {}
}
window.pushCallbacks[key] = callback;
};

一开始,和
"ecb":"pushCallbacks.onNotificationAPN"

注册iOS推送通知时。

现在工作。

关于cordova - 推送插件 : Can't find variable: onNotificationAPN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22134428/

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