一、本地消息推送
1、在AppDelegate.m 中注册通知
//注册通知
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
//请求获取通知权限(角标,声音,弹框)
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
//获取用户是否同意开启通知
NSLog(@"request authorization successed!");
}
}];
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
NSLog(@"%@",settings);
}];
2、在需要添加本地通知的页面添加头文件和通知的协议:
#import <UserNotifications/UserNotifications.h>
@interface MessagePushVC ()<UNUserNotificationCenterDelegate>
@end
3、在viewDidLoad方法中添加:
[[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];
4、添加消息内容
//文本消息通知
- (void)textLocationNotification
{
//第二步:新建通知内容对象
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.title = @"标题";
content.subtitle = @"副标题";
content.body = @"通知的内容";
content.badge = @1;
UNNotificationSound *sound = [UNNotificationSound defaultSound]; //系统默认的提示声音
//UNNotificationSound *sound = [UNNotificationSound soundNamed:@"caodi.m4a"]; //自定义提示声音
content.sound = sound;
//通知触发机制。(重复提醒,时间间隔要大于60s)
UNTimeIntervalNotificationTrigger *trigger1 = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO];
//创建UNNotificationRequest通知请求对象
NSString *requertIdentifier = @"RequestIdentifier1";
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:requertIdentifier content:content trigger:trigger1];
//将通知加到通知中心
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
NSLog(@"Error:%@",error);
}];
}
//文本+图片消息通知
- (void)textAndImageLocationNotification
{
NSString *imageFile = [[NSBundle mainBundle] pathForResource:@"avator_smaller" ofType:@"jpg"];
UNNotificationAttachment *imageAttachment = [UNNotificationAttachment attachmentWithIdentifier:@"iamgeAttachment" URL:[NSURL fileURLWithPath:imageFile] options:nil error:nil];
//第二步:新建通知内容对象
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.title = @"标题";
content.subtitle = @"副标题";
content.body = @"通知的内容";
content.badge = @1;
content.attachments = @[imageAttachment];//虽然是数组,但是添加多个只能显示第一个
UNNotificationSound *sound = [UNNotificationSound defaultSound]; //系统默认的提示声音
//UNNotificationSound *sound = [UNNotificationSound soundNamed:@"caodi.m4a"]; //自定义提示声音
content.sound = sound;
//通知触发机制。(重复提醒,时间间隔要大于60s)
UNTimeIntervalNotificationTrigger *trigger1 = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO];
//创建UNNotificationRequest通知请求对象
NSString *requertIdentifier = @"RequestIdentifier2";
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:requertIdentifier content:content trigger:trigger1];
//将通知加到通知中心
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
NSLog(@"Error:%@",error);
}];
}
//只有当前处于前台才会走,加上返回方法,使在前台显示信息
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
completionHandler(UNNotificationPresentationOptionBadge|
UNNotificationPresentationOptionSound|
UNNotificationPresentationOptionAlert);
}