Raoul
Raoul

Reputation: 85

Device Token Push Notifications

How do I obtain the device token? I am trying to add it and I can't seem to understand. What do I need to do? Where do I go? What do I need to get this?

- (void)application:(UIApplication *)application
        didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)newDeviceToken
{
    // Tell Parse about the device token.
    [PFPush storeDeviceToken:newDeviceToken];
    // Subscribe to the global broadcast channel.
    [PFPush subscribeToChannelInBackground:@""];
}

Upvotes: 1

Views: 4264

Answers (3)

jdog
jdog

Reputation: 10759

You need to call this in app delegate.

[[UIApplication sharedApplication]
registerForRemoteNotificationTypes:
    (UIRemoteNotificationTypeBadge |
     UIRemoteNotificationTypeSound | 
     UIRemoteNotificationTypeAlert)];

Make sure app delegate has this function

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)_deviceToken {
// Get a hex string from the device token with no spaces or < >

NSString *deviceToken = [[[[_deviceToken description]
                 stringByReplacingOccurrencesOfString: @"<" withString: @""] 
                stringByReplacingOccurrencesOfString: @">" withString: @""] 
               stringByReplacingOccurrencesOfString: @" " withString: @""];

}

Upvotes: 1

Saurabh
Saurabh

Reputation: 22873

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)_deviceToken {
    // Get a hex string from the device token with no spaces or < >

    NSString *deviceToken = [[[[_deviceToken description]
                     stringByReplacingOccurrencesOfString: @"<" withString: @""] 
                    stringByReplacingOccurrencesOfString: @">" withString: @""] 
                   stringByReplacingOccurrencesOfString: @" " withString: @""];



}

Upvotes: 5

Brian Rogers
Brian Rogers

Reputation: 129657

You call registerForRemoteNotificationTypes: when your app launches, then the system calls back into your app via the application:didRegisterForRemoteNotificationsWithDeviceToken: method (which you must implement). The newDeviceToken variable will have the device token.

See the documentation for the UIApplication class.

Upvotes: 1

Related Questions