51

I followed this thread, but the method didRegisterForRemoteNotificationsWithDeviceToken is still not called :

the documentation says :

After you call the registerForRemoteNotifications method of the UIApplication object, the app calls this method when device registration completes successfully

didRegisterUser appears well, but not did register notif.

Here is my code in the AppDelegate (the app version is 8.1) :

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //register notif
    UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
                                                    UIUserNotificationTypeBadge |
                                                    UIUserNotificationTypeSound);
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes categories:nil];
    [application registerUserNotificationSettings:settings];


    return YES;
}

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
    //register to receive notifications
    [application registerForRemoteNotifications];
    NSLog(@"didRegisterUser");
}

-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    NSLog(@"error here : %@", error);//not called
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    /*
    // Store the deviceToken in the current installation and save it to Parse.
    PFInstallation *currentInstallation = [PFInstallation currentInstallation];
    [currentInstallation setDeviceTokenFromData:deviceToken];
    currentInstallation.channels = @[ @"global" ];
    [currentInstallation saveInBackground];
     */
    NSLog(@"did register notif");//not called
}

I also have background mode -> remote notification in the info.plist.

3
  • 2
    For 19 July, 2016:- There may be issue from apple side,that is why delegates not called... check this link Jul 19, 2016 at 11:23
  • Guys just wanna let you know that APNS is working properly now. My delegates are getting called properly and I am receiving notifications. Indeed it was an issue at Apple's end but now Remote Notifications service is working fine.
    – Rahul Garg
    Jul 19, 2016 at 18:47
  • @rahulgarg good to know :) Jul 20, 2016 at 4:32

19 Answers 19

31

After a long dig I found that on 19 July, 2016 due to some error or updation at Apple's end , the didRegisterForRemoteNotificationsWithDeviceToken method would not be called even if every condition like Internet connection , Device and the methods used are perfect.

Refer to this link for confirmation https://forums.developer.apple.com/thread/52224

To verify please have a look in your other apps too. I had wasted my several hours but hope it helps someone. Thanks.

8
  • how can we register device then? Jul 19, 2016 at 10:06
  • 2
    Please have patience , I have reported bug at Apple , hope they fix it as soon as possible. Jul 19, 2016 at 10:09
  • I had also spend time to rectify the issue but I am agree with Kunal that APNS service is not working right now, Though apple system status show it is working. Jul 19, 2016 at 10:23
  • Thanks, I was starting to freak out about this. Jul 19, 2016 at 10:36
  • @KunalGupta, can you post the link of your bug which you reported to Apple? Jul 19, 2016 at 11:28
28

For 19 July 2016:-

As per Apple Developer form, there is an issue regarding Sandbox APNS down. so there may be issue from apple side,thats why delegates like application:didRegisterForRemoteNotificationsWithDeviceToken: and application:didFailToRegisterForRemoteNotificationsWithError: is not called.

To check the current status of APNS Sandbox this link... By now according to status APNS Sandbox is working fine and it's normal. so maybe there is some other bug from the apple side

So just don't worry if your methods are perfect and certificates are valid. This is just an issue from the Apple side and as soon as the issue resolved, your methods work perfectly(if everything is fine from your side).

Note that Production is working fine and the issue is regarding Sandbox APNS.

7
  • 4
    yea you are right all developers are bang their head why this happening :D Jul 19, 2016 at 11:52
  • Thanks for this information. As for now, the status page shows a green light for APNS/APNS Sandbox. My delegates still don't get called, though, so it might still be an issue.
    – Swissdude
    Jul 19, 2016 at 12:01
  • yes ... thats what I said ... production is working fine ... sandbox status is normal too ... its an issue from apple side Jul 19, 2016 at 12:03
  • wasting couple hours on this so far until I found this!
    – user523234
    Jul 19, 2016 at 14:57
  • somebody idea how may time or day far away this problem. Jul 19, 2016 at 15:32
25

I had this issue and finally found the note in Apple Developer web site and solved this issue:

Registering, Scheduling, and Handling User Notifications

iOS Note in the section: "Registering for Remote Notifications:

iOS Note: If a cellular or Wi-Fi connection is not available, neither the application:didRegisterForRemoteNotificationsWithDeviceToken: method nor the application:didFailToRegisterForRemoteNotificationsWithError: method is called. For Wi-Fi connections, this sometimes occurs when the device cannot connect with APNs over port 5223. If this happens, the user can move to another Wi-Fi network that isn’t blocking this port or, on an iPhone or iPad, wait until the cellular data service becomes available. In either case, the device should be able to make the connection, and then one of the delegation methods is called.

