Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not getting APNS Device token on ios 13

I have issue related to APNS device token . Before I was using Xcode 10.2 and iOS 12.1. At this moment I used to get the device token in delegate method

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

I am registering for APNS like this and it was working fine.

UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    center.delegate = self;
    [center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){
                if(!error){
                    [[UIApplication sharedApplication] registerForRemoteNotifications];
                }
    }];

Now when installed iOS 13 to my iPhone device and using Xcode 11 , the delegate method didRegisterForRemoteNotificationsWithDeviceToken is not called. Unable to understand this problem . I have already done research over this , I know there are some changes in getting token from the delegate method but in my case delegate method is not even called. Again it's working fine for iOS 12.

like image 965
IOS Dev Avatar asked Oct 07 '19 06:10

IOS Dev


People also ask

What is device token in iOS?

A device token is an identifier for the Apple Push Notification System for iOS devices. Apple assigns a Device Token on a per-app basis (iOS 7 and later) which is used as a unique identifier for sending push notifications.

Is device token unique in iOS?

The Device Tokens are NOT unique to the phone-app pairing. They are unique to the phone only. If you have multiple apps with push on the same phone they will all use the same Device Token. The certificate you use to send the notification will dictate which app it goes to.


3 Answers

Just reboot your iPhone. It's as simple as that and in 90% of cases it will solve your problem.

like image 62
Aleksandr Honcharov Avatar answered Oct 10 '22 05:10

Aleksandr Honcharov


Logon on https://appleid.apple.com/, then open url "https://developer.apple.com/account/ios/identifier/bundle" or "https://developer.apple.com/account/resources/certificates/list".

First, Create two New Certificates: (1)Apple Development Sign development versions of your iOS, macOS, tvOS, and watchOS apps. For use in Xcode 11 or later. (2)Apple Distribution Sign your apps for submission to the App Store or for Ad Hoc distribution. For use with Xcode 11 or later.

Then find menu "Identifiers" via url "https://developer.apple.com/account/resources/identifiers/list". Edit your Identifiers of your app, make sure that Development SSL Certificate & Production SSL Certificate is added to the Push Notifications.

Next, open menu "Profiles" via url "https://developer.apple.com/account/resources/profiles/list". Make sure that the Certificates as type of DistributionFor which will be used in Xcode 11 or late, and save

Lastly, download the Provisioning Profile file and the CA Certificates files created to your MAC, which will be added to XCode and key-chain application by double click the files separately.

What's more, remember to reboot your cell phone, and make sure that you had setup the remote notification correctly.

like image 45
Dengshan Tian Avatar answered Oct 10 '22 05:10

Dengshan Tian


I also faced the same problem. I tried many scenarios. I got success after doing the below steps:

  1. Restarted device
  2. Called the registerForRemoteNotifications method in the main thread.

In my case, I was getting a device token, but there is a delay in response (I think due to registering remote notifications in a background thread). But after moving [[UIApplication sharedApplication] registerForRemoteNotifications] in the main thread, all works fine.

Here is my code:

    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    center.delegate = delegate;
    [center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){
        if(!error){
            dispatch_async(dispatch_get_main_queue(), ^{
                [[UIApplication sharedApplication] registerForRemoteNotifications];
                [self callCompletion:TRUE completion:completion];
            });
        }
    }];

Hope this will help.

like image 1
Kirti Nikam Avatar answered Oct 10 '22 03:10

Kirti Nikam