didRegisterForRemoteNotificationsWithDeviceToken没有在ios8中调用,但是didRegister ...设置是

Pau*_*aul 25 objective-c apple-push-notifications ios

我按照这个线程,但didRegisterForRemoteNotificationsWithDeviceToken仍未调用该方法:

文件说:

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

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
}
Run Code Online (Sandbox Code Playgroud)

我还在info.plist中有后台模式 - >远程通知.

Kun*_*pta 28

长挖后,我发现,在2016年7月19日,由于在苹果结束一些错误或更新用的didRegisterForRemoteNotificationsWithDeviceToken方法不会被调用,即使像互联网连接,设备和使用的方法的每个条件都堪称完美.

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

要验证,请查看您的其他应用.我浪费了几个小时,但希望它有所帮助.谢谢.

  • 请耐心等待,我已经向Apple报告了错误,希望他们能尽快解决. (2认同)

EI *_*2.0 24

2016年7月19日: -

根据Apple Developer表单,有一个关于Sandbox APNSdown 的问题...所以苹果方面可能存在问题,这就是为什么代表喜欢application:didRegisterForRemoteNotificationsWithDeviceToken:application:didFailToRegisterForRemoteNotificationsWithError:不调用..

要查看APNS Sandbox 此链接的当前状态...现在根据状态APNS Sandbox工作正常且正常...所以可能是苹果方面还有其他一些错误

所以,如果您的方法完美且证书有效,请不要担心.这只是Apple方面的一个问题..一旦问题解决,你的方法就能很好地工作(如果你身边的一切都很好).

请注意,生产工作正常..所以问题仅涉及Sandbox APNS.

  • 是的,你是对的,所有的开发人员都会为此发生这样的事情:D (4认同)

Bal*_*ali 19

对于IOS 13

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

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

  • iOS 13 - 我遇到了同样的问题。由于某些未知原因,“didRegisterForRemoteNotificationsWithDeviceToken”未通过 wifi 触发,但适用于数据 (5认同)

Sve*_*ker 18

你的代码似乎是正确的.作为一个小改进,您可以编写didRegisterUserNotificationSettings方法,如下所示:

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

可能存在导致APN注册失败的配置问题.

  1. 确保您的Provisioning Profile包含aps-environment条目

  2. 确保您的Provisioning Profile中设置了唯一的App Identifier(不带任何"*"的字符串).您还应该在Info.plist中将此确切标识符用作"Bundle identifier"

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

  4. 尝试其他设备.

  • 嗨,问题是......设备.网络出现问题,我收到了所有的iMessages,有时是在发送后的五天.我再次尝试,现在它的工作原理.我不得不关闭并打开设备.非常感谢您的回答. (2认同)

Hom*_*ang 13

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

注册,安排和处理用户通知

iOS注意在"注册远程通知:

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

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

  • 如果最终用户遇到这种情况,我该如何检测并处理它?如果框架没有调用任何方法,我无法对此错误做出反应. (6认同)

小智 10

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

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

    FirebaseAppDelegateProxyEnabled to NO (BOOL)
Run Code Online (Sandbox Code Playgroud)

在您的 Info.plist 中。

  • 谢谢你!我今天刚刚设置了 Firebase Crashlytics,但我的推送通知停止工作。更改 Info.plist 中的设置并没有解决问题,但我确实找到了替代解决方案:从我的项目中删除所有与 Firebase 相关的代码,并在将来尽可能避免使用 Google 产品。 (2认同)

Nai*_*hta 8

XCode 8.3.1以后

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

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


Ely*_*Ely 8

在让自己疯狂之前:

  • 首先尝试重新启动您的设备

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


rou*_*nak 7

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


Jav*_*ría 6

鄙视我!!O忘记了最基本的步骤:

在此处输入图片说明

在遵循所有证书流程说明之后:https : //www.pluralsight.com/guides/swift/creating-ios-rich-push-notifications

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

愿原力与你同在!


Lud*_*uda 5

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