My iPhone only connected with Wifi, reboot iPhone and reconnect to WiFi AP solved this issue.

3
  • 7
    If this happens to an end-user, how can I detect it and handle it? If the framework doesn't call any methods I cannot react to this error. Apr 3, 2016 at 10:52
  • 1
    I was on wifi and when connected to LTE everything started working
    – Islam
    Jan 29, 2020 at 19:27
  • I had the same issue. Also the callback could come really late. Jul 13, 2020 at 8:02
21

Your code seems to be correct. As a minor improvement you could write your didRegisterUserNotificationSettings method like so:

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
    if (notificationSettings.types != UIUserNotificationTypeNone) {
        NSLog(@"didRegisterUser");
        [application registerForRemoteNotifications];
    }
}

There may be a configuration problem that causes a failed APN registration.

  1. Assure that your Provisioning Profile contains the aps-environment entry

  2. Assure that you have a unique App Identifier (a string without any "*") set in your Provisioning Profile. You should also use this exact identifier as "Bundle identifier" in your Info.plist

  3. Maybe you have declined the Push-Feature after the initial installation - in this case you will never see the in-app-push-alert again and have to enable Push in the settings app again.

  4. Try another device.

7
  • 1
    Try setting "iOS Distribution" as Code Signing Identity for Release Configuration in the Push1-Target's build settings. Else there seems to be no problem with your project. You just have to make sure that your Profile is correctly set up and that XCode uses this profile when building. You can force XCode to do this by setting "Provisioning Profile" in the Executable Target's build settings (section "Code Signing"). Jan 27, 2015 at 8:59
  • Thanks, I appreciate your help. I don't know, it does not work. I tried with another project. Would you mind trying in a new project and see if the steps work for you? I followed these steps: provisioning profile, background mode (remote notification), the code in AppDelegate, the settings in "code signing" for debug / release, the provisioning field is set to the correct one too. And the first time the app is launched, it asks me if I want to allow push, I say "ok". But I only receive "didRegisterUser". No errors.
    – Paul
    Jan 27, 2015 at 14:39
  • 2
    Hi, the problem was... the device. There was a problem with the network, I received all the iMessages, sometimes five days after they were sent. I tried again, and now it works. I had to switch off and on the device. Thank you very much for your answer.
    – Paul
    Jan 28, 2015 at 14:37
  • I having this issue but in 8.1 it is working fine. but 8.3 not calling the ' didRegisterForRemoteNotificationsWithDeviceToken' method.
    – Vineesh TP
    Mar 18, 2015 at 8:49
  • Good tips on troubleshooting this issue. I would just like to add that a valid troubleshooting step (and the one that ultimately worked for me) is simply to restart your device and/or restart XCode.
    – akousmata
    Apr 27, 2016 at 19:31
21

For IOS 13,

The didRegisterForRemoteNotificationsWithDeviceToken was not triggering at all when I connected to my wifi network. I tried a lot to fix this, but couldn't. At last I Changed my network from wifi to cellular data & suddenly it started working again. I even changed back to the old wifi network and it works with no issue.

Also, if you used your internet connection in the MAC to share using USB. Turn it off & connect your IPhone with a normal wifi or mobile data.

6
  • 5
    iOS 13 - I faced the same issues. For some unknown reasons didRegisterForRemoteNotificationsWithDeviceToken was not triggering with wifi, but works with data
    – Abin Baby
    Dec 17, 2019 at 12:58
  • 1
    I was struggling for whole day, you made my day. thank you. Mar 28, 2020 at 20:16
  • 1
    Damn, few moments before it was called perfectly, and now it is not although I reinstalled the app many times. I could never have thought of this. Thank you dude!
    – NightFury
    Apr 21, 2020 at 18:54
  • 1
    Thats bad, I created 2 separate apps on iTunes for testing purpose with different bundle Ids as I thought Call kit disabled my phone for pushes. Lol :) Apple has good surprises all the time.
    – Vish
    May 21, 2020 at 13:26
  • 1
    Wow, same issue here. It was working, then stopped suddenly. I disconnected Wifi, then re-connected, and it started working again.
    – maxmzd
    May 31, 2020 at 20:21
17

XCode 8.3.1 onwards

After trying out all the above options, if the delegate method is still not called, make sure in the project,

