registerForRemoteNotifications method not being called properly

Mahir picture Mahir · Jun 30, 2014 · Viewed 41.3k times · Source

I'm currently using Xcode6 beta (first version). Using parse.com, I'm trying to implement push notifications. In my app delegate, I have

  [application registerForRemoteNotifications];

When I run it on my ios8 beta iPhone, the app does not ask if I would like to enable push notifications, and the corresponding application:didRegisterForRemoteNotificationsWithDeviceToken: is never called. However, when I tried running it on an ios7 iPhone, the app crashed and I received an unrecognized selector error for the registerForRemoteNotifications method.

Then, I tried running it on a previous version of Xcode (version 5.0), but I received the compile error no visible @interface declares registerForRemoteNotifications

I'm assuming this error has to do with bugs with the transition to iOS 8, but I'm not sure how I can resolve this issue.

Answer

Madao picture Madao · Jun 30, 2014

Read the code in UIApplication.h.

You will know how to do that.

First:

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

add Code like this

#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

Second:

Add this Function

#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

And your can get the deviceToken in

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

if it still not work , use this function and NSLog the error

-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error