2

Can worklight return the device token for Android/iPhone/BB and if so how?

More specifically, I'm not looking for the "device id" but the native device token.

Worklight can return the "device id", but this is different than the device token. For example Worklight: How to get current device ID for Push subscription states how to get the "device id" using the call

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

Unfortunately this returns something different than the device token. When using the native iPhone call like so and comparing it to the WL deviceid it's obvious they are different.

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

}

Moreover, the native device token is needed for a third party notification platform which is outside of worklight and so using worklights messaging system isn't feasible.

Community
  • 1
  • 1
Anton Codes
  • 3,663
  • 1
  • 19
  • 28

1 Answers1

0

You're correct APNs device token and Worklight deviceId is two different things. In case you require APNs device token for using some 3rd party notification platform you can override the didRegisterForRemoteNotificationsWithDeviceToken method in your application delegate thus receiving full control over the device token once it arrives from APNs

Anton
  • 3,166
  • 1
  • 13
  • 12
  • The question is can worklight return the device token for Android, iPhone, and BB and if so how? You answer implies that worklight cannot return the device token and that it has to be implemented separately per platform. – Anton Codes Sep 10 '14 at 21:56
  • 1
    If you're using WL's push notification engine you don't need to worry about access tokens since the logic of obtaining it and reporting to the WL server is encapsulated inside of WL runtime. Therefore WL doesn't provide any API for accessing device token. If you're not using WL's push notification engine - you need to manually obtain access token per platform. – Anton Sep 11 '14 at 04:08