iOS 9中的UILocalNotification和iOS 10中的UNMutableNotificationContent?(UILocalNotification in iOS 9 and UN

编程入门 行业动态 更新时间:2024-10-28 18:29:03
iOS 9中的UILocalNotification和iOS 10中的UNMutableNotificationContent?(UILocalNotification in iOS 9 and UNMutableNotificationContent in iOS 10?)

我需要向后兼容(iOS 9)到一个项目。 我想出了这个:

if #available(iOS 10.0, *) { let content = UNMutableNotificationContent() } else { // Fallback on earlier versions }

我应该在Fallback中写什么? 我是否需要创建本地通知实例?

I need to give backward compatibility (iOS 9) to a project. I came up with this:

if #available(iOS 10.0, *) { let content = UNMutableNotificationContent() } else { // Fallback on earlier versions }

What should I write in Fallback? Do I need to create a local notification instance?

最满意答案

这是一个支持两个版本的小例子:

Objective-c版本:

if #available(iOS 10.0, *) { UNMutableNotificationContent *objNotificationContent = [[UNMutableNotificationContent alloc] init]; objNotificationContent.body = @"Notifications"; objNotificationContent.badge = @([[UIApplication sharedApplication] applicationIconBadgeNumber] + 1); UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:60 repeats:NO]; UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"identifier" content:objNotificationContent trigger:trigger]; UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { if (!error) { } else { } }]; } else { UILocalNotification *localNotif = [[UILocalNotification alloc] init]; localNotif.fireDate = [[NSDate date] dateByAddingTimeIntervalInterval:60]; localNotif.alertBody = @"Notifications"; localNotif.repeatInterval = NSCalendarUnitMinute; localNotif.applicationIconBadgeNumber = 0; [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; }

Swift版本:

if #available(iOS 10.0, *) { let content = UNMutableNotificationContent() content.categoryIdentifier = "awesomeNotification" content.title = "Notification" content.body = "Body" let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: false) let request = UNNotificationRequest(identifier: "FiveSecond", content: content, trigger: trigger) let center = UNUserNotificationCenter.current() center.add(request) { (error) in } } else { let notification = UILocalNotification() notification.alertBody = "Notification" notification.fireDate = NSDate(timeIntervalSinceNow:60) notification.repeatInterval = NSCalendarUnit.Minute UIApplication.sharedApplication().cancelAllLocalNotifications() UIApplication.sharedApplication().scheduledLocalNotifications = [notification] }

Here is a small example on supporting both version:

Objective-c version:

if #available(iOS 10.0, *) { UNMutableNotificationContent *objNotificationContent = [[UNMutableNotificationContent alloc] init]; objNotificationContent.body = @"Notifications"; objNotificationContent.badge = @([[UIApplication sharedApplication] applicationIconBadgeNumber] + 1); UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:60 repeats:NO]; UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"identifier" content:objNotificationContent trigger:trigger]; UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { if (!error) { } else { } }]; } else { UILocalNotification *localNotif = [[UILocalNotification alloc] init]; localNotif.fireDate = [[NSDate date] dateByAddingTimeIntervalInterval:60]; localNotif.alertBody = @"Notifications"; localNotif.repeatInterval = NSCalendarUnitMinute; localNotif.applicationIconBadgeNumber = 0; [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; }

Swift version:

if #available(iOS 10.0, *) { let content = UNMutableNotificationContent() content.categoryIdentifier = "awesomeNotification" content.title = "Notification" content.body = "Body" let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: false) let request = UNNotificationRequest(identifier: "FiveSecond", content: content, trigger: trigger) let center = UNUserNotificationCenter.current() center.add(request) { (error) in } } else { let notification = UILocalNotification() notification.alertBody = "Notification" notification.fireDate = NSDate(timeIntervalSinceNow:60) notification.repeatInterval = NSCalendarUnit.Minute UIApplication.sharedApplication().cancelAllLocalNotifications() UIApplication.sharedApplication().scheduledLocalNotifications = [notification] }

更多推荐

本文发布于:2023-07-27 15:11:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1292262.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:UILocalNotification   iOS   UNMutableNotificationContent

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!