[英]Handle local notification when app is in background
基本上,我正在安排一個本地通知,以便在特定時間(例如上午 8:00)敲響我的鬧鍾。 現在,我想執行特定任務,例如在應用程序處於后台時播放警報聲,但不點擊我在通知列表中收到的通知橫幅。
我正在使用下面的代碼,但它僅在我點擊通知橫幅並進入應用程序時才有效。
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response:
UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)
{
print("notification recived in background state")
}
請幫助我使用棘手的解決方案來處理我的本地通知,而無需點擊橫幅。
謝謝是提前。
設置放置在項目包中的聲音文件
let arrayOfSoundFile = ["water.caf","hello.mp3"]
let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationStringForKey("Title..", arguments: nil)
content.body = NSString.localizedUserNotificationStringForKey("Text Here...", arguments: nil)
//Custom sound file as par user requriment select file UIPickerView add and select file
content.sound = UNNotificationSound(named: arrayOfSoundFile[0])
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 50, repeats: false)
let identifier = "WakeUp"
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
let center = UNUserNotificationCenter.currentNotificationCenter()
center.addNotificationRequest(request, withCompletionHandler: { (error) in
if error != nil {
print("notification created successfully.")
}
})
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
let userInfo = notification.request.content.userInfo
let dictAps = userInfo["aps"] as! NSDictionary
print(dictAps)
completionHandler([.alert, .badge, .sound])
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.