简体   繁体   中英

How can I open app from remote notification action button

As in subject, I'm trying to open app when user taps 'Accept' button on remote notification.

Below is listed AppDelegate method which is responsible for handling button action:

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)notification  completionHandler: (void (^)())completionHandler {

    if ([identifier isEqualToString: @"ACCEPT_IDENTIFIER"]) {

    }

    completionHandler();
}

I was looking for solution awhile but I can't find helpful information for me.



Update: Because sentence 'Actionable notification buttons' cause confusion, img below shows what I mean by this sentence.

在此处输入图片说明

Swift 4:

Just make sure you include .foreground to your UNNotificationAction object.

Example:

// This will cause the app to launch in the foreground:
let action = UNNotificationAction(identifier: "showAction", title: "Show", options: [.foreground])

// This will not:
let action = UNNotificationAction(identifier: "showAction", title: "Show", options: [])

When you register the device for notifications you do it with a UIUserNotificationSettings :

[[UIApplication sharedApplication] registerUserNotificationSettings:[self createUserNotificationSettings]];

For instance, in this method you can create the UIUserNotificationAction which are the action buttons and custom settings:

- (UIUserNotificationSettings *) createUserNotificationSettings {
UIMutableUserNotificationAction *action1 = [[UIMutableUserNotificationAction alloc] init];
[action1 setActivationMode: UIUserNotificationActivationModeForeground];
[action1 setTitle:@"Title1"];
[action1 setIdentifier:@"first_button"];
[action1 setDestructive:YES];
[action1 setAuthenticationRequired:NO];

UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init];
[action2 setActivationMode: UIUserNotificationActivationModeForeground];
[action2 setTitle:@"Title2"];
[action2 setIdentifier:@"second_button"];
[action2 setDestructive:NO];
[action2 setAuthenticationRequired:NO];

UIMutableUserNotificationCategory *actionCategory = [[UIMutableUserNotificationCategory alloc] init];
[actionCategory setIdentifier:@"ACTIONABLE"];
[actionCategory setActions:@[action1, action2]
                forContext:UIUserNotificationActionContextDefault];

NSSet *categories = [NSSet setWithObject:actionCategory];
UIUserNotificationType types = (UIUserNotificationTypeAlert|
                                UIUserNotificationTypeSound|
                                UIUserNotificationTypeBadge);

return [UIUserNotificationSettings settingsForTypes:types categories:categories];
 }

When you receive the notification the following method gets called and you can check which button was pressed:

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)notification  completionHandler: (void (^)())completionHandler {
    if ([identifier isEqualToString: @"first_button"]) {
        NSLog(@"First notification button was pressed");
    } else {
        NSLog(@"Second notification button was pressed");
    }
}

swift 5:

let action = UNNotificationAction(identifier: "sample", title: "sample", options: [UNNotificationActionOptions.foreground])

the UNNotificationActionOptions.foreground gives you the application launch in the foreground and your code runs even the app is force closed.

after creating your action. assign it to the category.

let category = UNNotificationCategory(identifier: "sample", actions: [action], intentIdentifiers: [], options: [])

then assign the category

UNUserNotificationCenter.current().setNotificationCategories([category])

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