简体   繁体   中英

iOS - registerForRemoteNotifications callback in separate UIView

I have the following flow: on the first app launch, I present a view which explains to the user why I need him to accept push notifications. He then has two options: skip (if he does this, I will never register for push notifications), or accept push notifications, in which case I display the push notifications popup where he can either accept or refuse.

I have two issues. First of all, I always used to register for push notifications in the AppDelegate, with the following code:

    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)])
{
    UIUserNotificationType types = (UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound);
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
    [application registerUserNotificationSettings:settings];
    [application registerForRemoteNotifications];
}

in the - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method.

I am now attempting to use this exact same code in the view I display with the two buttons, and I've removed the code from the AppDelegate. Will this work anyway? The issue with having the code in the AppDelegate is that it directly prompts the user to either accept or refuse the push notifications on first launch, something I want to avoid.

Then, there is the issue that I need to know as soon as the user has either accepted or refused the push notifications (on the popup). I don't care what decision he's made, I just need to know as soon as he's made one, to be able to present the next view.

I've tried the following delegate methods:

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken

and -(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error

But they don't get called when I click on either allow or don't allow.

What is the right way to do this?

I found the solution to this problem. First of all, as commented by @ystack, you can absolutely invoke registration for remote notifications anywhere in the app.

Secondly, I added this method in my App Delegate:

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
[application registerForRemoteNotifications];
[[NSNotificationCenter defaultCenter]
 postNotificationName:@"pushNotification"
     object:self];
}

Then I added this in my particular ViewController:

        [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(pushNotificationClicked:)
                                                 name:@"pushNotification"
                                               object:nil];

And finally this method in the same ViewController:

- (void)pushNotificationClicked:(NSNotification *) notification {

if ([[notification name] isEqualToString:@"pushNotification"]){
    //do whatever you want
}
}

This way I get a callback when the user presses either Allow or Don't Allow.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM