【问题标题】:call a specific View Controller when remotepush notification is clicked in swift快速单击远程推送通知时调用特定的视图控制器
【发布时间】:2017-04-26 06:15:32
【问题描述】:

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

【问题讨论】:

标签: swift ios10.3


【解决方案1】:

正如@luzo 所指出的,通知是向视图控制器发送事件发生的方式。通知还有一个 userinfo 参数,它接受您希望与通知一起发送到视图控制器的数据字典。

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

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

并在viewcontroller中,注册通知的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")
    }

【讨论】:

    【解决方案2】:

    您应该为您的视图控制器实现路由器,该控制器将监听应用程序委托发送的通知,他将决定做什么。我就是这样做的,可能是一个更好的解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-25
      • 1970-01-01
      • 2017-08-13
      • 2016-04-23
      相关资源
      最近更新 更多