ios - 在后台应用程序时从默认通知中心接收通知
问题描述
在我的应用程序中,我使用fetchAssets(with:)
. 为了接收更改消息,我已经使用照片库的register(_:)
方法注册了我的观察者。我的观察员对PHPhotoLibraryChangeObserver
协议感到欣慰。所以当图书馆发生变化时,我应该得到通知。我想要支持的场景是在我运行我的应用程序时,我进入后台,然后打开相机应用程序,拍照,然后返回我的应用程序。当我的应用程序在后台时,当它返回到前台时,是否可以获得更改通知?
解决方案
是的,您可以创建 LocalNotificaion 并在应用程序进入后台并返回前台时触发它。
func scheduleNotification(timeInter : TimeInterval) {
let content = UNMutableNotificationContent()
let userActions = "User Actions"
content.title = "Title "
content.body = "Body"
content.sound = UNNotificationSound.init(named:
content.categoryIdentifier = userActions
content.userInfo = ["MID" : RANDOM_ID, "timeInterval" : String(timeInter)]
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: timeInter, repeats: false)
let identifier = String(timeInter)
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
print(request.identifier)
notificationCenter.add(request) { (error) in
if let error = error {
}
}
// let snoozeAction = UNNotificationAction(identifier: "Snooze", title: "Snooze", options: [])
let deleteAction = UNNotificationAction(identifier: "Delete", title: "Delete", options: [.destructive])
let category = UNNotificationCategory(identifier: userActions, actions: [deleteAction], intentIdentifiers: [], options: [])
notificationCenter.setNotificationCategories([category])
}
推荐阅读
- eclipse - 如何理解此 Liclipse (Eclipse) 升级失败错误消息?
- python-3.x - Django 多对一关系将数据添加到多方
- python-3.x - 无法通过“builtin_function_or_method”对象不可下标
- php - 如何在不使用斜杠重定向 301 的情况下修复 API 目标 URL 并将 POST 方法更改为 GET
- azure - 使用 KQL 查询的 App Insights REST 调用返回空
- javascript - 我正在制作一个音乐应用程序,如何更新/计算播放
- blogger - 如何保持切换的共享按钮扩展并在 Blogger 主题版本 2017 中实际工作?
- postgresql - 无法在 Postgres 中计算 percentile_disc、percent_rank 的收入
- python-3.x - 无法在 python3 下安装 pycrypto(使用 pip3),但它在 python[2] 下并行安装
- c# - 在 vscode 中运行 .NET