设备令牌推送通知

时间:2012-01-21 02:56:10

标签: xcode device token

如何获取设备令牌? 我想添加它,我似乎无法理解。 我需要做什么? 我要去哪? 我需要什么才能得到这个?

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

3 个答案:

答案 0 :(得分:5)

- (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: @""];



}

答案 1 :(得分:1)

当您的应用启动时,您会致电registerForRemoteNotificationTypes:,然后系统会通过application:didRegisterForRemoteNotificationsWithDeviceToken:方法(您必须执行此方法)回调您的应用。 newDeviceToken变量将具有设备令牌。

请参阅UIApplication课程的文档。

答案 2 :(得分:1)

你需要在app delegate中调用它。

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

确保app delegate具有此功能

- (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: @""];

}