繁体   English   中英

当应用程序在后台时处理本地通知

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM