本地推送(使用UserNotifications框架实现)
一、从APNs注册device token:
在app启动流程中调用的didFinishLaunchingWithOptions方法中调用registerForRemoteNotification方法
registerForRemoteNotification方法判断iOS系统版本,调用requestAuthorizationWithOptions
代码如下:
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
// Override point for customization after application launch.
[self registerNotification];
return YES;
}
- (void)registerNotification {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) {
}];
}
方法请求通知授权
二、添加本地通知:
iOS版本10以上:UNUserNotificationCenter创建推送通知中心,声明UNMutableNotificationContent对象
创建通知内容,然后设置本地通知的触发条件,可以用NSTimeInterval设置多久后发送,然后使用UNTimeIntervalNotificationTrigger
触发定时发送,此外还有指定日期时间条件的触发方法UNCalendarNotificationTrigger,以及根据地理区域触发的UNLocationNotificationTrigger方法,[UNNotificationRequest requestWithIdentifier:identifier(NSString对象,这个推送的标签)content:content trigger:trigger]创建一个创建本地推送的请求,addNotificationRequest:request
发送该请求,请求成功则创建成功
iOS版本10以下:UILocalNotification创建本地推送,[[UIApplication sharedApplication]scheduleLocalNotification:notif]使用UIApplication单例添加该推送
代码:
-(void)addLocalNoticeWithID:(NSString*)identifier {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.title=@"Local Push Test";
content.subtitle=@"Subtitle: Damn!";
content.body = @"Do you have freestyle? Skrrr~~~";
content.sound = [UNNotificationSound defaultSound];*
content.badge= @1;
NSTimeInterval time = [[NSDate dateWithTimeIntervalSinceNow:10] timeIntervalSinceNow];
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:time repeats:**NO**];
UNNotificationRequest *request = [UNNotificationRequestrequestWithIdentifier:identifier content:content trigger:trigger];
[center addNotificationRequest:request withCompletionHandler:^(NSError *_Nullable error) {
NSLog(@"Local Push was successfully added!");
}];
}
三、移除某本地推送:
iOS版本10以上:UNUserNotificationCenter获取推送通知中心,getPendingNotificationRequestsWithCompletionHandler方法获取当前所有本地推送的请求,如果存在我们希望移除的identifier则调removePendingNotificationRequestsWithIdentifiers方法删除。removeAllPendingNotificationRequests方法移除全部本地推送
iOS版本10以下:[[UIApplication sharedApplication]scheduledLocalNotifications]获取所有已经添加的本地推送的数组,遍历数组,找到相应的identifier的推送后使用cancelLocalNotification:localNotification方法移除。cancelAllLocalNotifications移除全部本地推送
- (void)removeOneNotificationWithID:(NSString*)noticeId {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center getPendingNotificationRequestsWithCompletionHandler:^(NSArray * _Nonnull requests) {
for(UNNotificationRequest *req in requests){
NSLog(@"The exist IDs:%@\n",req.identifier);
}
NSLog(@"Remove currentID:%@",noticeId);
}];
[center removePendingNotificationRequestsWithIdentifiers:@[noticeId]];
}
四、检查授权情况:
iOS版本10以上:UNUserNotificationCenter获取推送通知中心,getNotificationSettingsWithCompletionHandler获取当前推送设置,若settings.notificationCenterSetting == UNNotificationSettingEnabled则说明推送功能已开启,否则就是关闭状态
iOS版本10以下:直接通过[UIApplication sharedApplication]currentUserNotificationSettings].types获取通知开关状态
- (void)checkUserNotificationEnable {
__block BOOL isOn =NO;
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
if(settings.notificationCenterSetting==UNNotificationSettingEnabled) {
isOn =YES;
NSLog(@"Turned on the notify");
}else{
isOn =NO;
NSLog(@"Turned off the notify");
if(self.delegate&& [self.delegaterespondsToSelector:@selector(showAlertView)]) {
[self.delegate showAlertView];
}
}
}];
}
参考:https://www.jianshu.com/p/0f0dd782fdd5 UserNotifications框架详解
https://www.jianshu.com/p/9b1fa25a0712 iOS 本地推送(Local Push)