【问题标题】:Notification content extension is never called after receive notification收到通知后从不调用通知内容扩展
【发布时间】:2019-12-18 14:06:55
【问题描述】:

我在一个测试项目中添加了Notification content extension: 这是我的应用委托

import UIKit
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {



func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    registerForPushNotifications()
    addNotification()
    return true
}

// MARK: UISceneSession Lifecycle

func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
    // Called when a new scene session is being created.
    // Use this method to select a configuration to create the new scene with.
    return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}

func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
    // Called when the user discards a scene session.
    // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
    // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}
}


//MARK: - Push notification
extension AppDelegate {

func registerForPushNotifications() {
  UNUserNotificationCenter.current()
    .requestAuthorization(options: [.alert, .sound, .badge]) {
      [weak self] granted, error in

      print("Permission granted: \(granted)")
      guard granted else { return }
      self?.getNotificationSettings()
  }
}


func getNotificationSettings() {
  UNUserNotificationCenter.current().getNotificationSettings { settings in
    print("Notification settings: \(settings)")
    guard settings.authorizationStatus == .authorized else { return }
    DispatchQueue.main.async {
      UIApplication.shared.registerForRemoteNotifications()
    }
  }
}

func application( _ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  let tokenParts = deviceToken.map { data in String(format: "%02.2hhx", data) }
  let token = tokenParts.joined()
  print("Device Token: \(token)")
}

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
  print("Failed to register: \(error)")
}
}

//MARK: - Notifications
extension AppDelegate {

func addNotification(){
    let acceptAction = UNNotificationAction(identifier: "ACCEPT_ACTION",
          title: "Accept",
          options: UNNotificationActionOptions(rawValue: 0))
    let declineAction = UNNotificationAction(identifier: "DECLINE_ACTION",
          title: "Decline",
          options: UNNotificationActionOptions(rawValue: 0))

    let meetingInviteCategory =
          UNNotificationCategory(identifier: "TEST_APN",
          actions: [acceptAction, declineAction],
          intentIdentifiers: [],
          hiddenPreviewsBodyPlaceholder: "",
          options: .customDismissAction)
    // Register the notification type.
    let notificationCenter = UNUserNotificationCenter.current()
    notificationCenter.setNotificationCategories([meetingInviteCategory])
}

}

如您所见,我为通知添加了一个类别,这里是通知内容扩展中的 info.plist。

这是我用来发送通知的有效负载

{"aps":{
         "alert":"Testing.. (31)",
        "badge":1,
        "sound":"default",
        "category": "TEST_APN",
        "mutable-content":"1"
}}

问题是我创建的自定义通知从未被调用,即使在NotificationViewController 中,我设置了许多断点但它们从未调用过。这里有什么问题吗?谢谢

【问题讨论】:

    标签: swift apple-push-notifications


    【解决方案1】:

    NSExtension 的结构应该是这样的

    <key>NSExtension</key>
    <dict>
        <key>NSExtensionAttributes</key>
        <dict>
            <key>UNNotificationExtensionDefaultContentHidden</key>
            <false/>
            <key>UNNotificationExtensionUserInteractionEnabled</key>
            <true/>
            <key>UNNotificationExtensionCategory</key>
            <string>TEST_APN</string>
            <key>UNNotificationExtensionInitialContentSizeRatio</key>
            <real>0.12</real>
        </dict>
        <key>NSExtensionMainStoryboard</key>
        <string>MainInterface</string>
        <key>NSExtensionPointIdentifier</key>
        <string>com.apple.usernotifications.content-extension</string>
    </dict>
    

    【讨论】:

    • 非常感谢,但他们还没有被调用
    猜你喜欢
    • 1970-01-01
    • 2023-04-04
    • 2019-06-29
    • 1970-01-01
    • 2016-11-03
    • 1970-01-01
    • 2018-08-26
    • 1970-01-01
    • 2017-02-24
    相关资源
    最近更新 更多