I am implementing Firebase push notifications on my iOS app.
On didFinishLaunchingWithOptions
, I call [FIRApp configure]
. It returns nill token the first time the app starts, but if I run the app again, it returns a valid working token.
NSString *refreshedToken = [[FIRInstanceID instanceID] token];
null the first time.
Here's the code in application didFinishLaunchingWithOptions:
:
- (void)handleFCMregister
{
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_9_x_Max) {
UIUserNotificationType allNotificationTypes =
(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings =
[UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
} else {
// iOS 10 or later
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
UNAuthorizationOptions authOptions =
UNAuthorizationOptionAlert
| UNAuthorizationOptionSound
| UNAuthorizationOptionBadge;
[[UNUserNotificationCenter currentNotificationCenter]
requestAuthorizationWithOptions:authOptions
completionHandler:^(BOOL granted, NSError * _Nullable error) {
}
];
// For iOS 10 display notification (sent via APNS)
[[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];
// For iOS 10 data message (sent via FCM)
[[FIRMessaging messaging] setRemoteMessageDelegate:self];
#endif
}
[[UIApplication sharedApplication] registerForRemoteNotifications];
[FIRApp configure];
NSString *refreshedToken = [[FIRInstanceID instanceID] token];
DLog(@"refreshedToken ::%@", refreshedToken);
}
How to get a valid token the first time the app starts? What seems to be wrong? Thanks.