我正在尝试设置一个推送通知服务器,我已经让插件在我的科尔多瓦应用程序中工作,并且我正在尝试实现一些发送 ID 和应用程序注册 ID 的东西。我正在购买将用户 ID 放置在服务中,并且我试图将该值与应用程序中的 regid 一起存储在一个数组中,并通过 POST 请求将其发送到我的服务器。这是我的代码:
推.js
function onNotification(e, $scope, currentUser) {
$("#app-status-ul").append('<li>EVENT -> RECEIVED: ' + e.event + '</li>');
switch (e.event) {
case 'registered':
if (e.regid.length > 0) {
$("#app-status-ul").append('<li>REGISTERED -> REGID: ' + e.regid + '</li>');
$scope.Current = currentUser;
var userRegistration = [{
e.regid
}, {
$scope.Current.currentUser
}];
$ajax({
data: userRegistration,
type: "POST",
url: "url/device_register.php",
success: function () {
alert('Succesfully Registered with the Server.');
}
});
console.log("RegID = " + e.regid);
}
break;
case 'message':
//if this flag is set, this notification happened while we were in the foreground.
//you might want to play a sound to get the user's attention, throw up a dialog etc.
if (e.foreground) {
$("#app-status-ul").append('<li>--INLINE NOTIFICATION--' + '</li>');
//if the notification contains a soundname, play it.
// if the notification contains a soundname, play it.
var my_media = new Media("/android_asset/www/" + e.soundname);
my_media.play();
} else {
//otherwise we were launched because the user touched the notification in the notification tray.
if (e.coldstart) {
$("#app-status-ul").append('<li>--COLDSTART NOTIFICATION--' + '</li>');
} else {
$("#app-status-ul").append('<li>--BACKGROUND NOTIFICATION--' + '</li>');
}
}
$("#app-status-ul").append('<li>MESSAGE -> MSG ' + e.payload.message + '</li>');
$("#app-status-ul").append('<li>MESSAGE -> MSGCNT: ' + e.payload.msgcnt + '</li>')
$status.append('<li>MESSAGE -> TIME: ' + e.payload.timeStamp + '</li>');
break;
case 'error':
$("#app-status-ul").append('<li>ERROR -> MSG: ' + e.msg + '</li>');
break;
default:
$("#app-status-ul").append('<li>EVENT -> Unknown, an event was received and we do not know what it is</li>');
break;
}
}
每次我在我的设备上启动应用程序时,我都会收到错误消息:
processMessage 失败:错误:TypeError:无法设置未定义的属性“当前”
我不明白我哪里错了。该服务放置在另一个名为 app.js 的 .js 文件中,它在 push.js 之前和 jQuery 之后被调用。任何帮助将不胜感激。
更新:所以经过一些修补后,我发现我在从单独的 .js 文件中调用值时遇到了一些问题。同样为了回应对我的问题的评论,这里是代码的其他部分。
应用程序.js
App.controller('LogHomeCtrl', function ($scope, $log, $state, currentUser) {
var pushNotification;
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
pushNotification = window.plugins.pushNotification;
$("#app-status-ul").append('<li>registering ' + device.platform + '</li>');
if (device.platform == 'android' || device.platform == 'Android') {
pushNotification.register(
successHandler,
errorHandler, {
"senderID": "460885134680",
"ecb": "onNotification"
});
} else {
pushNotification.register(
tokenHandler,
errorHandler, {
"badge": "true",
"sound": "true",
"alert": "true",
"ecb": "onNotificationAPN"
});
}
}
function successHandler(result) {
alert('result = ' + result);
}
function errorHandler(error) {
alert('error = ' + error);
}
});
App.service('currentUser', function () {
return {};
});
我将如何将服务的价值存储到 push.js 中?