registerForRemoteNotifications method not being called properly registerForRemoteNotifications method not being called properly xcode xcode

registerForRemoteNotifications method not being called properly


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


A couple of observations:

  • the question was using REMOTE, not local
  • some questions deal with local, that is a completely different thing
  • I agree that answer time can be very long, but you must implement call backs (aka delegate methods), as other people pointed out
  • do NOT test against compiler flag, is conceptually and practically wrong
  • test presence of selectors as in this code:

    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 )}

    }

(don't missUIBackgroundModes in PList.. can be done in Capabilities)


In iOS 8 the system won't ask the user to allow your app to send Push (Remote) Notifications, it's allowed by default.

The user may opt-out from allowing Notifications from your app on Settings > Yor App > Notifications > Allow Notifications.