registerForRemoteNotifications方法未正确调用

Mah*_*hir 15 xcode push-notification ios parse-platform

我目前正在使用Xcode6 beta(第一版).使用parse.com,我正在尝试实现推送通知.在我的应用代表中,我有

  [application registerForRemoteNotifications];
Run Code Online (Sandbox Code Playgroud)

当我在ios8 beta iPhone上运行它时,应用程序不会询问我是否要启用推送通知,并且application:didRegisterForRemoteNotificationsWithDeviceToken:从不调用相应的通知.但是,当我尝试在ios7 iPhone上运行它时,应用程序崩溃了,我收到了unrecognized selectorregisterForRemoteNotifications方法的错误.

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

我假设这个错误与过渡到iOS 8的错误有关,但我不知道如何解决这个问题.

Mad*_*dao 29

阅读UIApplication.h中的代码.

你会知道怎么做.

第一:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
Run Code Online (Sandbox Code Playgroud)

像这样添加代码

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

第二:

添加此功能

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

你可以获得deviceToken

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
Run Code Online (Sandbox Code Playgroud)

如果它仍然不起作用,使用此功能和NSLog 错误

-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
Run Code Online (Sandbox Code Playgroud)

  • 这实际上是不正确的,应该被拒绝.应用程序:didRegisterUserNotificationSettings:方法将不会被调用*直到*您调用UIApplication registerForRemoteNotification方法.你不要在那个方法中调用它. (14认同)
  • 这个答案,你也在这里逐字发布:http://stackoverflow.com/a/24488562/1146823,没有正确实现运行时检查你是否在iOS 8上运行.对于那些需要支持的人无论是iOS 7还是8,都值得点击其他答案并阅读其中的注释,以便以正确的方式实现运行时检查. (9认同)
  • 文档表明每次应用程序启动时都应调用registerForRemoteNotifications以确保获得更新的令牌.最新的远程通知PG(10/31/14)声明,只有当用户响应警报时才会调用didRegisterUserNotificationSettings,因此在该回调中调用registerForRemoteNotifications将导致只调用一次.此外,最新的doc显示了一个示例,其中registerUserNotificationSettings:和registerForRemoteNotifications在didFinishLaunchingWithOptions中按顺序调用: (4认同)

ing*_*nti 5

几点意见:

  • 问题是使用REMOTE,而不是本地
  • 一些问题与当地有关,这是完全不同的事情
  • 我同意答案时间可能很长,但你必须实现回调(aka委托方法),正如其他人所指出的那样
  • 不要对编译器标志进行测试,在概念上和实际上都是错误的
  • 测试选择器的存在,如下代码所示:

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

    }

(不要错过PList中的UIBackgroundModes ..可以在Capabilities中完成)