【问题标题】:UNUserNotificationCenter didReceive / willPresent not triggered ios 11UNUserNotificationCenter didReceive / willPresent未触发ios 11
【发布时间】:2019-07-09 13:26:01
【问题描述】:

我有一个可以正常工作的应用程序,其中的通知显示效果很好。现在我想处理一些通知事件,比如用户点击横幅时。

我的 iOS 部署目标是 11.0,与我的部署目标相同。

我在我的 AppDelegate.swift 文件中实现了所有内容:

    class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, RCTBridgeDelegate {
var window: UIWindow?
  var didFinishLaunching: Bool = false

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {

    let bridge = RCTBridge(delegate: self, launchOptions: launchOptions)
    let rootView = RCTRootView(bridge: bridge, moduleName: "root", initialProperties: nil)
    rootView?.backgroundColor = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1)

    self.window = UIWindow(frame: UIScreen.main.bounds)

    let rootViewController = UIViewController()
    rootViewController.view = rootView
    self.window?.rootViewController = rootViewController
    self.window?.makeKeyAndVisible()

    didFinishLaunching = true

    Fabric.with([Crashlytics.self])

    FirebaseApp.configure()


    // This block is necessary to ask user authorization to receive notification
    if #available(iOS 10.0, *) {
      let center = UNUserNotificationCenter.current()
      center.delegate = self
      center.requestAuthorization(options: [.badge, .sound, .alert], completionHandler: {(grant, error)  in
        if error == nil {
          if grant {
            print("### permission granted")
            application.registerForRemoteNotifications()
          } else {
            //User didn't grant permission
          }
        } else {
          print("error: ",error)
        }
      })
    } else {
      // Fallback on earlier versions
      let notificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
      application.registerUserNotificationSettings(notificationSettings)
    }

    self.firstLaunchAction()
    self.initTracker()

    RNSplashScreen.showSplash("LaunchScreen", inRootView: rootViewController.view)

    return true
  }



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

  func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    print("### Failed to register for remote notifications with error: \(error)")
  }

  func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
            if response.actionIdentifier == "like" {
                  print("### Handle like action identifier")
              } else if response.actionIdentifier == "save" {
                  print("### Handle save action identifier")
              } else {
                  print("### No custom action identifiers chosen")
              }
            // Make sure completionHandler method is at the bottom of this func
            completionHandler()
        }

如您所见,我使用 Firebase 发送远程通知,所以我在 json 中有一个像这样的有效负载:

return {
        notification: {
            title,
            body
        },
        data: {
            title,
            body,
            ...data
        },
        android: {
            ttl: 3600 * 1000,
            notification: {
                icon: 'stock_ticker_update',
                color: '#002559'
            }
        },
        apns: {
            payload: {
                aps: {
                    alert: {
                        title: 'NITL ACUMEN BI RFS CODA Dev Offshore',
                        body: 'Send a Message - PN testing group'
                    },
                    sound: 'default'
                }
            }
        },
        condition
    };

我的大部分代码都是从网站上复制的。但我的实际行为是,当我点击通知横幅时,什么也没有发生......

我看到通知横幅,所以我认为我的 APNs 注册没有问题。我不知道为什么 didReceive 没有在点击事件上触发。

我在实施过程中是否遗漏了什么?我的通知负载错误?

有人可以帮我找出我的错误在哪里吗?我已经阅读了无数没有结果的教程。一些帮助将不胜感激谢谢:)

【问题讨论】:

  • 你能尝试两件事并告诉我结果吗? 1 - 忽略操作,只需在func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) 中的rootViewController 上显示UIAlertController 2 - 您可以使用PusherKnuff 之类的工具来发送推送通知,而不是为了测试而使用Firebase。它将发送一个更简单的有效载荷。让我知道结果。
  • 也是在说action,是不是缺少通知action注册步骤? developer.apple.com/documentation/usernotifications/…
  • 根据你的链接,我需要动作注册吗?我只想处理点击通知。我已经输入了UIAlertController inside the func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void),但没有任何显示。我正在尝试将 Knuff 配置为在没有 Firebase atm 的情况下发送通知
  • 尝试使用Knuff,保持应用程序运行,不要退出应用程序,只需放入后台,将配音器放入此功能并尝试点击通知。如果达到调试器点,请告诉我。
  • 其实这是一个私人项目...我找出问题的原因。当我的 rootViewController 被挂载以绑定我的 Javascript 包时,因为这个项目使用 React-Native。如果我评论 rootViewController.view = rootView 一切正常。所以我认为我的 Javascript 部分有问题...

标签: ios swift firebase push-notification


【解决方案1】:

要支持后台更新通知,请确保负载的 aps 字典包含值为 1 的 content-available 键。

{
   "aps" : {
       "content-available" : 1,
       "sound" : “default"
       ....
   },
   ....
}

当后台更新通知发送到用户的设备时,iOS 会在后台唤醒您的应用,并让其运行最多 30 秒。在 iOS 中,系统通过调用应用代理的 application: didReceiveRemoteNotification: fetchCompletionHandler: 方法来传递后台更新通知。

参考:Creating the Remote Notification Payload

【讨论】:

  • 谢谢你的回答,我已经看到了,我也试过了。我已阅读 content-available 键将远程通知变为静默远程通知。您能否解释一下有什么区别以及为什么“简单”远程通知不会触发 didReceive。方法 ?我只想处理点击事件
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-17
  • 2019-01-14
  • 1970-01-01
相关资源
最近更新 更多