Push notification device token

I'm looking for verification that this is correct, or to be corrected if this is wrong.


Here's some code:

#import <UserNotifications/UserNotifications.h>

My AppDelegate.h has this:

@interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate>

And willFinishLaunchingWithOptions has this:

UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){
  if( !error )
  {
    NSLog( @"Push registration success." );
  }
  else
  {
    NSLog( @"Push registration FAILED." );
  }
}];


I'm getting the success message. So far, so good.


In previous iOS versions, this delegate method was called:

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

For iOS 10, I can get this method to be called if I add this to my requestAuthorizationWithOptions completionHandler:

[[UIApplication sharedApplication] registerForRemoteNotifications];

Is this the right way to do it? It seems incorrect because, when I do this, the old application:didReceiveRemoteNotification method is still firing, as-is the new center:didReceiveNotificationResponse.

So, two things are firing now when a user taps a notification, and that does not seem right. However, if I remove the call to registerForRemoteNotifications, nothing fires when I receive a remote notification. And that makes sense because we have to have the device token to send to AWS, which facilitates delivery of notifications between us and Apple and Google.

I don't see a way to get the device token from any of the new UserNotification classes, but it seems like I've done something wrong since UIApplicationDelegate and UNUserNotificationCenterDelegate are both reacting to push notifications.

Any input?

Thanks.

Accepted Reply

I posted what I think is the full solution to here:

https://forums.developer.apple.com/message/162269

Replies

I found the answer here:

https://developer.apple.com/library/prerelease/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/IPhoneOSClientImp.html#//apple_ref/doc/uid/TP40008194-CH103-SW1


According to that document, this is the way to do it. It just seems odd to have two delegates reacting to the same event. Oh well.

Oops. Never mind. That document does not mention anything about the new NotificationCenter classes. There is a link to it from the USUserNotificationCenter documentation, but it does not appear to be current.

I posted what I think is the full solution to here:

https://forums.developer.apple.com/message/162269

https://forums.developer.apple.com/message/162269 doesn't work anymore..