- (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];