22

First of all, I am on iPhone 6 Plus/iOS 8.1 and I've tried everything here: why didRegisterForRemoteNotificationsWithDeviceToken is not called

Still no avail. To summarize:

  • I've checked my bundle identifier matches the one in provisioning profile/development portal
  • I've created a new development push APNS certificate
  • I've created a new development certificate
  • I've created a new provisioning profile for that development certificate
  • I've downloaded both the certificate and provisioning profile, and obviously, double clicked them to install
  • I've verified that everything is right on Developer Portal: all certificates and provisioning profiles valid, push enabled and configured with my new APNS certificate
  • I've uploaded my new APNS certificate to Parse (it's irrelevant at this step, but anyway) as I'm using Parse for my backend
  • I've made sure that I'm using the correct certificate/provisioning profile pair at Xcode to codesign
  • I've checked Notifications settings in case my app is not allowed to receive pushes, it's not there
  • I've tried setting date manually to tomorrow and tried re-installing the app
  • I've deleted the app from my device
  • I've deleted any related provisioning profiles from my device
  • I've restarted my device multiple times

In application:didFinishLaunchingWithOptions: I call:

if([application respondsToSelector:@selector(registerUserNotificationSettings:)]){
        //iOS 8+
        [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
    }else{
        //pre-iOS 8
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
         (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
    }

In application:didRegisterUserNotificationSettings: I call:

[application registerForRemoteNotifications];

I've checked it with a breakpoint, it DOES get called. I've also implemented two methods:

application:didRegisterForRemoteNotificationsWithDeviceToken:

and

application:didFailToRegisterForRemoteNotificationsWithError:

However, neither of them is called. Not an error, nothing. No error at the console either. (I've had an issue about entitlements earlier today, but creating new certificate/provisioning profile solved that)

What could be the issue?

UPDATE: Here is something. In application:didRegisterUserNotificationSettings: I've checked the notification settings and here is what I've got:

(lldb) po notificationSettings
<UIUserNotificationSettings: 0x170031cc0; types: (none);>

UPDATE 2: I've checked notifications again, and found out that now my app is added to the notifications in Settings, it's enabled, given permissions. But still, the handler is not called. Types are none. I'm 99% sure it's related to the problem.

UPDATE 3: I've tested on another device (iPod touch, iOS 8.1), and I've got no problems there. It immediately called the application:didRegisterForRemoteNotificationsWithDeviceToken: method with its token. The problem is specific to my iPhone.

Community
  • 1
  • 1
Can Poyrazoğlu
  • 33,241
  • 48
  • 191
  • 389
  • Did you reboot the phone? Sometimes mine no longer receives push notifications, I reboot and notifications are received again. – Imotep Nov 08 '14 at 15:16
  • @Imotep yes, unfortunately. multiple times, in combination with deleting the provisioning profiles and the app. – Can Poyrazoğlu Nov 08 '14 at 15:17
  • Could it be because of this? http://stackoverflow.com/questions/26690386/apple-push-notifications-not-arriving-anymore/26690992#26690992 – Flexicoder Nov 08 '14 at 15:24
  • @Flexicoder nope, the problem is not that "my device doesn't receive pushes from remote server". my problem is "my device doesn't even register for remote notifications locally" – Can Poyrazoğlu Nov 08 '14 at 15:26
  • Are you using the correct development certificate? I had to regenerate my development certificate for this method to be called. – jonbauer Jan 16 '15 at 05:47
  • @Coveloper It wasn't about the certificate, I've changed many things and it started working properly. I have no idea what really fixed it though. – Can Poyrazoğlu Jan 16 '15 at 14:08
  • @CanPoyrazoğlu good to hear! – jonbauer Jan 17 '15 at 00:54
  • I was having the same issue and I the answer here solved my problem: http://stackoverflow.com/questions/26261503/pass-from-uiusernotificationtypenone-to-uiusernotificationtypealert-uiusernoti My guess is that at some point while testing the app I registered with a different set of notification types and in the Settings the notifications for this app were disabled. – jjramos Feb 03 '15 at 14:36
  • @jjramos as I've pointed out in the question, I've checked Notifications settings in case my app is not allowed to receive pushes, it's not there. – Can Poyrazoğlu Feb 03 '15 at 15:35
  • @CanPoyrazoğlu, apologies. I actually meant that was the solution to my problem regarding receiving UIUserNotificationTypeNone as the notificationType in application:didRegisterUserNotificationSettings: Have you taken a look to this https://developer.apple.com/library/ios/technotes/tn2265/_index.html ? – jjramos Feb 03 '15 at 15:54
  • @jjramos yes, I've looked at it. interestingly, the problem went away, and I don't know why. – Can Poyrazoğlu Feb 03 '15 at 17:49

4 Answers4

17

I got a similar problem, the code was already implemented and working fine. Suddenly, after some adjustments, it just don't work it anymore.

The scenario running on device was:

  • Call registerForRemoteNotifications on didFinishLaunchingWithOptions.
  • The didRegisterForRemoteNotificationsWithDeviceToken won't call
  • nor didFailToRegisterForRemoteNotificationsWithError won't call.

I tried everything, almost reset all push configurations and certificates and provisioning profiles, etc.. All devices with the last version was working, but when I install the new version, just don't work anymore.

To solve this, I just did this:

  1. Went to the target capabilities;
  2. Turn the Push Notification Off;
  3. Build & Run the app on device and wait;
  4. Stop running the app;
  5. Turn the Push Notification On again.
  6. Build & Run the app on device;
  7. And like magic, it worked again

After 6h fighting with Xcode, that was my solution, without any explanation.

I hope this help someone.

Yuri Natividade
  • 354
  • 2
  • 3
4

I was asked to debug similar behaviour on a clients app, under iOS9.

It turns out the app was calling registerUserNotificationSettings and registerForRemoteNotifications from the appDelegate as is common.

However, it was also doing this a second time shortly after in a subsequent permissionsViewController.

If a user declined the request resulting from the appDelegate then the subsequent attempts to allow notifications after a user had seen a rationale for allowing it (the second calls from the permissionsViewController) were set with types set to zero same as the appDelegate calls.

Removing the initial appDelegate calls and having only the permissionsViewController calls present solved this.

ader
  • 5,403
  • 1
  • 21
  • 26
3

Another thing to check is the system status of APNS at Apple https://developer.apple.com/system-status/.

I was tearing my hair out wondering why I couldn't register my device while the APNS system was 'experiencing problems'.

Brett
  • 2,635
  • 22
  • 35
  • Hi Brett. +1. Was it, I wonder, 'experiencing problems' recently, e.g. today/yesterday? Because I'm experiencing a similar hair-pulling issue today, vs. sandbox APN (but prod APN is working fine, using the same code). – Chris W. Rea Apr 27 '16 at 16:27
  • 1
    Yes, I was going bald at the time I posted the comment! Checked the status today and there are no reported issues. didRegisterForRemoteNotificationsWithDeviceToken is now called as expected. – Brett Apr 28 '16 at 07:44
0

try using this: (works well), sorry stack doesn't seem to format this well

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        NSLog(@"iOS 8 Requesting permission for push notifications..."); // iOS 8
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:
                                                UIUserNotificationTypeAlert | UIUserNotificationTypeBadge |
                                                UIUserNotificationTypeSound categories:nil];
        [UIApplication.sharedApplication registerUserNotificationSettings:settings];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    } else {
        NSLog(@"iOS 7 Registering device for push notifications..."); // iOS 7 and earlier
        [UIApplication.sharedApplication registerForRemoteNotificationTypes:
         UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge |
         UIRemoteNotificationTypeSound];
    }


    return YES;
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{

    UIApplicationState state = [application applicationState];


    if (state == UIApplicationStateActive)
    {

        NSLog(@"received a notification while active...");

    }
    else if ( application.applicationState == UIApplicationStateInactive || application.applicationState == UIApplicationStateBackground  )
    {
        //opened from a push notification when the app was on background
        NSLog(@"i received a notification...");
        NSLog(@"Message: %@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]);
        NSLog(@"whole data: %@",userInfo);
    }

}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    self.token = [[deviceToken description] stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    self.token = [self.token stringByReplacingOccurrencesOfString:@" " withString:@""];
    [[NSUserDefaults standardUserDefaults] setObject:self.token forKey:@"deviceToken"];
    [[NSUserDefaults standardUserDefaults] synchronize];

    NSLog(@"-->> TOKEN:%@",self.token);
}
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{
    NSLog(@"Registering device for push notifications..."); // iOS 8
    [application registerForRemoteNotifications];
}
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier
forRemoteNotification:(NSDictionary *)notification completionHandler:(void(^)())completionHandler
{
    NSLog(@"Received push notification: %@, identifier: %@", notification, identifier); // iOS 8 s
    completionHandler();
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
    // Respond to any push notification registration errors here.
    NSLog(@"Failed to get token, error: %@", error);
}
MetaSnarf
  • 5,857
  • 3
  • 25
  • 41