标签: serviceextension

UNNotificationServiceExtension偶尔无法更新通知

我有一个UNNotificationServiceExtension用Swift写的.它所做的一切:

  • 设置通知标题
  • 设置通知正文
  • 加载图片和通话 contentHandler ()

这是我正在做的简短版本:

override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
    self.contentHandler = contentHandler
    self.bestAttemptContent = request.content.mutableCopy() as? UNMutableNotificationContent

    bestAttemptContent!.title = GetNotificationTitle(userInfo)
    bestAttemptContent!.body = GetNotificationBody(userInfo)

    if let imageUrl = <get image url> {
        let imageLoaderTask = URLSession.shared.dataTask(with: URL.init(string: imageUrl)!) { (newsImageData, newsImageUrl, newsImageError) -> Void in
            if newsImageError == nil && newsImageData != nil {
                let documentDirectory = self.GetDocumentDirectory()
                if documentDirectory != nil {
                    let newsFileURL = documentDirectory?.appendingPathComponent("news_image").appendingPathExtension("png")
                    do { …
Run Code Online (Sandbox Code Playgroud)

iphone ios swift ios10 serviceextension

7
推荐指数
1
解决办法
637
查看次数

本地通知的通知服务扩展

系统会加载通知服务扩展并didReceive(_:withContentHandler:)在iOS 10中调用它以获取本地通知吗?如果是,我们怎么做?

notifications uilocalnotification swift ios10 serviceextension

7
推荐指数
2
解决办法
3205
查看次数

iOS 10服务扩展不一致

虽然这里与另一个问题有相似之处: iOS 10不会调用Notification Service Extension

我希望提出一个具体的不当行为的具体问题.考虑以下推送负载:

{  
   "service-alert":{  
       "title":"[Service]Title",
       "subtitle":"[Service]Subtitle",
       "body":"[Service]Body may be substituted."
   },
   "image":"https://some.small/thumbnail.jpg",
   "aps":{  
       "badge":1,
       "alert":{  
          "title":"[Default]Title",
          "subtitle":"[Default]Subtitle",
          "body":"[Default]A simple body of text."
       },
       "sound":"default",
       "mutable-content":1
   }
}
Run Code Online (Sandbox Code Playgroud)

("service-alert"字典用于测试支持它的设备的默认警报文本上的覆盖)

我正在我的iOS 10设备(iPhone 6+)上调试Xcode,我在服务扩展中设置了一个断点,以验证它是否有机会在呈现之前修改推送.

当我发送推送时,有时会触发服务扩展(断点命中),我可以验证推送有效负载是否正确,并且显示的推送仅显示默认的"aps"有效负载.没有图像.没有修改过的标题/副标题/正文.

我的断点serviceExtensionTimeWillExpire没有被击中.通知机制似乎"正常"但结果不正确.

当我再次发送完全相同的推送时,它可以工作.
图像从"图像"URL加载,修改后的"服务警报"文本按设计显示.有时我必须在它工作之前发送相同的推动2或3次,但之后它似乎是一致的.因此,如果我看到修改过的通知一次,那么从每一次推送测试的那一点看起来都能正常工作.

我觉得,但是还没有确定的数据支持,一段时间不活动会导致服务扩展被刷新,并且在被调用时唤醒太慢.如果是这样,这会使它变得毫无用处,因为当收到推送时它永远不会完全清醒.

目前的环境:

  • Xcode 8.2 beta(8C30a)
  • iOS 10.2测试2,3和4
  • 调试方案

任何人都可以在iOS 10测试版和/或非测试版上确认/拒绝此行为吗?我是否发现了10.2错误,或者这是"正常行为?"

iphone xcode apple-push-notifications ios serviceextension

6
推荐指数
1
解决办法
520
查看次数

如何从NotificationServiceExtension将远程通知数据保存到Core Data(数据库)中

我在目标应用程序中实现了NotificationServiceExtension,它运行正常.

  • 创建了AppgroupID

     lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? = {
    
         var coordinator: NSPersistentStoreCoordinator? = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
         let options = [
             NSMigratePersistentStoresAutomaticallyOption: true,
             NSInferMappingModelAutomaticallyOption: true
             ]
    
         let oldStoreUrl = self.applicationDocumentsDirectory.appendingPathComponent("Model.sqlite")
         let directory: NSURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: AppGroupID)! as NSURL
         let newStoreUrl = directory.appendingPathComponent("Model.sqlite")!
    
         var targetUrl : URL? = nil
         var needMigrate = false
         var needDeleteOld = false
    
    
         if FileManager.default.fileExists(atPath: oldStoreUrl.path){
            needMigrate = true
            targetUrl = oldStoreUrl
         }
         if FileManager.default.fileExists(atPath: newStoreUrl.path){
            needMigrate = false
            targetUrl = newStoreUrl
    
            if FileManager.default.fileExists(atPath: oldStoreUrl.path){
                needDeleteOld = true …
    Run Code Online (Sandbox Code Playgroud)

notifications core-data ios swift serviceextension

6
推荐指数
1
解决办法
949
查看次数

didReceiveNotificationRequest:withContentHandler - 执行时间限制?

UNNotificationServiceExtension有一种方法 didReceiveNotificationRequest:withContentHandler来处理通知更改。

它有一个非常模糊的描述,关于超时时间:

该方法执行其任务和执行提供的完成块的时间有限。如果您的方法没有及时完成,

这是serviceExtensionTimeWillExpire方法的描述:

如果你的 didReceiveNotificationRequest:withContentHandler: 方法需要很长时间来执行它的完成块,系统会在一个单独的线程上调用这个方法,给你最后一次执行块的机会。

我不完全明白,“渴望执行”是什么意思。有量化数据吗?它是否因运行的设备等而异?

谢谢!

ios swift ios10 serviceextension

5
推荐指数
1
解决办法
1052
查看次数

iOS:如何从其UNNotificationServiceExtension中获取应用程序的已保存数据?

有没有办法从Notification-Service-Extension实例中获取保存在iOS应用程序Sandbox中的数据?我想在传递contenhandler之前从数据库中获取一些数据.

我从里面测试过

    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
            ...
            print("NotificationService didReceive UNNotificationRequest, contentHandler: \(contentHandler)")

            let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
            let filePath = "\(documentsPath)/Database.db"

            print("  documentsPath: \(filePath)")

            let fileManager = FileManager.default
            var isDir : ObjCBool = false
            if fileManager.fileExists(atPath: filePath, isDirectory:&isDir) {
                if isDir.boolValue {
                    print("  file exists and is a directory")

                } else {
                    print("  file exists and is a NOT a directory")
                }
            } else {
                print("file does not exist")
            }...
Run Code Online (Sandbox Code Playgroud)

对我来说,看起来扩展程序有自己的Sandbox和自己的文档文件夹. …

ios ios10 serviceextension

5
推荐指数
1
解决办法
995
查看次数

Notificaton服务扩展无法正常工作

当我发送mutable-content时,没有显示通知:1,推送有效负载既不会到达Notification服务扩展中的断点,但是如果没有显示可变内容推送,Notification内容扩展也正常工作.我没有修改Notification服务扩展中的代码,它是xcode生成的默认代码.在创建通知服务扩展时我是否遗漏了某些内容,或者可能是设备设置的问题.我几天前在同一台设备上进行了测试,通知服务扩展工作正常.

以下是我的服务扩展代码

import UserNotifications

class NotificationService: UNNotificationServiceExtension {

    var contentHandler: ((UNNotificationContent) -> Void)?
    var bestAttemptContent: UNMutableNotificationContent?

    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
        self.contentHandler = contentHandler
        bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)

        if let bestAttemptContent = bestAttemptContent {
            // Modify the notification content here...
            bestAttemptContent.title = "\(bestAttemptContent.title) [modified]"

           contentHandler(bestAttemptContent)
        }
    }

    override func serviceExtensionTimeWillExpire() {
        // Called just before the extension will be terminated by the system.
        // Use this as an opportunity to deliver your …
Run Code Online (Sandbox Code Playgroud)

notifications apple-push-notifications ios ios10 serviceextension

3
推荐指数
2
解决办法
4470
查看次数

如何在iOS 10之前使用UNNotificationServiceExtension运行应用程序?

我的应用程序实现了新的iOS 10富推送NotificationService扩展.

一切都按预期在iOS 10上运行,但我也想支持iOS 10之前的设备 - 当然不是丰富的推送,而只是常规推送.将Xcode中的部署目标降低到例如8.0或9.0并尝试在较旧的模拟器或设备上运行时,会出现以下错误:

Simulator: The operation couldn’t be completed. (LaunchServicesError error 0.)
Device: This app contains an app extension that specifies an extension point identifier that is not supported on this version of iOS for the value of the NSExtensionPointIdentifier key in its Info.plist.
Run Code Online (Sandbox Code Playgroud)

Apple没有正式发现任何东西,说明你的应用程序只能在iOS 10+上运行一旦添加服务扩展 - 有人可以确认吗?

iphone apple-push-notifications ios richpush serviceextension

1
推荐指数
1
解决办法
2112
查看次数