ios - Firebase Notification Swift 3 SecondViewController -
i build firebase notification in app. , successful working. want open if notification data equal "second" open secondviewcontroller in app. how can do? use userinfo did not it.
firebase notification special data: { "view" : "second" } this full appdelegate code. thank you. sorry english.
import uikit import firebase import usernotifications @uiapplicationmain class appdelegate: uiresponder, uiapplicationdelegate { var window: uiwindow? static var shared: appdelegate { return uiapplication.shared.delegate as! appdelegate } func application(_ application: uiapplication, didfinishlaunchingwithoptions launchoptions: [uiapplicationlaunchoptionskey: any]?) -> bool { firebaseapp.configure() application.registerforremotenotifications() requestnotificationauthorization(application: application) if let userinfo = launchoptions?[uiapplicationlaunchoptionskey.remotenotification] as? nsdictionary { nslog("[remotenotification] applicationstate: \(applicationstatestring) didfinishlaunchingwithoptions ios9: \(userinfo)") //todo: handle background notification } return true } var applicationstatestring: string { if uiapplication.shared.applicationstate == .active { return "active" } else if uiapplication.shared.applicationstate == .background { return "background" }else { return "inactive" } } func requestnotificationauthorization(application: uiapplication) { if #available(ios 10.0, *) { 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) } } } @available(ios 10, *) extension appdelegate : unusernotificationcenterdelegate { // ios10+, called when presenting notification in foreground func usernotificationcenter(_ center: unusernotificationcenter, willpresent notification: unnotification, withcompletionhandler completionhandler: @escaping (unnotificationpresentationoptions) -> void) { let userinfo = notification.request.content.userinfo nslog("[usernotificationcenter] applicationstate: \(applicationstatestring) willpresentnotification: \(userinfo)") //todo: handle foreground notification completionhandler([.alert]) } // ios10+, called when received response (default open, dismiss or custom action) notification func usernotificationcenter(_ center: unusernotificationcenter, didreceive response: unnotificationresponse, withcompletionhandler completionhandler: @escaping () -> void) { let userinfo = response.notification.request.content.userinfo nslog("[usernotificationcenter] applicationstate: \(applicationstatestring) didreceiveresponse: \(userinfo)") //todo: handle background notification completionhandler() } } extension appdelegate : messagingdelegate { func messaging(_ messaging: messaging, didrefreshregistrationtoken fcmtoken: string) { nslog("[remotenotification] didrefreshregistrationtoken: \(fcmtoken)") } // ios9, called when presenting notification in foreground func application(_ application: uiapplication, didreceiveremotenotification userinfo: [anyhashable : any]) { nslog("[remotenotification] applicationstate: \(applicationstatestring) didreceiveremotenotification ios9: \(userinfo)") if uiapplication.shared.applicationstate == .active { //todo: handle foreground notification } else { //todo: handle background notification } } }
notification data userinfo
inside usernotificationcenter add code...
let str:string = (userinfo["gcm.notification.imtype"] as? string)! switch str { case "first": let rootviewcontroller = self.window!.rootviewcontroller as! uinavigationcontroller let storyboard = uistoryboard(name: "main", bundle: nil) let firstviewcontroller = storyboard.instantiateviewcontroller(withidentifier: "firstid") as! messagestableviewcontroller rootviewcontroller.pushviewcontroller(firstviewcontroller, animated: true) case "second": let rootviewcontroller = self.window!.rootviewcontroller as! uinavigationcontroller let storyboard = uistoryboard(name: "main", bundle: nil) let secondviewcontroller = storyboard.instantiateviewcontroller(withidentifier: "secondid") as! secondviewcontroller rootviewcontroller.pushviewcontroller(secondviewcontroller, animated: true) default: print("type else") }
Comments
Post a Comment