方法未被调用: - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken

时间:2015-03-31 21:58:18

标签: ios iphone push-notification apple-push-notifications

永远坚持这个,但无法调用以下方法。我可以让手机要求许可,但之后就会卡住。

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
    NSLog(@"My token is: %@", deviceToken);
}

- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
    NSLog(@"Failed to get token, error: %@", error);
}

我尝试了很多事情,包括:

1)以下stackoverflow帖子中提供的所有内容:why didRegisterForRemoteNotificationsWithDeviceToken is not called

2)查看Apple的技术说明:https://developer.apple.com/library/ios/technotes/tn2265/_index.html

3)整个教程:http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1(希望我的证书都很好)

4)(是的,我有互联网连接)

有没有人有任何可能的解决方案?在过去的2-3天里我一直在绞尽脑汁,并且已经出厂两次重置我的手机,以及更改手机上的日期N ^ e次数以便让通知弹出显示为一遍又一遍地测试。

会喜欢任何帮助!谢谢!

以下是我用来调用...(尝试过其他一些版本):

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //other stuff not related

    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
    #ifdef __IPHONE_8_0
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert) categories:nil];
        [application registerUserNotificationSettings:settings];
        [application registerForRemoteNotifications];
    #endif
    } else {
        UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
        [application registerForRemoteNotificationTypes:myTypes];
    }

    return YES;
}

1 个答案:

答案 0 :(得分:0)

iOS 8引入了注册流程的变更,需要在获得用户许可后明确要求注册远程通知。

您正在调用此代码吗?

[[UIApplication sharedApplication] registerUserNotificationSettings: settings];

在你的应用程序代表中实现这个?

- (void) application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
    [application registerForRemoteNotifications];
}

也就是说,旧的API现已弃用,但如果您计划支持iOS 7,则仍然需要这些API。要支持两者,您可以检查UIApplication是否响应新的选择器:

if ([[UIApplication sharedApplication] respondsToSelector:@SEL(registerUserNotificationSettings:)]) {
    // iOS 8
    [[UIApplication sharedApplication] registerUserNotificationSettings: settings];

    // [[UIApplication sharedApplication] registerForRemoteNotifications] 
    // will be called in your UIApplicationDelegate callback
} else {
    // iOS 7
    [[UIApplication sharedApplication] registerForRemoteNotifications];
}