我在iOS 10
中开发了local Notifications
。它工作得很完美。但现在我应该如何编码local notifications
和push notification
,如果用户使用的是iOS 9
和更高版本。有谁能帮帮忙吗?
以下是iOS 10
中的代码
import UIKit
import UserNotifications
@available(iOS 10.0, *)
class ViewController: UIViewController,UNUserNotificationCenterDelegate {
override func viewDidLoad() {
super.viewDidLoad()
if #available(iOS 10.0, *) {
//Seeking permission of the user to display app notifications
UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.sound,.badge], completionHandler: {didAllow,Error in })
UNUserNotificationCenter.current().delegate = self
}
}
//To display notifications when app is running inforeground
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .sound, .badge])
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func buttonPressed(_ sender: UIButton) {
if #available(iOS 10.0, *) {
//Setting content of the notification
let content = UNMutableNotificationContent()
content.title = "hello"
content.body = "notification pooped out"
content.badge = 1
//Setting time for notification trigger
let date = Date(timeIntervalSinceNow: 10)
var dateCompenents = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: date)
let trigger = UNCalendarNotificationTrigger(dateMatching: dateCompenents, repeats: false)
//Adding Request
let request = UNNotificationRequest(identifier: "timerdone", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
}
}
发布于 2017-07-29 03:50:04
我为Swift 3做了这个类,它有一个请求允许推送通知和发送通知的功能。适用于iOS 9和iOS 10+。
import UIKit
import UserNotifications
class LocalNotification: NSObject, UNUserNotificationCenterDelegate {
class func registerForLocalNotification(on application:UIApplication) {
if (UIApplication.instancesRespond(to: #selector(UIApplication.registerUserNotificationSettings(_:)))) {
let notificationCategory:UIMutableUserNotificationCategory = UIMutableUserNotificationCategory()
notificationCategory.identifier = "NOTIFICATION_CATEGORY"
//registerting for the notification.
application.registerUserNotificationSettings(UIUserNotificationSettings(types:[.sound, .alert, .badge], categories: nil))
}
}
class func dispatchlocalNotification(with title: String, body: String, userInfo: [AnyHashable: Any]? = nil, at date:Date) {
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
let content = UNMutableNotificationContent()
content.title = title
content.body = body
content.categoryIdentifier = "Fechou"
if let info = userInfo {
content.userInfo = info
}
content.sound = UNNotificationSound.default()
let comp = Calendar.current.dateComponents([.hour, .minute], from: date)
let trigger = UNCalendarNotificationTrigger(dateMatching: comp, repeats: true)
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
center.add(request)
} else {
let notification = UILocalNotification()
notification.fireDate = date
notification.alertTitle = title
notification.alertBody = body
if let info = userInfo {
notification.userInfo = info
}
notification.soundName = UILocalNotificationDefaultSoundName
UIApplication.shared.scheduleLocalNotification(notification)
}
print("WILL DISPATCH LOCAL NOTIFICATION AT ", date)
}
}
使用:
您可以在以下位置请求权限:
LocalNotification.registerForLocalNotification(on: UIApplication.shared)
并发送本地通知:
LocalNotification.dispatchlocalNotification(with: "Notification Title for iOS10+", body: "This is the notification body, works on all versions", at: Date().addedBy(minutes: 2))
提示:
您可以将通知设置为在任何未来日期触发,在本例中,我使用日期扩展来获取通知触发的未来日期(分钟)。就是这样:
extension Date {
func addedBy(minutes:Int) -> Date {
return Calendar.current.date(byAdding: .minute, value: minutes, to: self)!
}
}
https://stackoverflow.com/questions/42688760
复制相似问题