gpt4 book ai didi

push-notification - 控制在SWIFT中收到推送通知后加载哪个 View Controller

转载 作者:行者123 更新时间:2023-12-03 09:03:15 25 4
gpt4 key购买 nike

收到推送通知并滑动以将其打开后,它只会打开我的应用程序,而不是我想要的VC。

所以我的问题是如何加载所需的VC?我知道该应用程序是否打开,我会将VC移到didReceiveRemoteNotification内部的另一个目录中,但是如果该应用程序未打开,该怎么办?还是在后台模式下?

另外,我有两个不同的推送通知,因此我需要它来移动两个不同的VC之一。如何区分不同的推送通知之间的区别?

谢谢。

最佳答案

已针对Swift 4.2进行了更新

就像说的那样,您想在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

根据您的通知有效负载,首先应确保它不是nil,然后调用将由应捕获此通知的viewController捕获的NSNotification。您还可以在此处根据收到的有效负载发布各种通知。可能看起来像这样,以它为例:
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

}

如果您收到消息通知,并且由于 token 已过期而不再登录,则该通知将永远不会在 View Controller 中捕获,因为它将永远不会被监视。

现在,您将在 View Controller 中捕获通知的部分。在viewWillAppear中:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(self.catchIt), name: NSNotification.Name(rawValue: "myNotif"), object: nil)
}

现在,您已经添加了该观察器,每次在该 Controller 中调用通知时,也会调用catchIt函数。您将必须在要实现特定操作的每个 View Controller 中实现它。
func catchIt(_ userInfo: Notification){

let prefs: UserDefaults = UserDefaults.standard
prefs.removeObject(forKey: "startUpNotif")

if userInfo.userInfo?["userInfo"] != nil{
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)
}
}

离开 View Controller 时,别忘了取消订阅通知,否则,如果viewController仍在堆栈中,它将捕获该通知并执行它(当然,您可能想要这样做,但是知道要进入的内容更安全)。因此,我建议在viewWillDisappear中取消订阅:
override func viewWillDisappear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.removeObserver(self)
}

这样,您将加载所需的viewController。现在我们还没有处理完所有案件。如果您尚未打开应用程序怎么办。显然,尚未加载UIViewController,并且它们都将无法捕获通知。您想知道是否在appDelegate的didFinishLaunchingWithOptions:中收到通知。我要做的是:
let prefs: UserDefaults = UserDefaults.standard
if let remoteNotification = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? NSDictionary {
prefs.set(remoteNotification as! [AnyHashable: Any], forKey: "startUpNotif")
prefs.synchronize()
}

现在,您已经设置了一个首选项,表示该应用程序是使用远程通知启动的。在应该首先在您的应用程序中加载的 Controller 中,建议在viewDidAppear中执行以下操作:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
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(类似于远程通知)。可以使用根 View Controller Local Notifications Root Pattern来实现类似的逻辑,我个人认为这将取决于您要实现的内容。

这些示例在这里说明如何简单地实现它。对于更大的项目,您将最终获得更复杂的体系结构,例如内部使用类似机制的协调器。

关于push-notification - 控制在SWIFT中收到推送通知后加载哪个 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31610896/

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