我尝试使用Worklight为Android和iOS设置推送通知。
到目前为止,我已设法设置Google GCM(订阅推送似乎成功),我使用IBM Worklight Getting Started page中的示例作为主要参考。
在适配器中,sendNotification
方法需要userId
,但我不知道如何检索它。在示例中,它作为参数传递给jar,但对我而言,这似乎不是一个可行的解决方案,因为我需要一个包含多个用户的真实应用程序。
我了解有关Stack Overflow的讨论:
但是他们仍然无法回答我的疑虑......在我尝试在客户端上调用WL.Client.getUserName()
的多次尝试之一中,它会返回null
。
据我所知,这与Worklight的安全性(和领域)设置有关,但我怀疑我并没有真正理解用户ID的概念。鉴于我对移动开发真的很陌生(因此很多概念对我来说都是新的,我可能会说错了),我的怀疑是:
我的authenticationConfig.xml如下所示:
<staticResources>
<resource id="subscribeServlet" securityTest="SubscribeServlet">
<urlPatterns>/subscribeSMS*;/receiveSMS*;/ussd*</urlPatterns>
</resource>
</staticResources>
<securityTests>
<!-- Added for pushing -->
<mobileSecurityTest name="PushApplication-strong-mobile-securityTest">
<testUser realm="PushAppRealm"/>
<testDeviceId provisioningType="none"/>
</mobileSecurityTest>
<customSecurityTest name="SubscribeServlet">
<test realm="SubscribeServlet" isInternalUserID="true"/>
</customSecurityTest>
</securityTests>
<realms>
<realm name="SampleAppRealm" loginModule="StrongDummy">
<className>com.worklight.core.auth.ext.FormBasedAuthenticator</className>
</realm>
<realm name="SubscribeServlet" loginModule="rejectAll">
<className>com.worklight.core.auth.ext.HeaderAuthenticator</className>
</realm>
<!-- Added for pushing -->
<realm loginModule="PushAppLoginModule" name="PushAppRealm">
<className>com.worklight.core.auth.ext.FormBasedAuthenticator</className>
</realm>
</realms>
<loginModules>
<!-- Added for pushing -->
<loginModule name="PushAppLoginModule">
<className>com.worklight.core.auth.ext.NonValidatingLoginModule</className>
</loginModule>
<loginModule name="StrongDummy">
<className>com.worklight.core.auth.ext.NonValidatingLoginModule</className>
</loginModule>
<loginModule name="requireLogin">
<className>com.worklight.core.auth.ext.SingleIdentityLoginModule</className>
</loginModule>
<loginModule name="rejectAll">
<className>com.worklight.core.auth.ext.RejectingLoginModule</className>
</loginModule>
</loginModules>
这个是适配器(从不调用deviceSubscribeFunc,我希望相反):
WL.Server.createEventSource({
name: 'PushEventSource',
onDeviceSubscribe: 'deviceSubscribeFunc',
onDeviceUnsubscribe: 'deviceUnsubscribeFunc',
securityTest:'PushApplication-strong-mobile-securityTest'
});
// NEVER CALLED!
function deviceSubscribeFunc(userSubscription, deviceSubscription){
WL.Logger.error(">> deviceSubscribeFunc"); // error is shown on the console, debug not
WL.Logger.debug(userSubscription);
WL.Logger.debug(deviceSubscription);
}
function deviceUnsubscribeFunc(userSubscription, deviceSubscription){
WL.Logger.error(">> deviceUnsubscribeFunc"); // error is shown on the console
WL.Logger.debug(userSubscription);
WL.Logger.debug(deviceSubscription);
}
function testCall(message) {
WL.Logger.error("Client says: " + message); // error is shown on the console, debug not
return { response : "hello client!" };
}
function submitNotification(userId, notificationText){
var userSubscription = WL.Server.getUserNotificationSubscription('NotificationManager.PushEventSource', userId);
if (userSubscription==null)
return { result: "No subscription found for user :: " + userId };
var badgeDigit = 1;
var notification = WL.Server.createDefaultNotification(notificationText, badgeDigit, {custom:"data"});
WL.Logger.debug("submitNotification >> userId :: " + userId + ", text :: " + notificationText);
WL.Server.notifyAllDevices(userSubscription, notification);
return {
result: "Notification sent to user :: " + userId
};
}
同时客户端订阅以这种方式推送(toUI是一种将字符串放入UI中的新div的简单方法):
var pushNotificationReceived = function(props, payload) {
toUI("pushNotificationReceived invoked");
toUI("props :: " + JSON.stringify(props));
toUI("payload :: " + JSON.stringify(payload));
}
if(WL.Client.Push) {
var isSubscribed = WL.Client.Push.isSubscribed('myPush');
toUI("User is " + (isSubscribed? "" : "<u>not</u>")+ " subscribed.", confStyle);
WL.Client.Push.onReadyToSubscribe = function() {
toUI("Ready to subscribe, subscribing...", confStyle);
WL.Client.Push.registerEventSourceCallback("myPush", "NotificationManager", "PushEventSource", pushNotificationReceived);
}
} else
toUI("Push not available.", errStyle);
答案 0 :(得分:0)
UserId应由触发推送通知的实体提供,例如你的后端。您如何验证您的用户?你有某种用户存储库吗?这是您保留userIds的地方。某个事件发生后,您的后端决定发送推送通知。想象一下银行应用程序。 User123已登录。在登录期间,WL服务器联系后端,后端验证User123存在于用户存储库中并提供密码匹配。登录User123后,订阅推送通知。那个星期晚些时候,一个使用User123的真人去了ATM并提取了10美元。此事件由Banking后端处理。银行后端看到提取的帐户与移动用户123相关联。因此,它告诉WL服务器向User123发送推送通知。
请注意,您不必将推送通知基于用户身份。您还可以使用基于标签的通知,其中用户订阅特定标签,您可以向订阅该标签的所有用户发送通知(http://www-01.ibm.com/support/knowledgecenter/SSZH4A_6.2.0/com.ibm.worklight.dev.doc/devref/t_tag-based_notifications_setting_up.html)