简体   繁体   English

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

[英]Handle local notification when app is in background

Basiccally I'm scheduling a local notification for ringing my alarm at particular time say 8:00 Am.基本上,我正在安排一个本地通知,以便在特定时间(例如上午 8:00)敲响我的闹钟。 NOw I want to perform a specific task like play sound of alarm when app is in background but without tapping on the notification banner which I recived in the notification list.现在,我想执行特定任务,例如在应用程序处于后台时播放警报声,但不点击我在通知列表中收到的通知横幅。

I'm using below code, but it works only when i tap the notification banner and get into the app.我正在使用下面的代码,但它仅在我点击通知横幅并进入应用程序时才有效。

 func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: 
 UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)
 {
   print("notification recived in background state")
 }

Please help me with the tricky solution to handle my local notification without tapping the banner.请帮助我使用棘手的解决方案来处理我的本地通知,而无需点击横幅。

Thanks is Advance.谢谢是提前。

Setting a sound file which is placed in project bundle设置放置在项目包中的声音文件

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.

相关问题 在应用程序处于后台状态时获取本地通知? - Get the local notification when the app is in background state? 无法访问时本地通知,并且应用程序在后台 - local notification when not reachable and app in background 如果应用程序从后台清除,如何处理本地通知 - How to handle local notification if the app is cleared from background 在后台处理本地通知并暂停 - Handle local notification in background and suspended 应用终止后无法处理本地通知 - Unable to Handle Local Notification when app has been terminated 当应用程序终止时处理点击本地通知 - iOS - Swift - Handle tap on local notification when app is terminated - iOS - Swift 如何在应用程序处于后台但未暂停时处理推送通知 - How to handle push notification when app is in background but not suspended 当应用程序在后台启动并发出本地通知时执行代码 - Execute code when app in background and a Local Notification fires Swift - 在后台应用程序中接收本地通知时执行功能 - Swift - Execute function when receive Local Notification in app in background 从iOS通知面板激活应用程序时,是否可以处理所选的本地通知? - Is it possible to handle the selected local notification when activing the app from iOS notification panel?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM