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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28128490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 04:29:30  来源:igfitidea点击:

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

iosobjective-capple-push-notificationsios13

提问by Paul

I followed this thread, but the method didRegisterForRemoteNotificationsWithDeviceTokenis still not called :

我关注了这个线程,但didRegisterForRemoteNotificationsWithDeviceToken仍然没有调用该方法:

the documentation says :

文档说:

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

调用 UIApplication 对象的 registerForRemoteNotifications 方法后,应用在设备注册成功时调用该方法

didRegisterUserappears well, but not did register notif.

didRegisterUser看起来不错,但不是did register notif

Here is my code in the AppDelegate (the app version is 8.1) :

这是我在 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
}

I also have background mode -> remote notification in the info.plist.

我也有后台模式-> info.plist 中的远程通知。

采纳答案by Sven Driemecker

Your code seems to be correct. As a minor improvement you could write your didRegisterUserNotificationSettings method like so:

您的代码似乎是正确的。作为一个小的改进,你可以像这样编写你的 didRegisterUserNotificationSettings 方法:

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
    if (notificationSettings.types != UIUserNotificationTypeNone) {
        NSLog(@"didRegisterUser");
        [application registerForRemoteNotifications];
    }
}

There may be a configuration problem that causes a failed APN registration.

可能是配置问题导致APN注册失败。

  1. Assure that your Provisioning Profile contains the aps-environment entry

  2. Assure that you have a unique App Identifier (a string without any "*") set in your Provisioning Profile. You should also use this exact identifier as "Bundle identifier" in your Info.plist

  3. Maybe you have declined the Push-Feature after the initial installation - in this case you will never see the in-app-push-alert again and have to enable Push in the settings app again.

  4. Try another device.

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

  2. 确保您在配置文件中设置了唯一的应用程序标识符(一个没有任何“*”的字符串)。您还应该在 Info.plist 中将此确切标识符用作“捆绑标识符”

  3. 也许您在初始安装后拒绝了推送功能 - 在这种情况下,您将永远不会再看到 in-app-push-alert 并且必须再次在设置应用程序中启用推送。

  4. 尝试其他设备。

回答by Kunal Gupta

After a long dig I found that on 19 July, 2016due to some error or updation at Apple's end , the didRegisterForRemoteNotificationsWithDeviceToken method would not be called even if every condition like Internet connection , Device and the methods used are perfect.

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

Refer to this link for confirmation https://forums.developer.apple.com/thread/52224

请参阅此链接进行确认https://forums.developer.apple.com/thread/52224

To verify please have a look in your other apps too. I had wasted my several hours but hope it helps someone. Thanks.

要进行验证,请查看您的其他应用。我浪费了几个小时,但希望它可以帮助某人。谢谢。

回答by EI Captain v2.0

For 19 July, 2016:-

2016 年 7 月 19 日:-

As per Apple Developer form, there is an issue regarding Sandbox APNSdown... so there may be issue from apple side,thats why delegates like application:didRegisterForRemoteNotificationsWithDeviceToken:and application:didFailToRegisterForRemoteNotificationsWithError:is not called..

根据Apple Developer form,有一个关于Sandbox APNSdown的问题......所以可能有来自Apple方面的问题,这就是为什么代表喜欢application:didRegisterForRemoteNotificationsWithDeviceToken:并且application:didFailToRegisterForRemoteNotificationsWithError:不被称为......

To check current status of APNS Sandbox this link... By now according to status APNS Sandbox is working fine and its normal ... so may be there is some other bug from apple side

要检查 APNS 沙盒的当前状态,请访问此链接...到目前为止,根据状态 APNS 沙盒工作正常且正常...所以苹果方面可能还有其他一些错误

So just don't worry if your methods are perfect and certificates are valid. This is just an issue from Apple side .. as soon as issue solved, your methods works perfectly(if everything is fine from your side).

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

Note that Production is working fine .. so issue is regarding only Sandbox APNS.

请注意,生产工作正常......所以问题仅与沙盒 APNS 相关。

回答by Homer Wang

I had this issue and finally found the note in Apple Developer web site and solved this issue:

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

Registering, Scheduling, and Handling User Notifications

注册、调度和处理用户通知

iOS Note in the section: "Registering for Remote 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.

iOS 注意:如果蜂窝或 Wi-Fi 连接不可用,则 application:didRegisterForRemoteNotificationsWithDeviceToken: 方法和 application:didFailToRegisterForRemoteNotificationsWithError: 方法都不会被调用。对于 Wi-Fi 连接,当设备无法通过端口 5223 与 APNs 连接时,有时会发生这种情况。如果发生这种情况,用户可以移动到另一个未阻止此端口的 Wi-Fi 网络,或者在 iPhone 或 iPad 上等待直到蜂窝数据服务可用。在任何一种情况下,设备都应该能够建立连接,然后调用一个委托方法。

My iPhone only connected with Wifi, reboot iPhone and reconnect to WiFi AP solved this issue.

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

回答by Naishta

XCode 8.3.1 onwards

XCode 8.3.1 起

After trying out all the above options, if the delegate method is still not called, make sure in the project,

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

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

回答by Amal T S

For IOS 13,

对于IOS 13

The didRegisterForRemoteNotificationsWithDeviceTokenwas not triggering at all when I connected to my wifi network. I tried a lot to fix this, but couldn't. At last I Changed my network from wifi to cellular data & suddenly it started working again. I even changed back to the old wifi network and it works with no issue.

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

Also, if you used your internet connection in the MAC to share using USB. Turn it off & connect your IPhone with a normal wifi or mobile data.

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

回答by Alexander Atamanov

Ok, guys, I've just spent 11 straight hours debugging this thing so hopefully, this answer will help somebody.

好的,伙计们,我刚刚花了 11 个小时来调试这个东西,所以希望这个答案会对某人有所帮助。

If you got EVERYTHING said in similar threads set up correctly and have got Google Firebase analytics integrated, and didRegisterForRemoteNotificationsWithDeviceTokenmethod is still not called, this could be because it is swizzled in Google Firebase by default. For some weird reason, FIR doesn't return to the original method after it sends your token to Google. To fix it set

如果您在类似线程中正确设置了所有内容并集成了 Google Firebase 分析,并且didRegisterForRemoteNotificationsWithDeviceToken方法仍未被调用,这可能是因为它默认在 Google Firebase 中混合。出于某种奇怪的原因,FIR 在将您的令牌发送给 Google 后不会返回原始方法。修复它设置

    FirebaseAppDelegateProxyEnabled to NO (BOOL)

in your Info.plist.

在您的 Info.plist 中。

回答by rounak

In my case, this was happening because I was proxying all device traffic through Charles. Removing that fixed it, and gave me the didRegister callback.

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

回答by Ely

Before making yourself crazy:

在让自己疯狂之前:

  • First try to restart your device
  • 首先尝试重新启动您的设备

Especially after moving from production to development APNS environment or vice versa.

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

回答by Syed Zahid Shah

Weird Solution but it worked for me. After trying for hours, I just turn off and on the wifi from the device setting app and it worked.

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