Firebase推送通知操作 [英] Firebase push notification action
问题描述
我刚用swift 3构建了我的第一个应用程序.Firebase运行良好,并且正在我的设备上接收通知.我要做的是在用户单击通知时设置操作.我想发送给具有特定产品ID(从后端发送)的特定ProductViewController.如果用户已登录,请继续使用ProductViewController,否则请不要停留在登录屏幕上.
I just builded my first app with swift 3. Firebase is working good and I'm receiving notifications on my device. What I want to do is to set the action when user click the notification. I want to send to specific ProductViewController, with specific product ID (sent from backend). If user is logged in continue to ProductViewController, if not stay on login screen.
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.
you need to fire a segue with the identifier of the viewController you want to open ..
self.performSegue(withIdentifier: "ProductViewControllerSegue",sender: nil)
您还需要通过StoryBoard设置segue才能实现此目标.
You also need to set segue through your storyBoard in order to achieve this..
这篇关于Firebase推送通知操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!