在按下本地通知上的按鈕時,我希望能夠根據通知中的某些信息打開特定的視圖控制器。如何才能做到這一點?如何從本地通知打開特定的視圖控制器?
在AppDelegate.swift
let notificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)
if let options = launchOptions {
if let notification = options[UIApplicationLaunchOptionsLocalNotificationKey] as? UILocalNotification {
if let userInfo = notification.userInfo {
let type = userInfo["TYPE"] as! String
// do something neat here
if (type == "SLEEP") {
} else if (type == "STRESS") {
} else {
}
}
}
}
return true
}
在視圖控制器
let sleepInPrefs = prefs.valueForKey(key) as? NSDate
if sleepInPrefs != nil {
print(sleepInPrefs)
print("Setting stress notif")
for notification in (UIApplication.sharedApplication().scheduledLocalNotifications)! {
if (notification.userInfo!["TYPE"]) != nil {
if (notification.userInfo!["TYPE"] as! String == key) {
UIApplication.sharedApplication().cancelLocalNotification(notification)
print("deleting notif")
break
}
}
}
let notification = UILocalNotification()
notification.fireDate = sleepInPrefs
notification.repeatInterval = NSCalendarUnit.Day
notification.timeZone = NSCalendar.currentCalendar().timeZone
notification.alertBody = ""
notification.hasAction = true
notification.alertAction = "open"
notification.soundName = UILocalNotificationDefaultSoundName
notification.userInfo = ["TYPE": key ]
notification.category = "PROMPT"
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
你可以把你嘗試了一些代碼:[UIApplication的registerUserNotificationSettings] -
//在iOS系統8.0和更高版本,應用程序必須使用的用戶通知的註冊? –
我更新了說明。謝謝。 – gohar94
請原諒格式。我正在通過電話進行此操作。 – gohar94