打开AppDelegate.m
怎么做:
首先:

-(BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptionsaddCodelikethis#ifdef__IPHONE_8_0//Right,thatisthepointUIUserNotificationSettings*settings=[UIUserNotificationSettingssettingsForTypes:(UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeAlert)categories:nil];[[UIApplicationsharedApplication]registerUserNotificationSettings:settings];#else//registertoreceivenotificationsUIRemoteNotificationTypemyTypes=UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeSound;[[UIApplicationsharedApplication]registerForRemoteNotificationTypes:myTypes];#endif

接着:
添加如下函数

#ifdef__IPHONE_8_0-(void)application:(UIApplication*)applicationdidRegisterUserNotificationSettings:(UIUserNotificationSettings*)notificationSettings{//registertoreceivenotifications[applicationregisterForRemoteNotifications];}-(void)application:(UIApplication*)applicationhandleActionWithIdentifier:(NSString*)identifierforRemoteNotification:(NSDictionary*)userInfocompletionHandler:(void(^)())completionHandler{//handletheactionsif([identifierisEqualToString:@"declineAction"]){}elseif([identifierisEqualToString:@"answerAction"]){}}#endif

然后你会在如下方法中获得deviceToken

-(void)application:(UIApplication*)applicationdidRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken

如果该函数没有响应, 使用如下函数并NSLogerror

-(void)application:(UIApplication*)applicationdidFailToRegisterForRemoteNotificationsWithError:(NSError*)error


希望对你有帮助!