Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

registerForRemoteNotifications method not being called properly

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.

like image 734
Mahir Avatar asked Jun 30 '14 08:06

Mahir


2 Answers

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
like image 138
Madao Avatar answered Sep 30 '22 05:09

Madao


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 miss UIBackgroundModes in PList.. can be done in Capabilities)

like image 41
ingconti Avatar answered Sep 30 '22 06:09

ingconti