ios - 当应用程序在后台时处理本地通知
问题描述
基本上,我正在安排一个本地通知,以便在特定时间(例如上午 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])
}
推荐阅读
- google-cloud-platform - 在 Google Cloud Platform 中使用 GKE 的多区域永久性磁盘
- d3.js - D3强制布局:旋转文字
- php - 使用 php 合并和更改 CSV 文件中的表名
- macos - macOS Catalina 上的 Vagrant 出现“过时文件句柄”错误
- mysql - mysqldump不写文件但没有错误
- python - 如何从 MS Windows 10 卸载 Django?
- java - 如何在 dao 类中制作通用接口
- bash - 在 TMUX 会话中修复 IEX 中的箭头键
- typescript - 如何获取 TypeScript 类的静态属性的类型?
- amcharts - amchar 4 自定义轴破坏形状