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

查看:598
本文介绍了从推送通知启动时,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:

方法1:

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

方法2:

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登录到控制台,并且尝试触发警报(遵循

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.

任何建议,不胜感激!

推荐答案

我没有找到解决此问题的方法,但是找到了解决方法.我仍然不知道为什么launchOptions总是为零,但是我已经能够通过执行以下操作来访问有效负载:

I didn't find a solution to this issue, but I found a workaround. I still have no idea why launchOptions was always nil, but I've been able to access the payload by doing the following:

在AppDelegate.swift中:

In AppDelegate.swift:

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {

...

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

...

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        let actionIdentifier = response.actionIdentifier

        switch actionIdentifier {
        case UNNotificationDismissActionIdentifier: // Notification was dismissed by user
            // Do something
            completionHandler()
        case UNNotificationDefaultActionIdentifier: // App was opened from notification
            // Do something
            completionHandler()
        default:
            completionHandler()
        }
    }

如果然后在userNotificationCenter中设置一个断点,则可以挖掘出通知负载:

If I then set a breakpoint in userNotificationCenter, I can dig out the notification payload:

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

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