【问题标题】:Worklight : get device tokenWorklight:获取设备令牌
【发布时间】:2014-09-09 17:52:18
【问题描述】:

worklight 能否返回 Android/iPhone/BB 的设备令牌?如果可以,如何返回?

更具体地说,我不是在寻找“设备 ID”,而是在寻找本机设备令牌。

Worklight 可以返回“设备 ID”,但这与设备令牌不同。例如Worklight: How to get current device ID for Push subscription 说明如何使用调用获取“设备 ID”

WL.Client.getUserInfo("wl_deviceNoProvisioningRealm", "userId");

不幸的是,这会返回与设备令牌不同的东西。当像这样使用本机 iPhone 调用并将其与 WL 设备 ID 进行比较时,很明显它们是不同的。

- (void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

    NSMutableDictionary *results = [NSMutableDictionary dictionary];
    NSString *token = [[[[deviceToken description] stringByReplacingOccurrencesOfString:@"<"withString:@""]
                        stringByReplacingOccurrencesOfString:@">" withString:@""]
                       stringByReplacingOccurrencesOfString: @" " withString: @""];
    [results setValue:token forKey:@"deviceToken"];

#if !TARGET_IPHONE_SIMULATOR
    [results setValue:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"] forKey:@"appName"];
    [results setValue:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"] forKey:@"appVersion"];


    NSUInteger rntypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];

    // Set the defaults to disabled unless we find otherwise...
    NSString *pushBadge = @"disabled";
    NSString *pushAlert = @"disabled";
    NSString *pushSound = @"disabled";

    if(rntypes & UIRemoteNotificationTypeBadge){
        pushBadge = @"enabled";
    }
    if(rntypes & UIRemoteNotificationTypeAlert) {
        pushAlert = @"enabled";
    }
    if(rntypes & UIRemoteNotificationTypeSound) {
        pushSound = @"enabled";
    }

    [results setValue:pushBadge forKey:@"pushBadge"];
    [results setValue:pushAlert forKey:@"pushAlert"];
    [results setValue:pushSound forKey:@"pushSound"];

    // Get the users Device Model, Display Name, Token & Version Number
    UIDevice *dev = [UIDevice currentDevice];
    [results setValue:dev.name forKey:@"deviceName"];
    [results setValue:dev.model forKey:@"deviceModel"];
    [results setValue:dev.systemVersion forKey:@"deviceSystemVersion"];

    [self successWithMessage:[NSString stringWithFormat:@"%@", token]];

#else
    [self successWithMessage:[NSString stringWithFormat:@"%@", @"simulator generated"]];
#endif

}

此外,worklight 之外的第三方通知平台需要本地设备令牌,因此使用 worklights 消息传递系统是不可行的。

【问题讨论】:

    标签: ios iphone ibm-mobilefirst devicetoken


    【解决方案1】:

    您是正确的 APNs 设备令牌和 Worklight deviceId 是两个不同的东西。如果您需要 APNs 设备令牌来使用某些 3rd 方通知平台,您可以覆盖应用程序委托中的 didRegisterForRemoteNotificationsWithDeviceToken 方法,从而在设备令牌从 APNs 到达时获得对设备令牌的完全控制

    【讨论】:

    • 问题是worklight 能否返回Android、iPhone 和BB 的设备令牌,如果可以,如何返回?您的回答意味着 worklight 无法返回设备令牌,并且必须按平台单独实施。
    • 如果您使用 WL 的推送通知引擎,则无需担心访问令牌,因为获取它并向 WL 服务器报告的逻辑封装在 WL 运行时内部。因此 WL 不提供任何 API 来访问设备令牌。如果您没有使用 WL 的推送通知引擎 - 您需要手动获取每个平台的访问令牌。
    猜你喜欢
    • 2014-05-07
    • 1970-01-01
    • 2012-12-15
    • 2012-10-02
    • 2014-07-31
    • 1970-01-01
    • 1970-01-01
    • 2014-08-17
    相关资源
    最近更新 更多