如何从远程通知操作按钮

时间:2016-01-08 13:29:16

标签: ios objective-c apple-push-notifications

正如在主题中一样,当用户点击并接受'时,我试图打开应用。远程通知按钮。

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

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

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

    }

    completionHandler();
}

我一直在寻找解决方案,但我无法为我找到有用的信息。

更新 因为句子'可操作的通知按钮'引起混淆,下面的img显示了我的意思。

enter image description here

3 个答案:

答案 0 :(得分:4)

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

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

例如,在此方法中,您可以创建 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];
 }

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

- (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");
    }
}

答案 1 :(得分:2)

迅速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: [])

答案 2 :(得分:2)

迅速5:

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

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

创建动作后。将其分配给类别。

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

然后分配类别

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