gpt4 book ai didi

ios - 点击本地通知打开特定的 View Controller

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:30:37 25 4
gpt4 key购买 nike

标题说明了一切。我浏览了很多帖子,试图找到一个解决方案,但没有成功......

我有一个通知,我不确定它的名字...

let request = UNNotificationRequest(identifier: "timerDone", content: content, trigger: trigger)

q1:名称是 timerDone 吗?

viewDidLoad() 中:

NotificationCenter.default.addObserver(self,
selector: "SomeNotificationAct:",
name: NSNotification.Name(rawValue: "timerDone"),
object: nil)

然后我有这个方法:

@objc func SomeNotificationAct(notification: NSNotification){
DispatchQueue.main.async() {
self.performSegue(withIdentifier: "NotificationView", sender: self)
}
}

AppDelegate.swift 中使用这个:

private func application(application: UIApplication, didReceiveRemoteNotification userInfo: Any?){

NotificationCenter.default.post(name: NSNotification.Name(rawValue: "SomeNotification"), object:nil)
}

任何想法如何做到这一点?提前致谢!

更新:@Sh_Khan

首先,我正在用 swift 编写代码,我尝试将您的代码从 obj-c 转换为 swift 如下:

if (launchOptions![UIApplicationLaunchOptionsKey.localNotification] != nil)
{
var notification =
launchOptions![UIApplicationLaunchOptionsKey.localNotification]
[self application:application didReceiveLocalNotification:notification]; //how to translate?
}

最后一行应该翻译成什么?

当你写的时候:

应该在 didReceiveLocalNotification 方法中的用户默认值中存储一个 bool 变量,并在 rootViewcontroller 的 viewDidAppear 方法中检查它以生成 segue,然后将其设为 false,因为 notificationCenter 仅在应用程序处于前台或后台时才起作用,如果还没有暂停

假设 bool 值是 notiView,当我们收到本地通知时将其设置为 true,因此转场将转到一个不同 View Controller 。这是你的意思吗?

最佳答案

我发现用户通知文档有点困惑和不完整。这些教程比大多数其他 Apple 框架都要好。然而,这些教程大多是不完整的,并且假设每个应用程序都在 AppDelegate 中实现了通知中心委托(delegate)。不!

对于许多在 View Controller 中(而不是在 AppDelegate 中)处理通知委托(delegate)的应用程序,需要在 AppDelegate didFinishLaunchingWithOptions 方法中将 View Controller 设置为用户通知中心委托(delegate)。否则,当您的应用程序从后台模式启动时,通知代理将看不到您的通知触发。您的 View Controller 在通知触发后加载。您需要一种在 View Controller 完成加载后启动委托(delegate)方法的方法。

例如:假设您正在使用一个 Split View Controller 作为您应用程序的初始 View Controller ,并且您已经将拆分 VC 的主视图 Controller 实现为您的通知代理,您需要让 UNUserNotificationCenter 知道主 VC 是它的当您的应用程序启动时委托(delegate)(不是像大多数教程建议的那样在主 VC 的 viewDidLoad() 内部)。例如,

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
...
let splitViewController = window!.rootViewController as! UISplitViewController
...
if #available(iOS 10.0, *) {
if let masterViewController = splitViewController.viewControllers.first as? MasterViewController {
UNUserNotificationCenter.current().delegate = masterViewController
}
}
}

当您的应用冷启动或从后台模式启动时,这将允许 iOS 在主 VC 加载后调用您的通知委托(delegate)方法。

此外,如果您需要您的主 VC 知道它是由于用户通知触发(而不是正常加载)而加载的,您将使用 NSUserDefaults 来传达此信息。因此,AppDelegate 将如下所示:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
...
let splitViewController = window!.rootViewController as! UISplitViewController
...
if #available(iOS 10.0, *) {
if let _ = launchOptions?[UIApplicationLaunchOptionsKey.localNotification] {
UserDefaults.standard.set("yes", forKey: kMyAppLaunchedByUserNotification)
UserDefaults.standard.synchronize()
}
if let masterViewController = splitViewController.viewControllers.first as? MasterViewController {
UNUserNotificationCenter.current().delegate = masterViewController
}
}
}

其中 kMyAppLaunchedByUserNotification 是您用来与主 VC 通信的 key 。在 Master View Controller 的 viewDidAppear() 中,您将检查 User Defaults 以查看它是否因通知而被加载。

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

if #available(iOS 10.0, *) {
if let _ = UserDefaults.standard.object(forKey: kMyAppLaunchedByUserNotification) {
UserDefaults.standard.removeObject(forKey: kMyAppLaunchedByUserNotification)
UserDefaults.standard.synchronize()
//Handle any special thing that you need to do when your app is launched from User notification as opposed to regular app launch here
// NOTE: the notification delegate methods will be called after viewDidAppear() regardless of you doing any special thing here because you told iOS already in AppDelegate didFinishLaunchingWithOptions
}
}
}

希望对你有帮助

关于ios - 点击本地通知打开特定的 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47734432/

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