Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

enter image description here

like image 460
Robert Avatar asked Jan 08 '16 13:01

Robert


People also ask

How do I open push notifications?

Turn on notifications for Android devicesTap More on the bottom navigation bar and select Settings. Tap Turn on notifications. Tap Notifications. Tap Show notifications.

What is remote notification in IOS?

Overview. Use remote notifications (also known as push notifications) to push small amounts of data to devices that use your app, even when your app isn't running. Apps use notifications to provide important information to users. For example, a messaging service sends remote notifications when new messages arrive.


2 Answers

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: [])
like image 94
Sarp Başaraner Avatar answered Oct 05 '22 23:10

Sarp Başaraner


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");
    }
}
like image 26
Mário Cosme Avatar answered Oct 05 '22 23:10

Mário Cosme