Push notification Worklight:Android上的错误推送通知
请帮帮我!我已经尝试了一个星期来修复我的推送通知演示,但仍然不起作用!退出应用程序后,设备无法接收消息 这是application-descriptor.xml文件中的android元素(带有浏览器键): 这是AdapterAuth-impl.js文件:Push notification Worklight:Android上的错误推送通知,push-notification,ibm-mobilefirst,worklight-adapters,Push Notification,Ibm Mobilefirst,Worklight Adapters,请帮帮我!我已经尝试了一个星期来修复我的推送通知演示,但仍然不起作用!退出应用程序后,设备无法接收消息 这是application-descriptor.xml文件中的android元素(带有浏览器键): 这是AdapterAuth-impl.js文件: function getUser() { return {Data:[{username:'hcv',password:"123456",bagde:'3',message:'hi, Jane'},{username:'vi',pass
function getUser() {
return {Data:[{username:'hcv',password:"123456",bagde:'3',message:'hi, Jane'},{username:'vi',password:'123',bagde:'3',message:'hi, Luffy'}]};
}
function onAuthRequired(headers,errorMessage){
errorMessage = errorMessage ? errorMessage:null;
return{
authRequired:true,
errorMessage:errorMessage
};
}
function submitAuth(username,password) {
var response = {Data:[{username:'hcv',password:"123456",bagde:'3',message:'hi, Jane'},{username:'vi',password:'123',bagde:'3',message:'hi, Luffy'}]};
for (var i=0;i<response.Data.length;i++){
if(username === response.Data[i].username && password === response.Data[i].password){
var Identity={
userId: username,
displayName: username,
attributes:{foo:"bar"}
};
WL.Server.setActiveUser("AuthLoginRealm", Identity);
return{
authRequired: false
};
}
}
return onAuthRequired(null,"Invalid login credentials");
}
function onLogout(){
WL.Server.setActiveUser("AuthLoginRealm", null);
WL.Logger.debug("Logged out");
}
WL.Server.createEventSource({
name:"pushEventSource",
poll:{
interval:60,
onPoll:"submitNotifications"
},
securityTest:"mobile-securityTest",
});
function submitNotifications() {
var response = {Data:[{username:'hcv',password:"123456",bagde:"3",message:"hi,Jane"},{username:"vi",password:"123",bagde:"1",message:"hi, Luffy"}]};
var messages = response.Data;
for(var i=0; i < messages.length;i++){
var userId = WL.Server.getActiveUser("AuthLoginRealm");
if(messages[i].username === userId){
var userSubscription = WL.Server.getUserNotificationSubscription("AdapterAuth.pushEventSource", messages[i].username);
if(userSubscription === null){
return {result:"no user was found:"+messages[i].username};
}
var notification = WL.Server.createDefaultNotification(messages[i].message, messages[i].bagde,{foo:'bar'});
WL.Logger.debug("submitNotification >> userId :: " + messages[i].username + ", text :: " + messages[i].message);
WL.Server.notifyAllDevices(userSubscription, notification);
WL.Logger.error("UserId"+messages[i].username+"not found");
}
}
}
函数getUser(){
返回{数据:[{用户名:'hcv',密码:'123456',bagde:'3',消息:'hi,Jane'},{用户名:'vi',密码:'123',bagde:'3',消息:'hi,Luffy'}];
}
函数onAuthRequired(标题、错误消息){
errorMessage=errorMessage?errorMessage:null;
返回{
authRequired:true,
errorMessage:errorMessage
};
}
函数submituth(用户名、密码){
var response={数据:[{用户名:'hcv',密码:'123456',bagde:'3',消息:'hi,Jane'},{用户名:'vi',密码:'123',bagde:'3',消息:'hi,Luffy'}];
对于(var i=0;i我已经测试了你的应用程序。我成功地在所有3种情况下接收推送通知:应用程序在前台、应用程序在后台以及应用程序关闭 我有两点意见:
var response={Data:[{用户名:'hcv',密码:“123456”,bagde:“3”,消息:“你好,简”},{用户名:“vi”,密码:“123”,bagde:“1”,消息:“你好,朱利安”};
此处,徽章不应使用双引号。请删除引号submitNotifications
功能…我看到您正在构建其中的所有内容—通知文本和要发送到的用户,但这不是它应该是什么样子(!)
此函数应接收以下内容:提交通知(userId,
通知文本)
所以我不知道你是如何发送推送通知的,我觉得这是错误的。
不管怎样,为了让它发挥作用,我必须改变
if(messages[i].username==userId){…
致:
if(true | | messages[i].username===userId){…
我强烈建议您重新阅读推送通知培训模块。您是否尝试了推送通知示例项目?是的,我尝试了…没关系。我刚刚添加了错误,它发生在我退出应用程序后…该错误也发生在示例中(即使在应用程序关闭时,该示例也可以工作并接收通知).这不是真正的问题.那么,我的问题是什么?
$('#isSupportbtn').bind("click", isSupport);
$('#isSubscibedbtn').bind("click",isSubscribed);
$('#subscribebtn').bind("click",doSubscribe);
$('#unSubscribebtn').bind("click",doUnSubscribe);
function isSupport(){
var isSupport = false;
if(WL.Client.Push){
isSupport = WL.Client.Push.isPushSupported();
}
alert(isSupport);
}
function isSubscribed(){
var isSubscribed = false;
if(WL.Client.Push){
isSubscribed = WL.Client.Push.isSubscribed('myPush');
}
alert(isSubscribed);
}
if(WL.Client.Push){
WL.Client.Push.onReadyToSubscribe = function(){
$('#subscribebtn').removeAttr('disabled');
$('#unSubscribebtn').removeAttr('disabled');
WL.Client.Push.registerEventSourceCallback(
"myPush",
"AdapterAuth",
"pushEventSource",
pushNotificationReceived
);
};
}
function doSubscribe(){
WL.Client.Push.subscribe("myPush",{
onSuccess:function(){
alert("Success");
},
onFailure:function(){
alert("Fail");
}
});
}
function doUnSubscribe(){
WL.Client.Push.unsubscribe("myPush",{
onSuccess:function(){
alert("Success");
},
onFailure:function(){
alert("Fail");
}
});
}
function pushNotificationReceived(props, payloads){
alert("pushNotificationReceived invoked");
alert("props :: " + JSON.stringify(props));
alert("payload :: " + JSON.stringify(payload));
}
function getUser() {
return {Data:[{username:'hcv',password:"123456",bagde:'3',message:'hi, Jane'},{username:'vi',password:'123',bagde:'3',message:'hi, Luffy'}]};
}
function onAuthRequired(headers,errorMessage){
errorMessage = errorMessage ? errorMessage:null;
return{
authRequired:true,
errorMessage:errorMessage
};
}
function submitAuth(username,password) {
var response = {Data:[{username:'hcv',password:"123456",bagde:'3',message:'hi, Jane'},{username:'vi',password:'123',bagde:'3',message:'hi, Luffy'}]};
for (var i=0;i<response.Data.length;i++){
if(username === response.Data[i].username && password === response.Data[i].password){
var Identity={
userId: username,
displayName: username,
attributes:{foo:"bar"}
};
WL.Server.setActiveUser("AuthLoginRealm", Identity);
return{
authRequired: false
};
}
}
return onAuthRequired(null,"Invalid login credentials");
}
function onLogout(){
WL.Server.setActiveUser("AuthLoginRealm", null);
WL.Logger.debug("Logged out");
}
WL.Server.createEventSource({
name:"pushEventSource",
poll:{
interval:60,
onPoll:"submitNotifications"
},
securityTest:"mobile-securityTest",
});
function submitNotifications() {
var response = {Data:[{username:'hcv',password:"123456",bagde:"3",message:"hi,Jane"},{username:"vi",password:"123",bagde:"1",message:"hi, Luffy"}]};
var messages = response.Data;
for(var i=0; i < messages.length;i++){
var userId = WL.Server.getActiveUser("AuthLoginRealm");
if(messages[i].username === userId){
var userSubscription = WL.Server.getUserNotificationSubscription("AdapterAuth.pushEventSource", messages[i].username);
if(userSubscription === null){
return {result:"no user was found:"+messages[i].username};
}
var notification = WL.Server.createDefaultNotification(messages[i].message, messages[i].bagde,{foo:'bar'});
WL.Logger.debug("submitNotification >> userId :: " + messages[i].username + ", text :: " + messages[i].message);
WL.Server.notifyAllDevices(userSubscription, notification);
WL.Logger.error("UserId"+messages[i].username+"not found");
}
}
}
06-24 09:27:11.927: E/ActivityThread(3096): Activity com.MyApp.MyApp has leaked IntentReceiver com.google.android.gcm.GCMBroadcastReceiver@41c85190 that was originally registered here. Are you missing a call to unregisterReceiver()?
06-24 09:27:11.927: E/ActivityThread(3096): android.app.IntentReceiverLeaked: Activity com.MyApp.MyApp has leaked IntentReceiver com.google.android.gcm.GCMBroadcastReceiver@41c85190 that was originally registered here. Are you missing a call to unregisterReceiver()?
06-24 09:27:11.927: E/ActivityThread(3096): at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:763)
06-24 09:27:11.927: E/ActivityThread(3096): at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:567)
06-24 09:27:11.927: E/ActivityThread(3096): at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1055)
06-24 09:27:11.927: E/ActivityThread(3096): at android.app.ContextImpl.registerReceiver(ContextImpl.java:1042)
06-24 09:27:11.927: E/ActivityThread(3096): at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:348)
06-24 09:27:11.927: E/ActivityThread(3096): at com.google.android.gcm.GCMRegistrar.setRetryBroadcastReceiver(GCMRegistrar.java:293)
06-24 09:27:11.927: E/ActivityThread(3096): at com.google.android.gcm.GCMRegistrar.register(GCMRegistrar.java:215)
06-24 09:27:11.927: E/ActivityThread(3096): at com.worklight.androidgap.plugin.Push.subscribe(Push.java:331)
06-24 09:27:11.927: E/ActivityThread(3096): at com.worklight.androidgap.plugin.Push.access$400(Push.java:45)
06-24 09:27:11.927: E/ActivityThread(3096): at com.worklight.androidgap.plugin.Push$2.run(Push.java:233)
06-24 09:27:11.927: E/ActivityThread(3096): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
06-24 09:27:11.927: E/ActivityThread(3096): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
06-24 09:27:11.927: E/ActivityThread(3096): at java.lang.Thread.run(Thread.java:856)