【发布时间】:2023-03-04 04:54:01
【问题描述】:
我正在尝试从函数“onNotification(e, $scope, currentUser)”内部调用服务但是每次我尝试记录它时,它都会返回错误:
processMessage 失败:错误:TypeError:无法设置未定义的属性“当前”
或者如果我使用“var userRegistration = this.currentUser”,我会在日志中得到“未定义”,但是我知道服务正在更新,因为当我在函数外部记录结果时它返回正确的值。
这是我的代码:
function onNotification(e, $scope, currentUser) {
var Currentuser = this.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>");
// Your GCM push server needs to know the regID before it can push to this device
// here is where you might want to send it the regID for later use.
console.log(Currentuser);
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>');
// on Android soundname is outside the payload.
// On Amazon FireOS all custom attributes are contained within payload
var soundfile = e.soundname || e.payload.sound;
// if the notification contains a soundname, play it.
var my_media = new Media("/android_asset/www/"+ soundfile);
my_media.play();
}
else
{ // otherwise we were launched because the user touched a 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>');
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;
}
}
这是同一个 js 文件中的服务:
App.service('currentUser', function ()
{
return{};
});
我应该如何在这个函数中调用服务?我的 Angular 知识有限。任何帮助将不胜感激。
更新:响应用户 PSL。 onNotification 在这里被调用:
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);
}
});
【问题讨论】:
-
什么是
onNotification?它是控制器还是什么?你是如何在那里注入一些东西的,例如你如何通过$scope? -
onNotification 是一个位于任何控制器之外的函数(如果我将它放在控制器中它就不起作用)。并且我通过将服务名称和 $scope 放在函数名称“onNotification(e,$scope,currentUser)”之后的括号中来通过正常方式(在其他地方工作)注入它
-
你能显示你打电话给
onNotification的地方吗?e是什么?那是服务名称吗?您必须传入 $scope 权限 -
我不知道这个插件是什么.. 但是尝试发送
currentUser作为键值对,就像发送badgesound等一样。 -
PushPlugin 中的插件,用于向苹果和安卓设备发送推送通知。我已经尝试了您建议的方法,但无济于事(仍然未定义)。以某种方式提取 e.regid (我需要的另一个值)并尝试将其推送到一个数组中以及任何想法我将如何去做是一个想法吗?
标签: javascript angularjs