Spi*_*elo 3 apple-push-notifications ios swift ios13
我正在从 Django 应用程序(使用django-push-notifications)向 iOS 应用程序发送推送通知。该应用程序面向 iOS 13,我在运行 iOS 13.3.1 的 iPhone 7 上运行它。我正在 Xcode 11.3.1 中调试
我正在尝试两种不同的方法来从 Django 端发送通知:
方法一:
devices.send_message(message={"title" : title, "body" : message}, thread_id="events", extra={"foo": "bar"})
Run Code Online (Sandbox Code Playgroud)
方法二:
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"
})
Run Code Online (Sandbox Code Playgroud)
据我所知,这两种方法都应该产生一个类似于方法 2 的负载。
我正在通过执行以下操作进行调试:
无论我做什么,launchOptions 始终为零。我试过设置断点来检查变量。如果 launchOptions 不为零,我尝试使用 os_log 登录到控制台,并且我尝试触发警报(遵循此问题的建议)以排除 Xcode 调试器干扰。它始终为零。
我的 AppDelegate 目前看起来像这样:
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
}
Run Code Online (Sandbox Code Playgroud)
警报触发,但警报内容只是读取“nil”。
我无法弄清楚缺少什么。我的通知有效负载可能不是我认为的那样(我已经在 Github 页面上询问 django-push-notifications 以确认是否存在问题)。也有可能我错过了设置远程通知的一步,但我确实可靠地收到了通知并且它们按我的预期显示,因此它们似乎正在工作。
非常感谢任何建议!
sah*_*ain 13
在 iOS 13.0 中,当应用程序被杀死时,如果您点击通知,想要打开应用程序并获取通知负载。这是你如何做到的。
请检查sceneDelegate下的connectOptions
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 }
}
Run Code Online (Sandbox Code Playgroud)
我没有找到解决此问题的方法,但找到了解决方法。我仍然不知道为什么 launchOptions 总是为零,但我已经能够通过执行以下操作来访问有效负载:
在 AppDelegate.swift 中:
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
Run Code Online (Sandbox Code Playgroud)
...
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
UNUserNotificationCenter.current().delegate = self
return true
}
Run Code Online (Sandbox Code Playgroud)
...
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()
}
}
Run Code Online (Sandbox Code Playgroud)
如果我然后在 userNotificationCenter 中设置一个断点,我可以挖掘出通知负载:

| 归档时间: |
|
| 查看次数: |
3201 次 |
| 最近记录: |