didRegisterForRemoteNotificationsWithDeviceToken从未在特定设备上调用过

Can*_*ğlu 17 xcode push-notification apple-push-notifications ios provisioning-profile

首先,我在iPhone 6 Plus/iOS 8.1上,我已经在这里尝试了一切:为什么没有调用RegisterForRemoteNotificationsWithDeviceToken

仍然无济于事.总结一下:

  • 我已经检查了我的捆绑标识符与配置文件/开发门户中的标识符匹配
  • 我已经创建了一个新的开发推送APNS证书
  • 我已经创建了一个新的开发证书
  • 我为该开发证书创建了一个新的配置文件
  • 我已经下载了证书和配置文件,显然,双击它们进行安装
  • 我已经验证了Developer Portal上的一切正确:所有证书和配置文件都有效,推送已启用并配置了我的新APNS证书
  • 我已将我的新APNS证书上传到Parse(这一步与此无关,但无论如何)因为我正在使用Parse作为后端
  • 我确保在Xcode上使用正确的证书/配置文件对进行协同设置
  • 我已经检查了通知设置以防我的应用程序不允许接收推送,它不存在
  • 我已经尝试手动设置日期到明天,并尝试重新安装该应用程序
  • 我已从设备中删除了该应用
  • 我已从设备中删除了任何相关的配置文件
  • 我多次重启我的设备

application:didFinishLaunchingWithOptions:我打电话:

if([application respondsToSelector:@selector(registerUserNotificationSettings:)]){
        //iOS 8+
        [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
    }else{
        //pre-iOS 8
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
         (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
    }
Run Code Online (Sandbox Code Playgroud)

application:didRegisterUserNotificationSettings:我打电话:

[application registerForRemoteNotifications];
Run Code Online (Sandbox Code Playgroud)

我用断点检查了它,它被调用了.我还实现了两种方法:

application:didRegisterForRemoteNotificationsWithDeviceToken:

application:didFailToRegisterForRemoteNotificationsWithError:

但是,它们都没有被称为.不是错误,没有.控制台也没有错误.(我今天早些时候遇到了有关权利的问题,但创建了新的证书/配置文件解决了这个问题)

可能是什么问题?

更新:这是一些东西.在application:didRegisterUserNotificationSettings:我检查的通知设置,这里是我得到了什么:

(lldb) po notificationSettings
<UIUserNotificationSettings: 0x170031cc0; types: (none);>
Run Code Online (Sandbox Code Playgroud)

更新2:我再次检查了通知,发现现在我的应用程序被添加到设置中的通知中,它已启用,具有权限.但是,仍然没有调用处理程序.类型是没有的.我99%肯定这与问题有关.

更新3:我已经在另一台设备上测试过(iPod touch,iOS 8.1),我没有遇到任何问题.它立即application:didRegisterForRemoteNotificationsWithDeviceToken:使用其令牌调用该方法.问题是我的iPhone特有的.

小智 10

我遇到了类似的问题,代码已经实现并且工作正常.突然之间,经过一些调整,它就再也不起作用了.

在设备上运行的方案是:

  • didFinishLaunchingWithOptions上调用registerForRemoteNotifications.
  • didRegisterForRemoteNotificationsWithDeviceToken不会叫
  • 也没有didFailToRegisterForRemoteNotificationsWithError不会调用.

我什么都试过,几乎重置所有推配置,证书和配置配置文件等.与上一版本的所有设备进行工作,但是当我安装新版本,只是不工作了.

为了解决这个问题,我刚刚这样做了:

  1. 去了目标能力;
  2. 关闭推送通知 关闭 ;
  3. 在设备上构建运行应用程序并等待;
  4. 停止运行应用程序;
  5. 关闭推送通知 一次.
  6. 在设备上构建运行应用程序;
  7. 和魔术一样,它再次起作用

与Xcode战斗6小时后,这是我的解决方案,没有任何解释.

我希望这有助于某人.

  • 2020 年不再适用 (4认同)

Met*_*arf 0

尝试使用这个:(效果很好),抱歉堆栈的格式似乎不太好

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        NSLog(@"iOS 8 Requesting permission for push notifications..."); // iOS 8
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:
                                                UIUserNotificationTypeAlert | UIUserNotificationTypeBadge |
                                                UIUserNotificationTypeSound categories:nil];
        [UIApplication.sharedApplication registerUserNotificationSettings:settings];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    } else {
        NSLog(@"iOS 7 Registering device for push notifications..."); // iOS 7 and earlier
        [UIApplication.sharedApplication registerForRemoteNotificationTypes:
         UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge |
         UIRemoteNotificationTypeSound];
    }


    return YES;
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{

    UIApplicationState state = [application applicationState];


    if (state == UIApplicationStateActive)
    {

        NSLog(@"received a notification while active...");

    }
    else if ( application.applicationState == UIApplicationStateInactive || application.applicationState == UIApplicationStateBackground  )
    {
        //opened from a push notification when the app was on background
        NSLog(@"i received a notification...");
        NSLog(@"Message: %@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]);
        NSLog(@"whole data: %@",userInfo);
    }

}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    self.token = [[deviceToken description] stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    self.token = [self.token stringByReplacingOccurrencesOfString:@" " withString:@""];
    [[NSUserDefaults standardUserDefaults] setObject:self.token forKey:@"deviceToken"];
    [[NSUserDefaults standardUserDefaults] synchronize];

    NSLog(@"-->> TOKEN:%@",self.token);
}
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{
    NSLog(@"Registering device for push notifications..."); // iOS 8
    [application registerForRemoteNotifications];
}
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier
forRemoteNotification:(NSDictionary *)notification completionHandler:(void(^)())completionHandler
{
    NSLog(@"Received push notification: %@, identifier: %@", notification, identifier); // iOS 8 s
    completionHandler();
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
    // Respond to any push notification registration errors here.
    NSLog(@"Failed to get token, error: %@", error);
}
Run Code Online (Sandbox Code Playgroud)