从推送通知启动时,launchOptions 始终为零 [英] launchOptions always nil when launching from a push notification

查看:26
本文介绍了从推送通知启动时,launchOptions 始终为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 Django 应用程序发送推送通知(使用 django-push-notifications) 到 iOS 应用程序.该应用程序面向 iOS 13,我在运行 iOS 13.3.1 的 iPhone 7 上运行它.我正在 Xcode 11.3.1 中调试

I'm sending push notifications from a Django app (using django-push-notifications) to an iOS app. The app targets iOS 13 and I'm running it on an iPhone 7 running iOS 13.3.1. I'm debugging in Xcode 11.3.1

我正在尝试两种不同的方法来从 Django 端发送通知:

I'm trying two different methods to send the notification from the Django side:

方法一:

devices.send_message(message={"title" : title, "body" : message}, thread_id="events", extra={"foo": "bar"})

方法二:

devices.send_message("[will be overwritten]", extra={
    "aps": {
        "alert": {
            "title": "Bold text in the notification",
            "body": "Second line in the notification"
        },
        "sound": "default",
    },
    "foo": "bar"
})

据我所知,这两种方法都应该产生类似于方法 2 的负载.

As far as I can tell, both methods should result in a payload which looks like Method 2.

我正在通过执行以下操作进行调试:

I'm debugging by doing the following:

  1. 在我的设备方案中设置等待可执行文件启动"
  2. 在 Xcode 中构建和运行
  3. 确保应用程序已在任务切换器中被杀死
  4. 触发发送远程通知
  5. 点按收到的通知以启动应用

无论我做什么,launchOptions 始终为零.我试过设置断点来检查变量.如果 launchOptions 不为零,我尝试使用 os_log 登录到控制台,并且我尝试触发警报(遵循来自 这个问题) 以排除 Xcode 调试器干扰.它始终为零.

No matter what I do, launchOptions is always nil. I've tried setting a breakpoint to inspect the variables. I've tried using os_log to log to the console if launchOptions is not nil, and I've tried triggering an alert (following advice from this question) to rule out Xcode debugger interference. It's always nil.

我的 AppDelegate 目前看起来像这样:

My AppDelegate currently looks like this:

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

    let notificationOption = launchOptions?[.remoteNotification]

    let alert = UIAlertController(title: "Your title", message: notificationOption.debugDescription, preferredStyle: .alert)
    let cancel = UIAlertAction(title: "Cancel", style: .default, handler: { action in
    })
    alert.addAction(cancel)
    DispatchQueue.main.async(execute: {
        application.windows.first!.rootViewController?.present(alert, animated: true, completion: nil)

    })
    return true
}

警报触发,但警报内容只是显示nil".

The alert triggers, but the alert content simply reads "nil".

我无法弄清楚缺少什么.我的通知有效负载可能不是我认为的那样(我已经在 Github 页面上询问 django-push-notifications 以确认这方面是否存在问题).也有可能我错过了设置远程通知的一步,但我确实可靠地收到了通知,并且它们按我的预期显示,因此它们似乎可以正常工作.

I can't figure out what's missing. It's possible that my notification payload isn't exactly what I think it is (I've asked on the Github page for django-push-notifications to confirm if there's an issue on that end). It's also possible I've missed a step in setting up remote notifications, but I do reliably receive the notifications and they display as I expect, so they seem to be working.

非常感谢任何建议!

推荐答案

在 iOS 13.0 中,当应用程序被杀死时,如果您点击通知,想打开应用程序并获取通知负载.这是您的操作方法.

In iOS 13.0 When the app is killed, if you tap on notification, would like to open the app and get hold of notification payload. Here is how you do it.

请检查sceneDelegate下的connectOptions

Please check for connectOptions under sceneDelegate

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
//look for remote notification response
   if let response = connectionOptions.notificationResponse{
        print(response.notification.request.content.userInfo)
   }

   guard let _ = (scene as? UIWindowScene) else { return }
} 

这篇关于从推送通知启动时,launchOptions 始终为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