Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 266
Chris Avatar asked Apr 04 '14 15:04

Chris


1 Answers

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

like image 135
Erhan Avatar answered Sep 20 '22 07:09

Erhan