首页 > 解决方案 > 在后台应用程序时从默认通知中心接收通知

问题描述

在我的应用程序中,我使用fetchAssets(with:). 为了接收更改消息,我已经使用照片库的register(_:)方法注册了我的观察者。我的观察员对PHPhotoLibraryChangeObserver协议感到欣慰。所以当图书馆发生变化时,我应该得到通知。我想要支持的场景是在我运行我的应用程序时,我进入后台,然后打开相机应用程序,拍照,然后返回我的应用程序。当我的应用程序在后台时,当它返回到前台时,是否可以获得更改通知?

标签: iosswiftbackground-processnotificationcentercamera-roll

解决方案


是的,您可以创建 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])
    }

推荐阅读