- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 1. 注册本地推送通知
// 1.1 注册本地推送通知的设置
/*
UIUserNotificationTypeNone = 0, 没有
UIUserNotificationTypeBadge = 1 << 0, 可以显示应用图标上的数字
UIUserNotificationTypeSound = 1 << 1, 可以播放声音
UIUserNotificationTypeAlert = 1 << 2, 可以弹窗
*/
// 这个就是推送通知面板的类型
UIMutableUserNotificationCategory *category = [UIMutableUserNotificationCategory new];
// 为我们的版面设置一个唯一标示(到通知发送的时候进行选择)
category.identifier = @"mycat1";
// 创建面板的动作按钮
/*
// 动作的唯一标识
@property (nullable, nonatomic, copy) NSString *identifier;
// 标题
@property (nullable, nonatomic, copy) NSString *title;
// 普通按钮
UIUserNotificationActionBehaviorDefault,
// 输入框
UIUserNotificationActionBehaviorTextInput
@property (nonatomic, assign) UIUserNotificationActionBehavior behavior NS_AVAILABLE_IOS(9_0);
为UIUserNotificationActionBehaviorTextInput模式下设置,发送按钮的标题
@property (nonatomic, copy) NSDictionary *parameters NS_AVAILABLE_IOS(9_0);
//
UIUserNotificationActivationModeForeground, // 打开应用
UIUserNotificationActivationModeBackground // 不打开应用
// 激活模式
@property (nonatomic, assign) UIUserNotificationActivationMode activationMode;
// 是否验证,如果要求验证
@property (nonatomic, assign, getter=isAuthenticationRequired) BOOL authenticationRequired;
// 红色是YES,蓝色是NO
@property (nonatomic, assign, getter=isDestructive) BOOL destructive;
*/
UIMutableUserNotificationAction *action1 = [UIMutableUserNotificationAction new];
action1.identifier = @"action1";
action1.title = @"action one";
action1.behavior = UIUserNotificationActionBehaviorDefault;
action1.activationMode = UIUserNotificationActivationModeBackground;
action1.authenticationRequired = NO;
action1.destructive = YES;
UIMutableUserNotificationAction *action2 = [UIMutableUserNotificationAction new];
action2.identifier = @"action2";
action2.title = @"action two";
action2.behavior = UIUserNotificationActionBehaviorDefault;
action2.activationMode = UIUserNotificationActivationModeBackground;
action2.authenticationRequired = YES;
action2.destructive = NO;
// 动作按钮添加到面板类型中
/*
// 默认可以弹出4个
UIUserNotificationActionContextDefault, // the default context of a notification action
// 在空间有限的情况下弹出2个
UIUserNotificationActionContextMinimal // the context of a notification action when space is limited
*/
[category setActions:@[action1,action2] forContext:UIUserNotificationActionContextDefault];
// 创建输入面板样式
UIMutableUserNotificationCategory *inputCat = [UIMutableUserNotificationCategory new];
inputCat.identifier = @"inputcat";
// 创建输入动作
UIMutableUserNotificationAction *inputAction = [UIMutableUserNotificationAction new];
inputAction.identifier = @"inputAction";
inputAction.title = @"action input";
inputAction.behavior = UIUserNotificationActionBehaviorTextInput;
inputAction.activationMode = UIUserNotificationActivationModeBackground;
inputAction.authenticationRequired = NO;
inputAction.destructive = NO;
// 设置按钮
inputAction.parameters = @{UIUserNotificationTextInputActionButtonTitleKey:@"回复"};
[inputCat setActions:@[inputAction] forContext:UIUserNotificationActionContextDefault];
// categories不知道参数作用
UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:[NSSet setWithObjects:category,inputCat, nil]];
[[UIApplication sharedApplication] registerUserNotificationSettings:setting];
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSLog(@"launchOptions:%@",launchOptions);
// 当本地推送通知死了以后,通过通知打开应用时回调
if(launchOptions && launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) {
UILocalNotification *notification = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
// [launchOptions writeToFile:[NSString stringWithFormat:@"%@/launchOptions",path] atomically:YES];
// 用于检测拿过来的通知是我们抛出通知
[notification.userInfo writeToFile:[NSString stringWithFormat:@"%@/launchOptions",path] atomically:YES];
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
// 用于检测是否执行了
// [@"" writeToFile:[NSString stringWithFormat:@"%@/launchOptions",path] atomically:YES encoding:NSUTF8StringEncoding error:nil];
}
return YES;
}
// 程序活着状态下,不管你在前台自动回调,在后台点击通知以后回调
- (void)application:(UIApplication *)application didReceiveLocalNotification:(nonnull UILocalNotification *)notification {
NSLog(@"notification:%@",notification);
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
[@"" writeToFile:[NSString stringWithFormat:@"%@/didReceiveLocalNotification",path] atomically:YES encoding:NSUTF8StringEncoding error:nil];
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
}
// 专门处理本地推送通知面板中的action
- (void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *)identifier forLocalNotification:(nonnull UILocalNotification *)notification completionHandler:(nonnull void (^)())completionHandler {
if([identifier isEqualToString:@"action2"]) {
NSLog(@"我真坏");
} else if([identifier isEqualToString:@"action1"]) {
NSLog(@"你真坏");
}
NSLog(@"%s",__func__);
//必须回调一下
completionHandler();
}
// 专门处理本地推送通知面板中的action(如果这个方法实现了,上面方法将不执行)
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void (^)())completionHandler {
NSLog(@"%s",__func__);
NSLog(@"responseInfo:%@",responseInfo);
//必须回调一下
completionHandler();
}
ViewController中:
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
// 2.1 创建通知
UILocalNotification *notification = [UILocalNotification new];
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
notification.alertTitle = @"孙杨是什么职业";
notification.alertBody = @"孙杨夺冠了";
notification.hasAction = YES;
notification.alertAction = @"萌萌哒";
notification.applicationIconBadgeNumber = 8;
notification.userInfo = @{@"key":@"haha"};
// 设置推送通知面板类型
notification.category = @"inputcat";
// 执行通知
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}