开发者社区> 问答> 正文

在后台应用程序时从默认通知中心接收通知

在我的应用程序中,我正在获取相机滚动的内容fetchAssets(with:)...为了接收更改消息,我在照片库中注册了我的观察者register(_:)方法。我的观察者正在安慰PHPhotoLibraryChangeObserver协议。因此,当图书馆发生变化时,我应该得到通知。我想要支持的场景是,当我运行我的应用程序时,我去后台,然后打开相机应用程序,拍照,然后回到我的应用程序。当我的应用程序回到前台时,是否有可能得到更改的通知?

展开
收起
游客5akardh5cojhg 2019-12-23 20:51:10 688 0
1 条回答
写回答
取消 提交回答
  • 是的,您可以创建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])
        }
    
    2019-12-23 20:51:33
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载