我在Worklight测试应用中使用推送通知。我通过Worklight Adapter进行了身份验证。但是通过这种方式,我总是会收到来自PushAdapter的响应“找不到用户的订阅:: [sample_username]”
设备已通过身份验证:
我的测试Android设备订阅了通知。
我简单的AuthAdapter:
function onAuthRequired(headers, errorMessage) {
errorMessage = errorMessage ? errorMessage : null;
return {
authRequired: true,
errorMessage: errorMessage
};
}
function submitAuthentication(username, password) {
var userIdentity = {
userId: username,
displayName: username,
attributes: {
foo: "bar"
}
};
WL.Server.setActiveUser("AdapterAuthRealm", null);
WL.Server.setActiveUser("AdapterAuthRealm", userIdentity);
return {
authRequired: false
};
}
也许,我的推送通知服务代码对帮助很有用(Angular JS):
console.log(services.service('PushService'));
if (WL.Client.Push){
WL.Client.Push.onReadyToSubscribe = function(){
WL.Client.Push.registerEventSourceCallback(
"myPush",
"PushAdapter",
"PushEventSource",
angular.element(document.body).injector().get('PushService').pushNotificationReceived);
};
}
services.service('PushService', function($q) {
var self = this;
self.send = function(userID, text) {
console.info("PushService:: send ",userID, text);
var deferred = $q.defer();
if (typeof WL !== 'undefined') {
WL.Client.invokeProcedure({
adapter : 'PushAdapter',
procedure : 'submitNotification',
parameters : [userID, text]
},{
onSuccess: function(response) {
deferred.resolve(response);
console.log("PushService:: push response: ",response);
},
onFailure: function(response) {
deferred.reject();
}
});
} else {
deferred.reject();
console.warn("WL is undefined");
}
return deferred.promise;
};
self.isSupported = function() {
var isSupported = false;
if (WL.Client.Push){
console.log(WL.Client.Push);
isSupported = WL.Client.Push.isPushSupported();
}
return isSupported;
};
self.isSubscribed = function() {
var isSubscribed = false;
if (WL.Client.Push){
isSubscribed = WL.Client.Push.isSubscribed('myPush');
}
return isSubscribed;
};
self.subscribe = function() {
var deferred = $q.defer();
WL.Client.Push.subscribe("myPush", {
onSuccess: function(response) { console.info("Push:: subscribe success"); deferred.resolve(response); },
onFailure: function() { console.warn("Push:: subscribe failture"); deferred.reject(); }
});
return deferred.promise;
};
self.unsubscribe = function() {
var deferred = $q.defer();
WL.Client.Push.unsubscribe("myPush", {
onSuccess: function(response) { console.info("Push:: unsubscribe success"); deferred.resolve(response); },
onFailure: function() { console.warn("Push:: unsubscribe failture"); deferred.reject(); }
});
return deferred.promise;
};
self.pushNotificationReceived = function(props, payload) {
console.info("pushNotificationReceived invoked:: ", JSON.stringify(props), JSON.stringify(payload));
};
return self;
});
答案 0 :(得分:0)
您的问题的一个可能方案是您尝试向用户发送会话已结束的推送。 例如,如果您有以下情形:
“conf \ server \ worklight.properties”中的和“apps \”“\ common \ js \ _ initOptions.js”:
// # How often heartbeat request will be sent to Worklight Server
heartBeatIntervalInSecs: -1,
移动应用程序不会发送心跳,因此在您的服务器超时(会话结束)之后就像您在服务器端注销了应用程序一样。所以,你取消订阅。这可能是推送通知失败的原因。
可能的解决方案是启用心跳或增加服务器端会话超时。
如果您还可以共享配置文件,那将会很有帮助:conf \ server \ worklight.properties和“apps \\ common \ js \ _ initsptions.js”以检查是否是这种情况。无论如何,如果是这种情况,第一次PUSH将在登录后持续3分钟然后您将开始得到错误。