IOS 8 注册远程通知只成功了一半

     2023-02-24     77

关键词:

【中文标题】IOS 8 注册远程通知只成功了一半【英文标题】:IOS 8 Registering for remote notification only half-works 【发布时间】:2014-11-11 20:01:36 【问题描述】:

编辑

我将把它放在顶部,因为这似乎可能是问题所在。注册用户设置后,我 NSLog:

NSLog(@"current notifications : %@", [application currentUserNotificationSettings]);

我收到了:

current notifications : <UIUserNotificationSettings: 0x165844a0; types: (none);>

但如您所见,我显然正在注册使用徽章、声音和警报的设置。

另外,我想我应该提到我已经关闭了通知 我的设置。但是,我希望它会调用 didFailToRegisterForRemoteNotificationsWithError 在这种情况下 是我生成自己的电话 ID 的地方。

-- 我收回我上面说的话。当我删除该应用程序并重新安装它时,它会重新打开允许通知。 --


我知道这个问题在这里很多,但我似乎无法让它发挥作用。这是我的简化代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

    // Override point for customization after application launch.
    [self registerForNotifications:application];


- (void)registerForNotifications:(UIApplication *)application 
    // Let the device know we want to receive push notifications
    NSLog(@"Registering for Remote Notifications");

    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) 
        #ifdef __IPHONE_8_0
            UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge
                                                                                                 |UIRemoteNotificationTypeSound
                                                                                                 |UIRemoteNotificationTypeAlert) categories:nil];
            [application registerUserNotificationSettings:settings];
        #endif
     else 
        UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
        [application registerForRemoteNotificationTypes:myTypes];
    
    NSLog(@"Done registering for Remote Notifications");


- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings

    NSLog(@"Did register for remote notification settings");
    //register to receive notifications
    [application registerForRemoteNotifications];


- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler

    NSLog(@"There was an error?");
    //handle the actions
    if ([identifier isEqualToString:@"declineAction"])

    else if ([identifier isEqualToString:@"answerAction"])

    


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

    NSLog(@"My token is: %@\nMy  device token is: %@\nSend Phone ID is: %@", [self loadSettings:@"phone_id"], deviceToken, [self loadSettings:@"send_phone_id"]);


- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error 
    NSLog(@"Failed to register for remote notifications");

对于我的 NSLog 语句,这就是我收到的全部内容:

2014-11-11 14:57:11.066 eTicket[3144:988203] Registering for Remote Notifications
2014-11-11 14:57:11.083 eTicket[3144:988203] Done registering for Remote Notifications
2014-11-11 14:57:11.148 eTicket[3144:988203] Settings Validated?: NO
2014-11-11 14:57:11.219 eTicket[3144:988203] Did register for remote notification settings

忽略“设置已验证”行。这只是我检查他们是否已经成功登录以跳过一些步骤。该部分有效,与远程通知无关。

我没有收到任何迹象表明它是否成功注册了通知,或者有错误。应用程序不会崩溃或抛出错误。我不知道为什么没有一个底层方法被击中。

谢谢, 詹姆斯

【问题讨论】:

根据我的经验,didRegisterForRemoteNotificationsWithDeviceToken 只会在您第一次注册通知时被调用。您可能需要删除该应用并从模拟器或设备重新安装。 @remus 我每次在重新构建之前都卸载了该应用程序。由于它是以前安装的,它永远不会提示我再次确认它,但是如果他们曾经接受过,这总是被调用过,或者如果他们选择了 no,则总是调用 failed to。至少,在 IOS 8 之前。 想试试this以防万一吗? @remus 我从现在开始设置了一天,结果相同,然后从现在开始 3 天,仍然得到相同的结果。不过还是谢谢。 【参考方案1】:

好吧,似乎我在上面的大部分内容中都做对了。但是,在注册设置后,我需要检查是否实际打开了任何设置。如果没有,我需要将它们发送到我自己的函数中。所以我这样做了:

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings

    NSLog(@"Did register for remote notification settings");
    NSLog(@"current notifications : %@", [[UIApplication sharedApplication] currentUserNotificationSettings]);
    //register to receive notifications
    if (notificationSettings.types) 
        NSLog(@"user allowed notifications");
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    else
        NSLog(@"user did not allow notifications");
        // show alert here
        [self doNotRegisterForRemoteNotifications];
    

doNotRegisterForRemoteNotifications 是我自己的函数,我可以在其中做我需要做的事情来生成我自己的电话 ID。

【讨论】:

远程通知 iOS 8

】远程通知iOS8【英文标题】:RemoteNotificationiOS8【发布时间】:2014-06-1404:40:35【问题描述】:如何在iOS8中获取用于远程通知的设备令牌?我在iOSAppDelegate中使用了didRegisterForRemoteNotificationsWithDeviceToken方法,它返回了设备令牌。但... 查看详情

Azure 通知中心不适用于 ios

...wingthistutorial我创建了azure通知中心并添加到我的应用程序注册中。我的应用注册远程通知成功,当我尝试在后端注册设备时,我成功获得了我的安装ID。publicoverridevoidRegisteredForRemoteNo 查看详情

iOS 8 远程通知 - 我应该何时调用 registerForRemoteNotifications()?

...牌可能会发生变化,因此您的应用每次启动时都需要重新注册。”我试图理解他 查看详情

