推送通知 – 在SWIFT中收到推送通知后控制哪个视图控制器加载

推送通知 – 在SWIFT中收到推送通知后控制哪个视图控制器加载,第1张

概述一旦我收到推送通知并滑动打开它,它只是打开我的应用程序,而不是我想要的VC。 所以我的问题是如何加载我想要的VC?我知道如果应用程序是开放的,我会将VC移动到didReceiveRemoteNotification中的另一个,但如果该应用程序未打开,该怎么办?或者是否在后台模式? 另外我有两个不同的推送通知,所以我需要它移动一个两个不同的VC。我如何知道不同的推动不同的区别? 谢谢。 更新为Swi 一旦我收到推送通知并滑动打开它,它只是打开我的应用程序,而不是我想要的VC。

所以我的问题是如何加载我想要的VC?我知道如果应用程序是开放的,我会将VC移动到dIDReceiveRemoteNotification中的另一个,但如果该应用程序未打开,该怎么办?或者是否在后台模式?

另外我有两个不同的推送通知,所以我需要它移动一个两个不同的VC。我如何知道不同的推动不同的区别?

谢谢。

更新为Swift 3.0

就像有人说,你想在applicationDIDLaunchWithOptions中注册远程通知:

func application(_ application: UIApplication,dIDFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {    let pushSettings = UIUserNotificationSettings(types: [.alert,.badge,.sound],categorIEs: nil)    UIApplication.shared.registerUserNotificationSettings(pushSettings)    UIApplication.shared.registerForRemoteNotifications()}

当您从lockScreen / Background返回时,无法知道您将在哪个vIEwController中。我所做的是从appDelegate发送通知。当您收到remoteNotification时,appDelegate中的dIDReceiveRemoteNotification被调用。

func application(_ application: UIApplication,dIDReceiveRemoteNotification userInfo: [AnyHashable: Any]) {    let notif = JsON(userInfo) // SwiftyJsON required

根据您的通知所包含的内容,您应该首先确保它不是零,然后调用将被捕获该通知的vIEwController捕获的通知。可能看起来像这样,只是把它作为一个例子:

if notif["callback"]["type"] != nil{    NotificationCenter.default.post(name: Notification.name(rawValue: "myNotif"),object: nil)    // This is where you read your JsON to kNow what kind of notification you received,for example :    }

例如,如果您收到消息通知,并且您没有登录,因为令牌已过期,则通知将永远不会捕获在视图控制器中,因为它将永远不会被监视。

现在,您在视图控制器中捕获通知的部分。在vIEwWillAppear:

overrIDe func vIEwWillAppear(_ animated: Bool) {    NotificationCenter.default.addobserver(self,selector: #selector(self.catchIt),name: NSNotification.name(rawValue: "myNotif"),object: nil)}

现在你添加了这个观察者,每次在这个控制器中调用一个通知时,函数catchIt也会被调用。您将必须在每个要执行特定 *** 作的视图控制器中实现它。

func catchIt(_ userInfo: Notification){    if userInfo.userInfo?["userInfo"] != nil{        let prefs: UserDefaults = UserDefaults.standard        prefs.removeObject(forKey: "startUpNotif")        prefs.synchronize()        let storyboard = UIStoryboard(name: "Main",bundle: nil)        let vc: RedirectAppInactiveVC = storyboard.instantiateVIEwController(withIDentifIEr: "RedirectAppInactiveVC") as! RedirectAppInactiveVC        self.navigationController?.pushVIEwController(vc,animated: true)    }    else{        let storyboard = UIStoryboard(name: "Main",bundle: nil)        let vc: RedirectAppActiveVC = storyboard.instantiateVIEwController(withIDentifIEr: "RedirectAppActiveVC") as! RedirectAppActiveVC        self.navigationController?.pushVIEwController(vc,animated: true)    }}

离开视图控制器时不要忘记取消订阅通知,否则vIEwController(如果仍然在堆栈中)将捕获通知并执行它(您可能希望这样做,但是知道您将要进行的更安全) )。所以我建议在vIEwWilldisappear中取消订阅:

overrIDe func vIEwWilldisappear(_ animated: Bool) {    NotificationCenter.default.removeObserver(self)}

这样做,你将加载你想要的vIEwController。现在我们还没有对待所有的案件。如果你还没有打开你的应用程序怎么办显然没有UIVIEwController被加载,并且它们都不能捕获通知。你想知道你是否在doFinishLaunchingWithOptions中收到通知:在appDelegate中。我做的是:

let prefs: UserDefaults = UserDefaults.standard    if let remoteNotification = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? NSDictionary {        prefs.set(remoteNotification as! [AnyHashable: Any],forKey: "startUpNotif")        prefs.synchronize()    }

现在,您已经设置了使用远程通知启动应用程序的首选项。在应用程序中应首先加载的控制器中,我建议在vIEwDIDAppear中执行以下 *** 作:

overrIDe func vIEwDIDAppear(animated: Bool) {    let prefs:UserDefaults = UserDefaults.standard    if prefs.value(forKey: "startUpNotif") != nil{        let userInfo: [AnyHashable: Any] = ["inactive": "inactive"]        NotificationCenter.default.post(name: Notification.name(rawValue: "myNotif"),object: nil,userInfo: userInfo as [AnyHashable: Any])    }

希望它有帮助。我还制作了一个github仓库来说明本地通知:Local Notifications Observer Pattern(类似于远程通知)。可以使用根视图Controller Local Notifications Root Pattern来实现类似的逻辑,我个人认为它将取决于您要实现的内容。

总结

以上是内存溢出为你收集整理的推送通知 – 在SWIFT中收到推送通知后控制哪个视图控制器加载全部内容,希望文章能够帮你解决推送通知 – 在SWIFT中收到推送通知后控制哪个视图控制器加载所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1054634.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-25
下一篇2022-05-25

发表评论

登录后才能评论

评论列表(0条)

    保存