概述
iOS中的通知包括本地推送通知和远程推送通知,两者在iOS系统中都可以通过弹出横幅的形式来提醒用户,点击横幅会打开应用。在iOS 10及之后版本的系统中,还支持通知扩展功能(UNNotificationServiceExtension、UNNotificationContentExtension),下面就来详细介绍iOS推送通知的相关功能及操作。
一、本地推送通知
本地推送通知是由本地应用触发的,是基于时间的通知形式,一般用于闹钟定时、待办事项等提醒功能。发送本地推送通知的大体步骤如下:
注册本地通知;
创建本地通知相关变量,并初始化;
设置处理通知的时间fireDate;
设置通知的内容:通知标题、通知声音、图标数字等;
设置通知传递的参数userInfo,该字典内容可自定义(可选);
添加这个本地通知到UNUserNotificationCenter。
1. 注册本地推送通知
- (void)sendLocalNotification {
NSString *title = @"通知-title";
NSString *sutitle = @"通知-subtitle";
NSString *body = @"通知-body";
NSInteger badge = 1;
NSInteger timeInteval = 5;
NSDictionary *userInfo = @{@"id": @"LOCAL_NOTIFY_SCHEDULE_ID"};
if (@available(iOS 10.0, *)) {
// 1.创建通知内容
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
[content setValue:@(YES) forKeyPath:@"shouldAlwaysAlertWhileAppIsForeground"];
content.sound = [UNNotificationSound defaultSound];
content.title = title;
content.subtitle = subtitle;
content.body = body;
content.badge = @(badge);
content.userInfo = userInfo;
// 2.设置通知附件内容
NSError *error = nil;
NSString *path = [[NSBundle mainBundle] pathForResource:@"logo_img_02@2x" ofType:@"png"];
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";
// 3.设置声音
UNNotificationSound *sound = [UNNotificationSound soundNamed:@"sound01.wav"];// [UNNotificationSound defaultSound];
content.sound = sound;
// 4.触发模式
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:timeInteval repeats:NO];
// 5.设置UNNotificationRequest
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:LocalNotiReqIdentifer content:content trigger:trigger];
// 6.把通知加到UNUserNotificationCenter, 到指定触发点会被触发
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
}];
} else {
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
// 1.设置触发时间(如果要立即触发,无需设置)
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
// 2.设置通知标题
localNotification.alertBody = title;
// 3.设置通知动作按钮的标题
localNotification.alertAction = @"查看";
// 4.设置提醒的声音
localNotification.soundName = @"sound01.wav";// UILocalNotificationDefaultSoundName;
// 5.设置通知的 传递的userInfo
localNotification.userInfo = userInfo;
// 6.在规定的日期触发通知
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
// 7.立即触发一个通知
//[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
}
}
2. 取消本地推送通知
- (void)cancelLocalNotificaitons {
// 取消一个特定的通知
NSArray *notificaitons = [[UIApplication sharedApplication] scheduledLocalNotifications];
// 获取当前所有的本地通知
if (!notificaitons || notificaitons.count <= 0) { return; }
for (UILocalNotification *notify in notificaitons) {
if ([[notify.userInfo objectForKey:@"id"] isEqualToString:@"LOCAL_NOTIFY_SCHEDULE_ID"]) {
if (@available(iOS 10.0, *)) {
[[UNUserNotificationCenter currentNotificationCenter] removePendingNotificationRequestsWithIdentifiers:@[LocalNotiReqIdentifer]];
} else {
[[UIApplication sharedApplication] cancelLocalNotification:notify];
}
break;
}
}
// 取消所有的本地通知
//[[UIApplication sharedApplication] cancelAllLocalNotifications];
}
3. AppDelegate中的回调方法
在上面的代码中我们设置了userInfo,在iOS中收到并点击通知,则会自动打开应用。但是在不同版本的iOS系统中回调方式有所差异,如下:
系统版本 < iOS 10