gcm推送通知:先成功,后IOS未注册

】gcm推送通知:先成功,后IOS未注册【英文标题】:gcmpushnotification:firstsuccess,thennotregisteredinIOS【发布时间】:2016-01-1810:49:38【问题描述】:在IOS中通过谷歌云消息接收通知的所有段落之后,但我遇到了这个问题:我在php中发送... 查看详情

iOS 8 检查远程通知类型的权限

】iOS8检查远程通知类型的权限【英文标题】:iOS8checkpermissionofremotenotificationtype【发布时间】:2014-08-2914:08:21【问题描述】:我可以像这样检查用户是否在iOS8之前授予了通知(警报)权限:UIRemoteNotificationTypetypes=[[UIApplicationshared... 查看详情

IOS 8 推送通知检查返回错误的 UIRemoteNotificationType

...PushNotifications是否已启用。在AppDelegate.m中,我为远程通知注册了应用程序,并在iPhone(iOS8 查看详情

IOS 远程通知在 IOS 8 的后台不起作用

】IOS远程通知在IOS8的后台不起作用【英文标题】:IOSremotenotificationsnotworkinginbackgroundforIOS8【发布时间】:2015-03-1015:30:56【问题描述】:我一直在使用Xamarin.IOS,最近遇到了当Iphone/Ipad处于后台时尝试接收远程通知负载的问题。IOS7... 查看详情

注册远程通知失败

】注册远程通知失败【英文标题】:Failtoregisterforremotenotifications【发布时间】:2016-09-1415:53:25【问题描述】:将Xcode版本更新到8.0后,我收到此错误。当尝试注册远程通知时,我可以在我的日志中看到这样的ErrorDomain=NSCocoaErrorDoma... 查看详情

在 iOS (swift) 应用中,第二个 Firebase 应用的注册无法接收远程通知

】在iOS(swift)应用中,第二个Firebase应用的注册无法接收远程通知【英文标题】:IniOS(swift)app,registrationofasecondFirebaseappcan\'treceiveremotenotifications【发布时间】:2017-08-2116:16:04【问题描述】:我有两个Firebase项目(在同一个帐户上,... 查看详情

iOS swift中没有收到推送通知?

...题描述】:我正在使用FCM进行推送通知。FCM已连接,设备注册成功,我可以打印设备令牌,但设备没有收到通知。一般->功能选项卡->在后台模式下启用推送通知和远程通知。在这里注册设备以进行远程通知。funcapplication(_app... 查看详情

使用 Swift 2 将不允许 iOS 8 设备注册推送通知

】使用Swift2将不允许iOS8设备注册推送通知【英文标题】:UsingSwift2won\'tallowiOS8devicestoRegisterforPushNotifications【发布时间】:2015-11-1105:42:58【问题描述】:我正在使用Xcode7和Parse为iOS配置推送通知。通知在运行iOS9的设备上运行良好... 查看详情

取消注册推送通知

】取消注册推送通知【英文标题】:UnregisterPushnotification【发布时间】:2015-03-2717:37:36【问题描述】:我正在构建一个应用程序,其中我们有一个登录页面来验证用户。当用户通过身份验证(成功登录)时,他可以从我们的远程... 查看详情

远程推送通知注册失败

】远程推送通知注册失败【英文标题】:Failtoregisterforremotepushnotification【发布时间】:2015-04-2907:25:45【问题描述】:我在使用iOS8的iPhone5上设置推送通知时出错:2015-04-2900:23:14.688pushNotificationTest[192:6284]未能注册远程通知2015-04-2900... 查看详情

如何在没有证书的情况下设置远程通知? (注册护士)

】如何在没有证书的情况下设置远程通知?(注册护士)【英文标题】:Howtosetupremotenotificationswithoutcertificate?(RN)【发布时间】:2018-09-0610:03:34【问题描述】:我想在ReactNative中测试一些远程通知服务。但是遇到了一些问题。在iOS中... 查看详情

iOS 10 中的远程通知

...API参考资料和关于此框架的WWDCSession,我们现在有了一种注册推送通知的新方法,如下所示:UNUserNotificationCenter.current().reques 查看详情

快速注册 iOS 7 通知

】快速注册iOS7通知【英文标题】:RegisteringforiOS7notificationsinswift【发布时间】:2015-02-0922:42:23【问题描述】:我希望能够在iOS7和iOS8上收到通知,我已经成功地使用ios8设置了它,但是在这个iOS7上我收到一个错误,只是“lldb”,... 查看详情

注册以接收远程 CloudKit 更改的通知不起作用

】注册以接收远程CloudKit更改的通知不起作用【英文标题】:RegisteringtoreceivenotificationsofremoteCloudKitchangesnotworking【发布时间】:2020-02-2616:20:48【问题描述】:我刚刚使用新的iOS13NSPersistentCloudKitContainer完成了CoreData+CloudKit的设置。... 查看详情

Ionic 2:iOS 推送通知 - 注册 ID 错误

】Ionic2:iOS推送通知-注册ID错误【英文标题】:Ionic2:iOSPushnotification-RegisterIDError【发布时间】:2017-07-1012:34:33【问题描述】:我必须构建一个包含推送通知(iOS和Android)的Ionic2应用程序。我已经成功地为带有firebase的android设备... 查看详情