Can I register for push notifications outside of AppDelegate.m?

My application can successfully register for push notifications, but I would like to move the pop-up warning to another area of ​​the application.

If I implement the same code from AppDelegate.m to another screen, Other.m , of my application, it never registers:

 -(void)buttonTapped { // Register for Push Notifications UIRemoteNotificationType notifyTypes = (UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge); [[UIApplication sharedApplication] registerForRemoteNotificationTypes:notifyTypes]; } #pragma mark - APNS Functions -(void)application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken { NSLog(@"didRegisterForRemoteNotifications - 1"); } -(void)application:application didFailToRegisterForRemoteNotificationsWithError:error { NSLog(@"didFailToRegisterForRemoteNotifications"); } 

If I included didRegisterForRemoteNotificationsWithDeviceToken in my AppDelegate.m , an instance of the AppDelegate.m method is called from my Other.m , but that’s not how I want it to work.

Any ideas or suggestions would be appreciated.

+7
ios objective-c iphone apple-push-notifications
source share
3 answers

Oh sure.

You can register wherever you want using

 [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)]; 

but didRegisterForRemoteNotificationsWithDeviceToken: deviceToken is only available in AppDelegate

+13
source share
 -(void)application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken 

This is the UIApplicationDelegate protocol protocol, so it will be launched in [UIApplication sharedApplication] .delegate. By default, this is AppDelegate.

0
source share

The accepted answer was deprecated in iOS 8.0. After running Swift code for iOS 9.0:

 let notificationSettings = UIUserNotificationSettings(forTypes: [.Badge, .Sound, .Alert], categories: nil) UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings) 
0
source share

All Articles