swift - 当快速点击远程推送通知时调用特定的 View Controller

标签 swift ios10.3

我想在点击通知时在特定 View Controller 中显示推送通知,并且还想将数据从通知发送到 View Controller 。我正在使用 swift 进行开发

最佳答案

正如 @luzo 所指出的,通知是发送事件已发生的信息以与 View Controller 进行通信的方式。该通知还有一个 userinfo 参数,该参数接受您想要与通知一起发送到 View Controller 的数据字典。

在 Swift 3 中,将其添加到点击按钮:

let center = NotificationCenter.default
center.post(name: Notification.Name(rawValue: "nameOfNotification"),
                    object: nil,
                    userInfo:["id":"data"])

并在 View Controller 中注册通知的 id 并添加函数引用:

    let center = NotificationCenter.default
    center.addObserver(forName:NSNotification.Name(rawValue: "nameOfNotification"), object:nil, queue:nil, using:notifDidConnect)

并添加函数实现:

 func notifDidConnect(notification:Notification) -> Void {
        guard let userInfo = notification.userInfo,
            let id  = userInfo["id"] as? String else {
               print("error occured")
               return
             }
        print("notification received")
    }

关于swift - 当快速点击远程推送通知时调用特定的 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43626374/

相关文章:

ios - Ionic 4 build 不适用于 ios 10.3

javascript - IOS 10.3.1 Chrome 浏览器 CORS 失败

ios - “使用未解析的标识符”

swift - 有没有办法将 `if case` 语句写成表达式?

swift - cloudkit 输入用户 ID 作为新记录的引用(在代码中)

swift - 从 iOS 10.3 开始,discoverAllIdentities 和 discoverUserIdentity 为 CKUserIdentity.lookupInfo 返回 nil

ios - Code=3072 "The operation was cancelled"设置alternate app icon时

ios - 在 Swift 中将委托(delegate)设置为 self 抛出错误

ios - 如何为 iOS AVFoundation 定义自定义检测器?

iOS10.3系统富文本删除线无法显示