didRegisterForRemoteNotificationsWithDeviceToken未在ios8中调用,但是已经注册了…

我跟着 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中将此确切标识符用作“捆绑标识符”>也许您在初次安装后拒绝了推送功能 – 在这种情况下,您将永远不会再看到应用内推送警报,并且必须再次启用“设置”应用.>尝试另一个设备.

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...