IOS:应用内通知 + 通知服务 + 内容扩展

IOS: Inapp notification + Notification Service + Content extensions

问题是通知服务扩展本身是无用的,只有一件事可以在应用程序被终止时显示。因此,通过应用内通知和内容扩展,我可以显示自定义通知,但只有当应用不是 killed/force-closed 时才会显示该通知。 问题:如果有内容和服务通知扩展,如何管理应用内通知,以及如何强制通知服务扩展到 call/wake 通知内容扩展。

似乎我需要清理项目并删除应用程序并重新安装。这是 FCM 实现这种情况的完整步骤,也许有些步骤是多余的,但我不想在它工作时触摸它:

应用程序是 killed/closed:显示自定义内容视图的通知 应用程序在后台:通知出现在自定义内容视图中 应用程序在前台:通知静默到达

  1. 服务器端的通知应该是这样的

    { "notification"://必填项 { "data":{}, "body":""//似乎也是强制性的,无论如何你可以在服务扩展中更改它 } "content_available":true,//必填 "mutable_content":true,//必填 "to":""//必填 }

  2. 创建通知服务扩展目标。

  3. 在 NSExtension 下的 info.plist 中,将 UNNotificationExtensionCategory 添加为 Array 并添加一个名称任意的类别,但在任何地方都使用相同的类别。
  4. 在 Notification Service Extension 目标中,确保一切都与主目标中的相同(swift 版本、构建版本、部署目标、设备)。
  5. 在功能中添加应用程序组(应与主要目标中的相同)。
  6. 在didReceive的NotificationService中获取可变内容

    bestAttemptContent = (request.content.mutableCopy() as?UNMutableNotificationContent)

  7. 将您的类别添加到内容中

    bestAttemptContent.categoryIdentifier = "yourCategory"

  8. 调用 contentHandler(bestAttemptContent) 后出现标准通知。

  9. 创建通知内容扩展目标。

  10. 在 NSExtension 下的 info.plist 中,将 UNNotificationExtensionCategory 添加为数组,并添加一个与您已经为服务创建的名称相同的类别。
  11. 根据您的设计更改MainInterface.storyboard中的设计。
  12. 在 NotificationViewController 中,自定义数据将在 notification.request.content.userInfo["gcm.notification.data"] 中。所以在 NotificationViewController 中用数据填充视图。不要忘记 preferredContentSize.
  13. 在功能中添加应用程序组(应与主要目标中的相同)。
  14. 主要目标AppDelegate中实现UNUserNotificationCenterDelegate.
  15. didFinishLaunchingWithOptions中添加

    UNUserNotificationCenter.current().delegate = self UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .alert, .sound]) { (granted, error) in } application.registerForRemoteNotifications()

  16. 在AppDelegate中添加

    func application(_application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken:数据){ Messaging.messaging().apnsToken = deviceToken }

  17. 在 userNotificationCenter willPresent 检查你的应用是否运行。我是这样做的:

    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 如果 self.window?.rootViewController == nil { completionHandler([.badge, .alert, .sound]) } UIApplication.shared.applicationIconBadgeNumber = UIApplication.shared.applicationIconBadgeNumber + 1 }

  18. 在此处处理静默通知:

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)

    1. 在此处处理通知上的点击:

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)

这里可以用同样的方法获取自定义数据response.notification.request.content.userInfo["gcm.notification.data"]

  1. 如果你有异步网络调用加载 data/images 不要忘记实现 func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void)

似乎就这些了:)。啊,扩展正在工作 IOS 10。如果有遗漏或多余的地方,请纠正我。