请帮帮我!我已经试了一个星期来修复我的推送通知演示,但它仍然无法正常工作!退出应用程序后,设备无法接收消息。
这是来自application-descriptor.xml文件的android元素(使用Browser Key):
<android version="1.0" securityTest="mobile-securityTest">
<worklightSettings include="false"/>
<pushSender key="AIzaSyBmDTJw8IQAA1MmTF9MkSQUQXYxoFBKKtk" senderId="430560385547"/>
<security>
<encryptWebResources enabled="false"/>
<testWebResourcesChecksum enabled="false" ignoreFileExtensions="png, jpg, jpeg, gif, mp4, mp3"/>
<publicSigningKey>PushNotificationMyAppAndroid</publicSigningKey>
<packageName>com.MyApp</packageName>
</security>
<compressWebResources enabled="true"/>
</android>
这是main.js文件:
$('#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));
}
这是AdapterAuth-impl.js文件:
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)
答案 0 :(得分:0)
我已经测试了你的申请。我设法在所有3种情况下收到推送通知:当应用程序在前台时,应用程序在后台以及应用程序何时关闭时。
我有2条评论:
var response = {Data:[{username:'hcv',password:"123456",bagde:"3",message:"hi,Jane"},{username:"vi",password:"123",bagde:"1",message:"hi,Julian"}]};
此处,徽章不应为双引号。删除引号。
submitNotifications
函数...我看到你正在构建其中的所有内容 - 通知文本和要发送给的用户,但这不是它应该是怎样的(! )。
这个函数应该接收这些:submitNotifications(userId,
notifivcationText)
所以我不知道你是如何发送推送通知的,我看起来不对。 无论如何,为了让它工作,我不得不改变
if( messages[i].username === userId){ ...
要:
if( true || messages[i].username === userId){ ...
我强烈建议重新阅读Push Notifications培训模块。