- (void)addLocalNotice:(NSString *)titlepush {app
if (@available(iOS 10.0, *)) {ide
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];测试
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];spa
// 标题component
content.title = titlepush;orm
content.subtitle = titlepush;string
// 内容it
content.body = titlepush;io
// 声音table
// 默认声音
// content.sound = [UNNotificationSound defaultSound];
// 添加自定义声音
content.sound = [UNNotificationSound soundNamed:@"Alert_ActivityGoalAttained_Salient_Haptic.caf"];
// 角标 (我这里测试的角标无效,暂时没找到缘由)
content.badge = @1;
// 多少秒后发送,能够将固定的日期转化为时间
NSTimeInterval time = [[NSDate dateWithTimeIntervalSinceNow:10] timeIntervalSinceNow];
// NSTimeInterval time = 10;
// repeats,是否重复,若是重复的话时间必须大于60s,要不会报错
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:time repeats:NO];
/*
//若是想重复可使用这个,按日期
// 周一早上 8:00 上班
NSDateComponents *components = [[NSDateComponents alloc] init];
// 注意,weekday默认是从周日开始
components.weekday = 2;
components.hour = 8;
UNCalendarNotificationTrigger *calendarTrigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:YES];
*/
// 添加通知的标识符,能够用于移除,更新等操做
NSString *identifier = @"noticeId";
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];
[center addNotificationRequest:request withCompletionHandler:^(NSError *_Nullable error) {
NSLog(@"成功添加推送");
}];
}else {
UILocalNotification *notif = [[UILocalNotification alloc] init];
// 发出推送的日期
notif.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
// 推送的内容
notif.alertBody = @"你已经10秒没出现了";
// 能够添加特定信息
notif.userInfo = @{@"noticeId":@"00001"};
// 角标
notif.applicationIconBadgeNumber = 1;
// 提示音
notif.soundName = UILocalNotificationDefaultSoundName;
// 每周循环提醒
notif.repeatInterval = NSCalendarUnitWeekOfYear;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
}
}
NSString *stringInfo=[NSString stringWithFormat:@"APNS error: %@", err];
[self addLocalNotice:stringInfo];