Chris Chris - 4 months ago 35
iOS Question

Can I Register for Push Notifications Outside of AppDelegate.m?

My app is able to successfully register for push notifications but I would like to move the pop-up alert to a different area of the app

If I implement the same code from

AppDelegate.m
to a different screen,
Other.m
, of my app, 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 enable
didRegisterForRemoteNotificationsWithDeviceToken
in my
AppDelegate.m
, the
AppDelegate.m
instance of the method gets called from my
Other.m
, but that is not how I want this to work.

Any ideas or suggestions would be appreciated.

Answer

Yes, of course.

You can register wherever you want with using

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

but didRegisterForRemoteNotificationsWithDeviceToken:deviceToken is only available in AppDelegate