推送通知-快速回复 - (Obj-C)

在iOS 9.0中,用户对通知可以快捷回复文本消息,需要设置动作的行为为输入文本,一般用户后台模式的按钮(前台就直接进应用了)
效果图:


推送通知-快速回复 - (Obj-C)_第1张图片
action_5.png

需要创建类别,设置类别的behavior属性和parameters属性:

    
    // 创建动作
    UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc]init];
    action2.identifier = @"background";
    action2.title = @"我是后台按钮";
    action2.activationMode = UIUserNotificationActivationModeBackground;
    
    // 设置行为 (iOS 9)
    /*
     UIUserNotificationActionBehaviorDefault
     UIUserNotificationActionBehaviorTextInput  快捷回复
     */
    action2.behavior = UIUserNotificationActionBehaviorTextInput;
    // 设置行为参数
    /*
     UIKIT_EXTERN NSString *const UIUserNotificationTextInputActionButtonTitleKey 设置属性时使用的Key
     
     UIKIT_EXTERN NSString *const UIUserNotificationActionResponseTypedTextKey 接收信息时的Key
     */
    action2.parameters = @{UIUserNotificationTextInputActionButtonTitleKey:@"飞鸽传书"};

获取快速回复的推送通知内容:

/**
 *  当接收到使用iOS9快捷回复的通知后调用
 *
 *  @param application       应用
 *  @param identifier        动作标识符
 *  @param notification      通知
 *  @param responseInfo      响应的内容(快捷回复输入的内容)
 *  @param completionHandler 完成回调
 */
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void (^)())completionHandler{
    
    NSString *inputContent = responseInfo[UIUserNotificationActionResponseTypedTextKey];
    NSLog(@"%@",inputContent);
    
    // 调用回调 更新应用的快照&设置应用的挂起
    completionHandler();
}

如果实现该方法,将不再调用以下方法

- (void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)())completionHandler NS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED;

快速回复:

推送通知-快速回复 - (Obj-C)_第2张图片
快捷回复.png
推送通知-快速回复 - (Obj-C)_第3张图片
快速回复_1.png
获取快速回复内容.png

示例代码:
ViewController

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

// 创建本地通知
- (IBAction)StartLocalNoteButtonClick:(id)sender {
    
    // 创建本地通知
    UILocalNotification *localNotification = [[UILocalNotification alloc]init];
    
    // 设置属性
    // 1.触发事件
    localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
    // 2.设置通知的显示内容
    localNotification.alertBody = @"收到一条新消息";
    
    // 设置通知的类别  (与类别进行绑定)
    localNotification.category = @"localNote";
    
    // 设置传递的信息
    localNotification.userInfo = @{@"page":@1};
    
    // 预定通知(UIApplication 作为系统和应用的桥梁,一般负责应用和系统相关的工作)
    // iOS 8.0以后,在预定和展示通知之前必须要注册通知,请求授权
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 注册通知,请求授权
    /*
         UIUserNotificationTypeNone  无任何效果
         UIUserNotificationTypeBadge 通知含有角标
         UIUserNotificationTypeSound 带声音
         UIUserNotificationTypeAlert 提示文字
     */
    
    // 设置类别
    UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc]init];
    // 设置类别标记
    category.identifier = @"localNote";
    
    // 创建动作
    UIMutableUserNotificationAction *action1 = [[UIMutableUserNotificationAction alloc]init];
    // 设置标识符
    action1.identifier = @"foreground";
    // 设置标题
    action1.title = @"我是前台按钮";
    // 设置激活模式
    action1.activationMode = UIUserNotificationActivationModeForeground;

    
    // 创建动作
    UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc]init];
    action2.identifier = @"background";
    action2.title = @"我是后台按钮";
    action2.activationMode = UIUserNotificationActivationModeBackground;
    
    // 设置行为 (iOS 9)
    /*
     UIUserNotificationActionBehaviorDefault
     UIUserNotificationActionBehaviorTextInput  快捷回复
     */
    action2.behavior = UIUserNotificationActionBehaviorTextInput;
    // 设置行为参数
    /*
     UIKIT_EXTERN NSString *const UIUserNotificationTextInputActionButtonTitleKey 设置属性时使用的Key
     
     UIKIT_EXTERN NSString *const UIUserNotificationActionResponseTypedTextKey 接收信息时的Key
     */
    action2.parameters = @{UIUserNotificationTextInputActionButtonTitleKey:@"飞鸽传书"};
    
    // 添加动作
    /*
         UIUserNotificationActionContextDefault  设置该类型 弹窗型最多可以展示6个按钮(4个自定义按钮,2个默认按钮:打开和关闭)
         UIUserNotificationActionContextMinimal  设置该类型 相当于设置iOS7没有自定义按钮的样式(打开和关闭,如果屏幕较小展示不够,可以设置此类型)
     */
    [category setActions:@[action1,action2] forContext:UIUserNotificationActionContextDefault];
    
    UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:[NSSet setWithObjects:category, nil]];
    [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
    
}

@end

AppDelegate

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void (^)())completionHandler{
    
    NSString *inputContent = responseInfo[UIUserNotificationActionResponseTypedTextKey];
    NSLog(@"%@",inputContent);
    
    // 调用回调 更新应用的快照&设置应用的挂起
    completionHandler();
}

你可能感兴趣的:(推送通知-快速回复 - (Obj-C))