iOS开发 iOS10推送必看(基础篇)
https://www.jianshu.com/p/f5337e8f336d
https://github.com/b593771943/XLLLocalNotification_OC
https://blog.csdn.net/u010964503/article/details/52230981
参考例子2:在信用卡日历里面有
AppDelegate
if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0)
{
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!error) {
NSLog(@"推送注册成功");
}
}];
[application registerForRemoteNotifications];
}
- (IBAction)switchTapped:(id)sender {
UISwitch *switcher = (UISwitch *)sender;
if (switcher.on) {
[self scheduleLocalNotification];
return;
}
[self removeLocalNotification];
}
- (void)scheduleLocalNotification {
// 10系统以上
if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0)
{
// 1.设置触发条件
NSDateComponents *components = [[NSDateComponents alloc] init];
// components.weekday = 1;
components.hour = 15;
components.minute = 9;
UNCalendarNotificationTrigger *timeTrigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:YES];
// 2.创建通知内容
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.body = @"我是小明";
content.badge = @([UIApplication sharedApplication].applicationIconBadgeNumber + 1);
content.sound = [UNNotificationSound defaultSound];
NSString *detail = @"其实他是假小明";
content.userInfo = @{
@"detail":detail
};
// 3.通知标识
NSString *requestIdentifier = [NSString stringWithFormat:@"%lf", [[NSDate date] timeIntervalSince1970]];
// 4.创建通知请求,将1,2,3添加到请求中
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:requestIdentifier content:content trigger:timeTrigger];
// 5.将通知请求添加到通知中心
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (!error)
{
NSLog(@"推送已经添加成功");
}
}];
} else { // iOS10以下
[SVProgressHUD showImage:NULL_IMAGE status:@"不支持iOS10.0以下的设备"];
}
}
- (void)removeLocalNotification {
UIApplication *app = [UIApplication sharedApplication];
app.applicationIconBadgeNumber = 0;
[app cancelAllLocalNotifications];
}