gpt4 book ai didi

ios - 使用推送通知快速打开特定 View

转载 作者:行者123 更新时间:2023-11-29 05:30:30 24 4
gpt4 key购买 nike

我有一个应用程序,用户可以在其中接收推送通知。此推送通知工作正常并到达设备。如果应用程序上有新用户或用户添加包时,系统会向用户设备发送通知。如何单击通知以转到新用户配置文件,而不仅仅是打开应用程序。以下是我如何将推送通知发送到我的设备。

extension AppDelegate: UNUserNotificationCenterDelegate {

func delegateSetup(_ application: UIApplication, launchOptions: [UIApplication.LaunchOptionsKey: Any]?) {

// For iOS 10+ display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
// For iOS 10+ data message (sent via FCM)
Messaging.messaging().delegate = self

application.registerForRemoteNotifications()

let token = Messaging.messaging().fcmToken
print("FCM token: \(token ?? "no token")")

Messaging.messaging().shouldEstablishDirectChannel = true

let option: UNAuthorizationOptions = [.badge, .sound, .alert]
UNUserNotificationCenter.current()
.requestAuthorization(options: option) { success, error in
if let error = error {
print("Error: \(error)")
}
}

if launchOptions?[.remoteNotification] != nil {
// window?.rootViewController = NavigationManager.instantiateSignupViewController()
} else {
// window?.rootViewController = NavigationManager.instantiateSplashViewController()
}
if let token = Messaging.messaging().fcmToken {
//Send token to server if neccessary
sendTokenToServer(token)
}
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let tokenParts = deviceToken.map { data in String(format: "%02.2hhx", data) }
let token = tokenParts.joined()
print("FirebaseTest Device Token: \(token)")
}

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("FirebaseTest Failed to register: \(error)")
}

public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Swift.Void) {
log("FirebaseTest 444 \(notification.request.content.userInfo)")
completionHandler([.alert, .badge, .sound])
}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
// Print full message.
log("FirebaseTest \(userInfo)")
if application.applicationState == .active {
log("FirebaseTest App already open")
} else {
log("FirebaseTest App opened from Notification")
}
completionHandler(.newData)
}

}

感谢任何帮助

响应通知的示例是

"status": true,
"message": "Notification sent",
"data": [
"{\"multicast_id\":5626624406944416203,\"success\":1,\"failure\":0,\"canonical_ids\":0,\"results\":[{\"message_id\":\"1566407814132865\"}]}",
{
"body" : "A new property has just been approved under your agency",
"title" : "New property Approved",
"type" : "new_property",
"meta" : {"property_id" : 4}
}
]

property_id 用于显示详细信息

最佳答案

当您点击通知中心的通知时,打开应用时会调用此方法

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { 
let userInfo = response.notification.request.content.userInfo
print(userInfo)
}

您可以延迟从此通知信息显示另一个 vc 的过程,直到应用程序处于事件状态

关于ios - 使用推送通知快速打开特定 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57647565/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com