我跟着
this thread,但方法didRegisterForRemoteNotificationsWithDevicetoken仍然没有被调用:
文件说:
After you call the registerForRemoteNotifications method of the
UIApplication object,the app calls this method when device
registration completes successfully
didRegisterUser看起来很好,但没有注册通知.
这是AppDelegate中的代码(应用程序版本是8.1):
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//register notif
UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
UIUserNotificationTypeBadge |
UIUserNotificationTypeSound);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes categories:nil];
[application registerUserNotificationSettings:settings];
return YES;
}
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
//register to receive notifications
[application registerForRemoteNotifications];
NSLog(@"didRegisterUser");
}
-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(@"error here : %@",error);//not called
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDevicetoken:(NSData *)devicetoken {
/*
// Store the devicetoken in the current installation and save it to Parse.
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
[currentInstallation setDevicetokenFromData:devicetoken];
currentInstallation.channels = @[ @"global" ];
[currentInstallation saveInBackground];
*/
NSLog(@"did register notif");//not called
}
我也有背景模式 – > info.plist中的远程通知.
解决方法
你的代码似乎是正确的.作为一个小的改进,您可以编写您的didRegisterUserNotificationSettings方法,如下所示:
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
if (notificationSettings.types != UIUserNotificationTypeNone) {
NSLog(@"didRegisterUser");
[application registerForRemoteNotifications];
}
}
可能存在导致APN注册失败的配置问题.
>确保您的配置配置文件包含aps环境条目>确保您的配置配置文件中设置了唯一的应用标识符(不包含任何“*”的字符串).您还应该在Info.plist中将此确切标识符用作“捆绑标识符”>也许您在初次安装后拒绝了推送功能 – 在这种情况下,您将永远不会再看到应用内推送警报,并且必须再次启用“设置”应用.>尝试另一个设备.