【发布时间】:2014-12-05 13:01:40
【问题描述】:
我正在尝试使用 Worklight 为 Android 和 iOS 设置推送通知。
到目前为止,我已经成功设置了 Google GCM(订阅推送似乎成功),并且我使用了 IBM Worklight Getting Started page 中的示例作为主要参考。
在适配器中,sendNotification 方法需要userId,但我不知道如何检索它。在示例中,它作为参数传递给 jar,但对我来说这似乎不是一个可行的解决方案,因为我需要一个具有多个用户的真实应用程序。
我知道关于 Stack Overflow 的讨论:
- userID in getUserNotificationSubscription WORKLIGHT Pushnotification
- Worklight: Push notification without User ID
但他们仍然没有回答我的疑问...在众多尝试之一中,我尝试在客户端上调用 WL.Client.getUserName(),但它返回 null。
据我了解,这与 Worklight 的安全(和领域)设置有关,但我怀疑我并没有真正理解用户 ID 的概念。鉴于我对移动开发真的很陌生(因此很多概念对我来说都是新的,我可能说错了),我的疑问是:
- Worklight 是否存储了不同于 android 用户 ID 的用户 ID(作为抽象)?
- 如果是,这就是有时提到安全/领域的原因吗?如何将这两个用户 ID(android/worklight)配对,或者至少与 worklight 用户合作?
- 既然这似乎是一个常见的问题(将通知推送给不同的用户),是否可能没有在线示例代码?
- 根据用户 ID,应该遵循哪一个流程/架构? EG:用户 id 需要存储到一个领域,这个领域作为关于 x 的信息并且应该是 y 类型,它将用于 z 等......(我仍然必须弄清楚如何处理领域)
我的 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);
【问题讨论】:
标签: android push-notification ibm-mobilefirst worklight-security