ios - 来自后台的远程通知
问题描述
我正在快速创建一个将使用UNNotificationAction
按钮的应用程序。
我已经userNotificationCenter
正确设置,我可以在应用程序打开时正确调用didReceive
...从这里我显示一个模式窗口。
问题是,当应用程序未在前台或后台运行时(用户尚未打开应用程序)didFinishLaunchingWithOptions
,当我检查时我无法解析我的代码launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification]
当用户使用UNNotificationAction
.
解决方案
请检查下面的代码,希望这会有所帮助。
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate,CLLocationManagerDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if #available(iOS 10.0, *) {
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
}
@available(iOS 10, *)
// Receive displayed notifications for iOS 10 devices.
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
// With swizzling disabled you must let Messaging know about the message, for Analytics
Messaging.messaging().appDidReceiveMessage(userInfo)
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
// print(userInfo)
completionHandler([.alert, .badge, .sound])
// Change this to your preferred presentation option
// completionHandler([])
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
switch response.actionIdentifier {
case "action1":
print("Action First Tapped")
case "action2":
print("Action Second Tapped")
default:
break
}
// Print full message.
print(userInfo)
Messaging.messaging().appDidReceiveMessage(userInfo)
completionHandler()
}
推荐阅读
- ios - UIImagePickerController 和照片库访问
- python - 如何使用 Grid 重新调整 Tkinter 文本输入小部件的大小?
- performance - 在 Kubernetes Live Probes 中配置 Spring Boot 在高负载下表现良好
- c++ - gcc/g++ 使用链接器标志“-lSDL2”
- javascript - 淡化动画使我的模态在 Windows 10 中失败
- azure - [Azure][Bot Service] 有没有 API 可以从 directline 3.0 chache 中删除对话?
- swift - Swift:禁用 UITabBarItem 的方法不起作用
- cakephp - CakePHP 如何验证 CSRF 令牌?
- java - 如何使用 Gson 反序列化 Kotlin 数组列表?
- c++ - 使用指针作为参数重载 ostream << 运算符,导致输出内存地址