under 'Capabilities', (next to General), 
 - Push Notifications option is turned ON`
- and under Background Modes, Remote Notifications is turned ON
2
17

Ok, guys, I've just spent 11 straight hours debugging this thing so hopefully, this answer will help somebody.

If you got EVERYTHING said in similar threads set up correctly and have got Google Firebase analytics integrated, and didRegisterForRemoteNotificationsWithDeviceToken method is still not called, this could be because it is swizzled in Google Firebase by default. For some weird reason, FIR doesn't return to the original method after it sends your token to Google. To fix it set

    FirebaseAppDelegateProxyEnabled to NO (BOOL)

in your Info.plist.

2
  • 1
    Thank you! I just set up Firebase Crashlytics today and my push notifications stopped working. Changing that setting in my Info.plist did not solve the problem, however I did find an alternate solution: removing all Firebase-related code from my project and avoiding using Google products whenever possible in the future. Feb 24, 2021 at 22:32
  • Encountered this too. See the following Firebase GitHub issue link for resolution: github.com/firebase/firebase-ios-sdk/issues/…
    – kgaidis
    Jul 31, 2022 at 19:46
8

Before making yourself crazy:

  • First try to restart your device

Especially after moving from production to development APNS environment or vice versa.

7

Shame on me!! O forgot the most basic step:

enter image description here

After following all the certificates process stuff instructions from here: https://www.pluralsight.com/guides/swift/creating-ios-rich-push-notifications

The tool for sending push notifications was: https://github.com/noodlewerk/NWPusher

May the Force be with you!

0
7

In my case, this was happening because I was proxying all device traffic through Charles. Removing that fixed it, and gave me the didRegister callback.

1
  • Never happened to me, but it's good to be aware of that. Feb 2, 2021 at 13:15
5

For me, it was running the app on the simulator. Push token is not generated for simulators and so I was entering didFailToRegisterForRemoteNotificationsWithError instead of didRegisterForRemoteNotificationsWithDeviceToken

3
  • This is easy to forget, I was aware of this but was wondering what was going wrong when I got into my normal routine of testing on a simulator. So I added a compile time check #if targetEnvironment(simulator) that will print "Cannot register for push notifications on a simulator" if on a simulator
    – Adam Zarn
    Apr 25, 2019 at 17:34
  • In the newer version of Xcode push notifications work with simulators. Feb 11, 2020 at 6:44
  • Ho wI can work with push notifications in Xcode and with Simulators?
    – shogitai
    Mar 18, 2020 at 15:34
4

Weird Solution but it worked for me. After trying for hours, I just turn off and on the wifi from the device setting app and it worked.

1
  • Only thing that has worked for me. Such an annoying bug
    – Tyler927
    May 14, 2020 at 16:16
2

In my case, after iOS 9.3 I believe. If I used Localytics, the didRegisterForRemoteNotificationsWithDeviceToken will not get called. I have to remove this from my code (in ApplicationDidFinishLaunchingWithOption)

[Localytics autoIntegrate:@"xxxxx" launchOptions:launchOptions];
1
  • You might have to downgrade Localytics to 3.5. I just removed it altogether from my project. Their support is terrible I still have not heard back from them Jul 8, 2016 at 4:33
2

On Xcode 12 you might have to do this when adding push notifications to an app the first time:

  • Go to Project -> YourTarget -> Signing & Capabilities

On that screen, tap on the "+ Capability" button on the top left (hard to notice)

enter image description here

The Capabilities "Library" will open:

enter image description here

Select and add Push Notifications:

enter image description here

You might also want to check the "Remote notifications" box on Background modes.

enter image description here

Finally, like others mentioned, make sure your provisioning profile for Development and Distribution are up to date, and include "Push Notifications" in the list of capabilities.

enter image description here

1

Please add below code ,it working for me,

 #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

You will get device Token in below method ,

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

for detail please refer Details Answer

Hope this is help for some one .

1

I solved this issue to connect cellular mode.

I tested two iPhone connected cellular, one iPhone and one iPad connected Wi-Fi only.

Sometimes devices connected wifi worked, but sometimes it didn't. And it worked well after connecting cellular.

Apple's official doc

0

I'm not sure, but just a hunch. This happened for me a couple times. I was launching the app and then immediately backgrounding it!

The fix was to keep the app in foreground for a little while until you get the callback.

0

Your may need to a SIM card to activate your iPhone, or you will never get the notification call back.

1
  • You always need a SIM Card to activate iOS after you reset. After that, it's not necessary to keep the SIM Card. Feb 2, 2021 at 13:13
0

In my case the provision profile was invalid. Reenabling the profile fixed the issue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.