Ios 未正确调用registerForRemoteNotifications方法

Ios 未正确调用registerForRemoteNotifications方法,ios,xcode,push-notification,parse-platform,Ios,Xcode,Push Notification,Parse Platform,我目前正在使用Xcode6测试版(第一个版本)。使用parse.com,我试图实现推送通知。在我的应用程序代理中,我有 [application registerForRemoteNotifications]; 当我在ios8 beta版iPhone上运行它时,该应用程序不会询问我是否要启用推送通知,并且不会调用相应的应用程序:didRegisterForRemoteNotificationsWithDeviceToken:。但是,当我尝试在ios7 iPhone上运行它时,应用程序崩溃了

我目前正在使用Xcode6测试版(第一个版本)。使用parse.com,我试图实现推送通知。在我的应用程序代理中,我有

  [application registerForRemoteNotifications];
当我在ios8 beta版iPhone上运行它时,该应用程序不会询问我是否要启用推送通知,并且不会调用相应的
应用程序:didRegisterForRemoteNotificationsWithDeviceToken:
。但是,当我尝试在ios7 iPhone上运行它时,应用程序崩溃了,我收到了
registerforremotentifications
方法的
无法识别的选择器
错误

然后,我尝试在以前版本的Xcode(5.0版)上运行它,但收到编译错误
no visible@interface declares registerForRemoteNotifications


我假设此错误与过渡到iOS 8的错误有关,但我不确定如何解决此问题。

RegisterForRemotonifications
方法可从iOS 8获得

application:didRegisterForRemotionNotificationsWithDeviceToken:
delegate方法仅在注册成功时调用,如果出现任何失败,它将调用
application:didFailToRegisterForRemoteNotificationsWithError:
方法

有关更多信息,请浏览:

另外,检查应用程序的通知是否启用
设置->通知->应用程序->启用通知

根据文档,两次回调都不会发生 直到设备与推送服务器建立持久连接。所以 如果没有可用的wifi或数据连接,则回调将不可用 发生-苹果不认为这是一个错误的条件。只有可能导致DID失败的错误。。。 回调是一个不正确的证书/应用程序权限问题(a) 开发问题),或用户拒绝了权限

请仔细阅读UIApplication.h中的代码。 你会知道怎么做的

第一:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
添加这样的代码

#ifdef __IPHONE_8_0
  //Right, that is the point
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound) categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
#else
    //register to receive notifications
    UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
#endif
第二:

添加此功能

#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
如果仍不起作用,请使用此功能并记录错误


在iOS 8中,系统不会要求用户允许你的应用发送推送(远程)通知,默认情况下是允许的


用户可以在设置>或“应用程序>通知>允许通知”上选择不允许您的应用程序发出通知,只需使用此代码片段在iOS 8中注册通知:

UIUserNotificationSettings *settings = 
    [UIUserNotificationSettings 
        settingsForTypes: (UIUserNotificationTypeBadge | 
                           UIUserNotificationTypeSound |                 
                           UIUserNotificationTypeAlert) 
        categories:nil];

[[UIApplication sharedApplication] registerUserNotificationSettings:settings];

使用
UIUserNotificationType
而不是
UIRemoteNotificationType

一些观察结果:

  • 问题是使用远程,而不是本地
  • 有些问题涉及当地,这是完全不同的事情
  • 我同意回答时间可能很长,但正如其他人所指出的,您必须实现回调(也称为委托方法)
  • 不针对编译器标志进行测试,这在概念上和实践上都是错误的
  • 测试选择器是否存在,如本代码所示:

    func registerForPushForiOS7AndAbove(){
    
    UIApplication.sharedApplication()
    let application = UIApplication.sharedApplication()
    if application.respondsToSelector(Selector("registerUserNotificationSettings:")) {
    
        let notifSettings = UIUserNotificationSettings(forTypes: .Sound | .Alert | .Badge,
            categories: nil)
    
        application.registerUserNotificationSettings(notifSettings)
        application.registerForRemoteNotifications()
    
    }
    else{
        application.registerForRemoteNotificationTypes( .Sound | .Alert | .Badge )
    }
    
    }

(不要错过
ui PList.中的backgroundmodes.可以在功能中完成)

我已经实现了这两种委托方法,但两种方法都没有实现called@Mahir检查互联网连接是否可用。事实上,对于ios8设备,互联网速度异常缓慢。由于
RegisterForemotentifications
仅为ios8,要尝试ios7设备,我是否使用
RegisterForemotentificationsTypes:
?@Mahir始终建议使用新api,请检查操作系统版本以调用特定方法。在iOS9上测试,即使没有internet连接,也会调用didRegisterForRemoteNotificationsWithDeviceToken。但它需要在线调用一次,才能让操作系统存储令牌。您也在此处逐字发布了此答案:,它无法正确执行运行时检查,以确定您是否在iOS 8上运行。对于那些需要同时支持iOS 7和iOS 8的人来说,点击其他答案并阅读其中的注释,以获得实现运行时检查的正确方法是值得的。这实际上是不正确的,应该被否决。在调用UIApplication RegisterForRemotonification方法之前,不会调用application:didRegisterUserNotificationSettings:方法。你不能用那种方法叫它。@GregG我不知道你为什么这么说。这些文档特别指出:“注意:只有当应用程序已成功使用registerUserNotificationSettings注册用户通知时,才会进行这些回调:”这些文档表明,每次启动应用程序时都应调用registerForRemoteNotifications,以确保获得更新的令牌。最新的远程通知PG(10/31/14)指出,当用户响应警报时,将只调用一次didRegisterUserNotificationSettings,因此在该回调中调用registerForRemoteNotifications将导致每次仅调用一次。此外,最新文档显示了一个示例,其中在didFinishLaunchingWithOptions中依次调用了registerUserNotificationSettings:和RegisterForRemotionTifications,请原谅:我误读了有关didRegisterUserNotificationSettings回调的文档。每次都会调用它,但警报只显示一次。因此,将registerforremotentifications调用放在那里应该没问题,尽管文档中的示例与我之前在didFinishLaunchingWithOptions:method中所述的一样。当我将iPhone 4S从7.1.2更新到8.0.2时,推送停止工作,而它在我的iPhone 5 8.0.2上就像一个魔咒一样工作。我必须选择Settings>Yor应用程序路径中的“background update”行才能让它工作。在某些版本的iOS registerForRemoteNotifications()中需要,而在其他版本中则不需要。我通过检查SharedApplication是否响应选择器“RegisterForremotentifications:”来解决这个问题。如果是的话,我就打电话来
func registerForPushForiOS7AndAbove(){

UIApplication.sharedApplication()
let application = UIApplication.sharedApplication()
if application.respondsToSelector(Selector("registerUserNotificationSettings:")) {

    let notifSettings = UIUserNotificationSettings(forTypes: .Sound | .Alert | .Badge,
        categories: nil)

    application.registerUserNotificationSettings(notifSettings)
    application.registerForRemoteNotifications()

}
else{
    application.registerForRemoteNotificationTypes( .Sound | .Alert | .Badge )
}