Ios 在应用程序处于后台时从默认通知中心接收通知
在我的应用程序中,我使用Ios 在应用程序处于后台时从默认通知中心接收通知,ios,swift,background-process,notificationcenter,camera-roll,Ios,Swift,Background Process,Notificationcenter,Camera Roll,在我的应用程序中,我使用fetchAssets(with:)获取相机滚动的内容。为了接收更改消息,我已使用照片库的register(:)方法注册了我的观察者。我的观察者对PHPhotoLibraryChangeObserver协议感到欣慰。所以当图书馆发生变化时,我应该得到通知。我想要支持的场景是,当我运行我的应用程序时,我进入后台,然后打开相机应用程序,拍照,然后返回到我的应用程序。当我的应用程序返回前台时,是否可能在后台收到更改通知 是的,您可以创建LocalNotification,并在应
fetchAssets(with:)
获取相机滚动的内容。为了接收更改消息,我已使用照片库的register(:)
方法注册了我的观察者。我的观察者对PHPhotoLibraryChangeObserver
协议感到欣慰。所以当图书馆发生变化时,我应该得到通知。我想要支持的场景是,当我运行我的应用程序时,我进入后台,然后打开相机应用程序,拍照,然后返回到我的应用程序。当我的应用程序返回前台时,是否可能在后台收到更改通知 是的,您可以创建LocalNotification,并在应用程序进入后台并返回前台时触发它
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])
}
是的,你们可以创建LocalNotification,当应用程序在后台运行,在前台返回时触发它
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])
}
我应该为此请求用户权限吗?我应该为此请求用户权限吗?