如何从远程通知操作按钮打开应用

Rob*_*ert 4 objective-c apple-push-notifications ios

与主题一样,我试图在用户点击远程通知上的“接受”按钮时打开应用程序。

下面列出了AppDelegate负责处理按钮动作的方法:

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

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

    }

    completionHandler();
}
Run Code Online (Sandbox Code Playgroud)

我一直在寻找解决方案,但找不到对我有用的信息。



更新: 由于句子“可操作的通知按钮”引起混乱,因此以下img显示了此句子的意思。

在此处输入图片说明

Már*_*sme 5

当您为通知注册设备时,您可以使用UIUserNotificationSettings 进行操作

[[UIApplication sharedApplication] registerUserNotificationSettings:[self createUserNotificationSettings]];
Run Code Online (Sandbox Code Playgroud)

例如,在此方法中,您可以创建UIUserNotificationAction,它们是操作按钮和自定义设置:

- (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];
 }
Run Code Online (Sandbox Code Playgroud)

当您收到通知时,将调用以下方法,您可以检查按下了哪个按钮:

- (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");
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这不是我的问题的答案。您的答案中提出的实现是由我完成的,并且效果很好,但我的问题是如何根据单击的按钮使用“handleActionWithIdentifier”方法将应用程序置于前台? (7认同)

Sar*_*ner 5

斯威夫特4:

只需确保将其包含.foreground到UNNotificationAction对象中即可。

例:

// 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: [])
Run Code Online (Sandbox Code Playgroud)


Sal*_*ani 5

迅速5:

let action = UNNotificationAction(identifier: "sample", title: "sample", options: [UNNotificationActionOptions.foreground])
Run Code Online (Sandbox Code Playgroud)

UNNotificationActionOptions.foreground 使您可以在前台启动应用程序,即使应用程序被强制关闭,您的代码也会运行。

创建您的操作后。将其分配给类别。

let category = UNNotificationCategory(identifier: "sample", actions: [action], intentIdentifiers: [], options: [])
Run Code Online (Sandbox Code Playgroud)

然后分配类别

UNUserNotificationCenter.current().setNotificationCategories([category])
Run Code Online (Sandbox Code Playgroud)