Worklight:获取设备令牌

时间:2014-09-09 17:52:19

标签: ios iphone ibm-mobilefirst devicetoken

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设备进行比较时,显然它们是不同的。

- (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

}

此外,第三方通知平台需要使用本机设备令牌,因为使用工作灯消息系统是不可行的。

1 个答案:

答案 0 :(得分:0)

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