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.

Amal T S
  • 3,327
  • 2
  • 24
  • 57
Paul
  • 6,108
  • 14
  • 72
  • 128
  • 2
    For 19 July, 2016:- There may be issue from apple side,that is why delegates not called... check [this link](http://stackoverflow.com/a/38456437/4601170) – Bhavin Bhadani Jul 19 '16 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 '16 at 18:47
  • @rahulgarg good to know :) – Bhavin Bhadani Jul 20 '16 at 04:32

19 Answers19

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.

Kunal Gupta
  • 2,984
  • 31
  • 34
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.

Bhavin Bhadani
  • 22,224
  • 10
  • 78
  • 108
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.

Homer Wang
  • 531
  • 6
  • 8
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.

Sven Driemecker
  • 3,421
  • 1
  • 20
  • 22
  • 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"). – Sven Driemecker Jan 27 '15 at 08: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 '15 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 '15 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 '15 at 08: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 '16 at 19:31
  • didRegisterForRemoteNotificationsWithDeviceToken get call on other devices but does not work on two iphone.You have any idea about this – Pramuka Dias Jul 19 '16 at 13:32
  • deprecated in iOS 10 – Vyachaslav Gerchicov Apr 16 '20 at 12:37
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.

Amal T S
  • 3,327
  • 2
  • 24
  • 57
  • 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 '19 at 12:58
  • 1
    I was struggling for whole day, you made my day. thank you. – Mohammad Reza Koohkan Mar 28 '20 at 20:16
  • I have the same issue, it's not working on Wifi or my Data plan :/ – Omar Masri Apr 21 '20 at 14:56
  • 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 '20 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 '20 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 '20 at 20:21
19

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
    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. – sean_j_roberts Feb 24 '21 at 22:32
  • Encountered this too. See the following Firebase GitHub issue link for resolution: https://github.com/firebase/firebase-ios-sdk/issues/7473#issuecomment-775277208 – kgaidis Jul 31 '22 at 19:46
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
Naishta
  • 11,885
  • 4
  • 72
  • 54
  • 1
    they forgot to mention that here: https://firebase.google.com/docs/cloud-messaging/ios/client Background Modes actually triggers the `didRegisterForRemoteNotifications` THANK YOU~! – Mikael Feb 20 '19 at 08:30
  • Aaah, thank you. That's what StackOverflow is all about :) – Jean Le Moignan Jan 30 '20 at 16:25
8

Before making yourself crazy:

  • First try to restart your device

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

Ely
  • 8,259
  • 1
  • 54
  • 67
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!

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.

rounak
  • 9,217
  • 3
  • 42
  • 59
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

Luda
  • 7,282
  • 12
  • 79
  • 139
  • 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 '19 at 17:34
  • In the newer version of Xcode push notifications work with simulators. – Kendall Helmstetter Gelner Feb 11 '20 at 06:44
  • Ho wI can work with push notifications in Xcode and with Simulators? – shogitai Mar 18 '20 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.

Syed Zahid Shah
  • 391
  • 3
  • 5
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];
Amal T S
  • 3,327
  • 2
  • 24
  • 57
Kong Hantrakool
  • 1,865
  • 3
  • 20
  • 35
  • 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 – Kong Hantrakool Jul 08 '16 at 04: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

Eneko Alonso
  • 18,884
  • 9
  • 62
  • 84
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 .

Community
  • 1
  • 1
Jaywant Khedkar
  • 5,941
  • 2
  • 44
  • 55
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

Jeff Gu Kang
  • 4,749
  • 2
  • 36
  • 44
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.

mfaani
  • 33,269
  • 19
  • 164
  • 293
0

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

Chris
  • 7,579
  • 3
  • 18
  • 38
Santiago
  • 448
  • 3
  • 12
0

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

Andrey Volobuev
  • 862
  • 8
  • 11