ios8 中未调用 didRegisterForRemoteNotificationsWithDeviceToken,但 didRegister...Settings 是

didRegisterForRemoteNotificationsWithDeviceToken not called in ios8, but didRegister...Settings is

我跟着this thread,但是方法didRegisterForRemoteNotificationsWithDeviceToken还是没有被调用:

文档说:

After you call the registerForRemoteNotifications method of the UIApplication object, the app calls this method when device registration completes successfully

didRegisterUser 看起来很好,但不是 did register notif

这是我在 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 注册失败。

  1. 确保您的配置文件包含 aps-environment 条目

  2. 确保您在配置文件中设置了唯一的应用程序标识符(不带任何“*”的字符串)。您还应该在 Info.plist

  3. 中使用此确切标识符作为 "Bundle identifier"
  4. 也许您在初始安装后拒绝了推送功能 - 在这种情况下,您将永远不会再看到应用内推送警报,并且必须再次在设置应用中启用推送。

  5. 换个设备试试。

我遇到了这个问题,最后在 Apple Developer 网站上找到了说明并解决了这个问题:

Registering, Scheduling, and Handling User Notifications

iOS 部分注释:“注册远程通知:

iOS Note: If a cellular or Wi-Fi connection is not available, neither the application:didRegisterForRemoteNotificationsWithDeviceToken: method nor the application:didFailToRegisterForRemoteNotificationsWithError: method is called. For Wi-Fi connections, this sometimes occurs when the device cannot connect with APNs over port 5223. If this happens, the user can move to another Wi-Fi network that isn’t blocking this port or, on an iPhone or iPad, wait until the cellular data service becomes available. In either case, the device should be able to make the connection, and then one of the delegation methods is called.

我的 iPhone 只连接了 Wifi,重启 iPhone 并重新连接到 WiFi AP 解决了这个问题。

就我而言,在 iOS 9.3 之后我相信。如果我使用 Localytics,将不会调用 didRegisterForRemoteNotificationsWithDeviceToken。我必须从我的代码中删除它(在 ApplicationDidFinishLaunchingWithOption 中)

[Localytics autoIntegrate:@"xxxxx" launchOptions:launchOptions];

经过长时间的挖掘,我发现在 2016 年 7 月 19 日,由于 Apple 端的一些错误或更新,didRegisterForRemoteNotificationsWithDeviceToken 方法不会被调用,即使每个条件如 Internet连接,设备和使用的方法都很完美。

参考此link进行确认https://forums.developer.apple.com/thread/52224

要验证,请也查看您的其他应用。 我浪费了几个小时,但希望它能帮助别人。 谢谢

2016 年 7 月 19 日:-

根据 Apple Developer form,存在关于 Sandbox APNS 的问题。因此苹果方面可能存在问题,这就是为什么不调用 application:didRegisterForRemoteNotificationsWithDeviceToken:application:didFailToRegisterForRemoteNotificationsWithError: 等代表的原因。

查看 APNS 沙盒的当前状态this link... 到目前为止,根据状态 APNS Sandbox 工作正常并且正常。所以苹果方面可能还有其他错误

因此,如果您的方法完美且证书有效,请不要担心。这只是 Apple 方面的问题,一旦问题解决,您的方法就会完美运行(如果您方面一切正常)。

请注意,生产运行良好,问题与沙盒 APNS 有关。

请添加下面的代码,它对我有用,

 #ifdef __IPHONE_8_0
   - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
   {
   //register to receive notifications
  [application registerForRemoteNotifications];
  }

    - (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler
  {
  //handle the actions
  if ([identifier isEqualToString:@"declineAction"]){
  }
 else if ([identifier isEqualToString:@"answerAction"]){
  }
 }
 #endif

您将通过以下方法获得设备令牌,

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

详情请参考Details Answer

希望这对某些人有所帮助。

XCode 8.3.1 以后

尝试以上所有选项后,如果仍然没有调用委托方法,请确保在项目中,

under 'Capabilities', (next to General), 
 - Push Notifications option is turned ON`
- and under Background Modes, Remote Notifications is turned ON

真丢脸!! O忘记了最基本的步骤:

按照此处的所有证书流程说明进行操作后:https://www.pluralsight.com/guides/swift/creating-ios-rich-push-notifications

发送推送通知的工具是:https://github.com/noodlewerk/NWPusher

愿原力与你同在!

好的,伙计们,我刚刚花了连续 11 个小时调试这个东西,希望这个答案能帮助到别人。

如果您正确设置了类似线程中所说的一切,并且集成了 Google Firebase 分析,并且 didRegisterForRemoteNotificationsWithDeviceToken 方法仍然没有被调用,这可能是因为它在 Google Firebase 默认情况下被调配了。 出于某种奇怪的原因,FIR 在将您的令牌发送到 Google 后不会 return 到原始方法。要修复它,请设置

    FirebaseAppDelegateProxyEnabled to NO (BOOL)

在你的 Info.plist.

就我而言,发生这种情况是因为我正在通过 Charles 代理所有设备流量。删除修复它,并给了我 didRegister 回调。

对我来说,它是 运行 模拟器上的应用程序。不会为模拟器生成推送令牌,所以我输入 didFailToRegisterForRemoteNotificationsWithError 而不是 didRegisterForRemoteNotificationsWithDeviceToken

对于IOS 13,

当我连接到我的 wifi 网络时,didRegisterForRemoteNotificationsWithDeviceToken 根本没有触发。我尝试了很多来解决这个问题,但做不到。最后,我将网络从 wifi 更改为蜂窝数据,突然它又开始工作了。我什至改回旧的 wifi 网络,它没有问题。

此外,如果您在 MAC 中使用 Internet 连接来使用 USB 进行共享。将其关闭并使用普通 wifi 或移动数据连接您的 IPhone。

在让自己发疯之前:

  • 首先尝试重启您的设备

尤其是在从生产 APNS 环境转移到开发 APNS 环境之后,反之亦然。

奇怪的解决方案,但它对我有用。 尝试了几个小时后,我只是从设备设置应用程序中关闭和打开 wifi,它就起作用了。

我不确定,但只是预感。这对我来说发生过几次。我正在启动该应用程序,然后立即将其置于后台!

解决方法是将应用程序保持在前台一段时间,直到您收到回调。

我解决了这个问题以连接蜂窝模式。

我测试了两个 iPhone 连接的蜂窝网络,一个 iPhone 和一个 iPad 仅连接 Wi-Fi。

有时连接 wifi 的设备可以工作,但有时不能。连接蜂窝后效果很好。

Apple's official doc

您可能需要 SIM 卡才能激活您的 iPhone,否则您将永远不会收到回电通知。

在我的案例中,配置文件无效。重新启用配置文件解决了问题

在 Xcode12 上,您可能需要在第一次向应用程序添加推送通知时执行此操作:

  • 转到项目 -> YourTarget -> 签名和功能

在该屏幕上,点击左上角的“+ 功能”按钮(很难注意到)

功能“库”将打开:

Select 并添加推送通知:

您可能还想选中后台模式中的“远程通知”框。

最后,像其他人提到的那样,确保您的开发和分发配置文件是最新的,并在功能列表中包含“推送通知”。