I have an app delegate that has the following method:-
-(BOOL) registerForNotifications:(UIApplication *)application {
// [START register_for_notifications]
// Register for remote notifications. This shows a permission dialog on first run, to
// show the dialog at a more appropriate time move this registration accordingly.
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1) {
// iOS 7.1 or earlier. Disable the deprecation warnings
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
UIRemoteNotificationType allNotificationTypes =
(UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge);
[application registerForRemoteNotificationTypes:allNotificationTypes];
#pragma clang diagnostic pop
} else {
// iOS 8 or later
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_9_x_Max) {
UIUserNotificationType allNotificationTypes =
(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
[application registerUserNotificationSettings:settings];
} else {
// iOS 10 or later
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
// For iOS 10 display notification (sent via APNS)
[UNUserNotificationCenter currentNotificationCenter].delegate = self;
UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge;
[[UNUserNotificationCenter currentNotificationCenter]
requestAuthorizationWithOptions:authOptions
completionHandler:^(BOOL granted, NSError * _Nullable error) {
NSLog(@"notification permission is %@", granted ? @"granted" : @"denied");
}
];
#endif
}
[application registerForRemoteNotifications];
// [END register_for_notifications]
}
return YES;
}If I call this method from
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self registerForNotifiations:application];
}it works.
However, if I call it later (from another thread) as follows, it doesn't work:
UIApplication *application = [UIApplication sharedApplication];
[(AppDelegate *)[application delegate] registerForNotifications:application];If I delete, and install app with the first method, so notifications work, I can switch to the second code and update the app, and notifications will still work, but I can't do the reverse, that is, install app with second method, notifications don't work, update app to use first method, notifications still don't work, until I delete the app and re-install.
Do I need to call this on the main thread perhaps? If so how do I do that?
*edit*
Ok, tried the following version of the second method and it made no difference.
dispatch_async(dispatch_get_main_queue(), ^{
UIApplication *application = [UIApplication sharedApplication];
[(AppDelegate *)[application delegate] registerForNotifications:application];
});Is [UIApplication sharedApplication] the same as application passed to didFinishLaunchingWIthOptions?
I am at a loss as to why this is not working.