准备写一下代码。
10.0版本的实现及点击处理逻辑
10.0之前版本的实现及点击处理逻辑
if (IOS10_OR_LATER) {
// 1.创建通知内容
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.title = @"我的测试";
content.subtitle = @"测试通知";
content.body = @"来自TR-App";
content.badge = @1;
NSError *error = nil;
NSString *path = [[NSBundle mainBundle] pathForResource:@"default_avtar_image" ofType:@"png"];
// 2.设置通知附件内容
UNNotificationAttachment *att = [UNNotificationAttachment attachmentWithIdentifier:@"att1" URL:[NSURL fileURLWithPath:path] options:nil error:&error];
if (error) {
NSLog(@"attachment error %@", error);
}
content.attachments = @[att];
content.launchImageName = @"icon_certification_status1@2x";
// 2.设置声音
UNNotificationSound *sound = [UNNotificationSound defaultSound];
content.sound = sound;
// 3.触发模式
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO];
// 4.发送请求标识符
NSString *requestIdentifer = @"TestRequest";
// 5.创建一个发送请求
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:requestIdentifer content:content trigger:trigger];
// 6.把通知加到UNUserNotificationCenter, 到指定触发点会被触发 [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
}];
// 7.移除通知
// [[UNUserNotificationCenter currentNotificationCenter] removeDeliveredNotificationsWithIdentifiers:@[@"TestRequest"]];
// 8.更新一个通知 添加一个相同请求标识的通知,就会更新最开始这个标识的通知
// 代码省略就是创建一个新的通知标识符相同
} else {
NSDictionary *userInfo = @{@"aps": @"我的", @"alert": @"我的内容"};
UILocalNotification *notification = [[UILocalNotification alloc] init];
// 通知内容
notification.alertBody = userInfo[@"alert"];
// 通知被触发时播放的声音
notification.soundName = UILocalNotificationDefaultSoundName;
// 通知参数
notification.userInfo = userInfo;
// 通知重复提示的单位,可以是天、周、月
notification.repeatInterval = 0;//不重复
// 立刻执行通知注册
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}
网友评论