gpt4 book ai didi

ios - Firebase 推送通知操作

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

我刚刚使用 swift 3 构建了我的第一个应用。Firebase 运行良好,我在我的设备上收到通知。我想要做的是设置用户单击通知时的操作。我想发送到特定的 ProductViewController,带有特定的产品 ID(从后端发送)。如果用户已登录,则继续访问 ProductViewController,如果未登录,则停留在登录屏幕上。

AppDelegate.swift代码:

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, FIRMessagingDelegate {

var window: UIWindow?

func applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage) {
print(remoteMessage.appData)
}

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

// Override point for customization after application launch.

let lagFreeField = UITextField()
self.window?.addSubview(lagFreeField)
lagFreeField.becomeFirstResponder()
lagFreeField.resignFirstResponder()
lagFreeField.removeFromSuperview()


// [START register_for_notifications]
if #available(iOS 10.0, *) {
let authOptions : UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions, completionHandler: {_,_ in })

// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
// For iOS 10 data message (sent via FCM)
FIRMessaging.messaging().remoteMessageDelegate = self as? FIRMessagingDelegate

} else {
let settings = UIUserNotificationSettings(types: [.alert, .badge , .sound], categories: nil)

application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
}

application.registerForRemoteNotifications()

// [END register_for_notifications]

FIRApp.configure()

print("AppDelegate")

IQKeyboardManager.sharedManager().enable = true
self.window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let tab = storyboard.instantiateViewController(withIdentifier: "TabBarController") as! UITabBarController
self.window?.rootViewController = tab
if Defaults.hasKey(.logged), let logged = Defaults[.logged], logged == true{
APIRequest.username = Defaults[.username]!
APIRequest.password = Defaults[.password]!
let tab = storyboard.instantiateViewController(withIdentifier: "TabBarController") as! UITabBarController
self.window?.rootViewController = tab

} else {
let controller = storyboard.instantiateViewController(withIdentifier: "LoginViewController")
self.window?.rootViewController = controller
}
self.window?.makeKeyAndVisible()

return true
}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
print("Userinfo \(userInfo)")
} ... other default functions

最佳答案

您需要使用要打开的 viewController 的标识符来触发 Segue ..

self.performSegue(withIdentifier: "ProductViewControllerSegue",sender: nil)

您还需要通过 Storyboard设置 segue 才能实现此目的..

关于ios - Firebase 推送通知操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44076216/

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