gpt4 book ai didi

ios - 点击通知后如何显示某个 View Controller 并发送数据?

转载 作者:行者123 更新时间:2023-11-28 07:52:54 25 4
gpt4 key购买 nike

我试图在这里阅读类似的帖子 How to make your push notification Open a certain view controller?但信息不完整。

我正在尝试实现推送通知(firebase 云消息传递)。收到通知警报后,我希望如果用户点击该通知,它会将用户发送到某个 View Controller 并打开已从服务器发送的数据。

  1. how to access the data/information from the server that has been sent through push notification?
  2. send that data/information to a certain view controller?

这是我在 app delegate 中的代码

import UIKit
import Firebase
import UserNotifications


@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, MessagingDelegate, UNUserNotificationCenterDelegate {

var window: UIWindow?
var fcmTokenUser : String?


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

FirebaseApp.configure()


print(NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last! as String)


// To get FCM token that will be sent to APNS via Google FCM
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()


Messaging.messaging().delegate = self
let token = Messaging.messaging().fcmToken
fcmTokenUser = token




return true
}



func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String){
// This callback is fired at each app startup (when the user install the app for the very first time) and whenever a new token is generated due to The app is restored on a new device, The user uninstalls/reinstall the app, The user clears app data.

// after fcm generated for the very first time,then fcm can also be retrieved in the 'didFinishLaunchingWithOptions' method above (let token = Messaging.messaging().fcmToken)


fcmTokenUser = fcmToken





}




private func application(application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
Messaging.messaging().apnsToken = deviceToken as Data
}





}

最佳答案

将此代码添加到您的 AppDelegate检测用户 tap (响应),双击后它会显示一个特定的 viewController正如您指定的那样。

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

//THIS PRINT WILL SHOW YOU THE USER TAP ON NOTIFICATION
print("userNotificationCenter --- \(response) --- ")

let sb = UIStoryboard(name: "Main", bundle: nil)
let otherVC = sb.instantiateViewController(withIdentifier: "yourViewControllerIdentifier") as! yourViewControllerClassName
window?.rootViewController = otherVC;
}

访问data/information从已通过 push notification 发送的服务器-

通知数据在 application:didReceiveRemoteNotification: 中传送到您的应用程序.如果要在applicationDidBecomeActive:中处理您应该将其存储在 application:didReceiveRemoteNotification: 中并在 applicationDidBecomeActive. 中再次阅读

关于ios - 点击通知后如何显示某个 View Controller 并发送数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49229492/

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