swift - 从远程通知打开 ViewController

标签 swift push-notification apple-push-notifications appdelegate

当我的应用捕捉到远程通知时,我尝试打开一个特定的 ViewController。

让我展示一下我的项目架构。 这是我的 Storyboard: enter image description here

当我收到通知时,我想打开一个“SimplePostViewController”,所以这是我的 appDelegate:

var window: UIWindow?
var navigationVC: UINavigationController?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    let notificationTypes: UIUserNotificationType = [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound]
    let pushNotificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: nil)
    let storyboard = UIStoryboard(name: "Main", bundle: nil)

    self.navigationVC = storyboard.instantiateViewControllerWithIdentifier("LastestPostsNavigationController") as? UINavigationController
    application.registerUserNotificationSettings(pushNotificationSettings)
    application.registerForRemoteNotifications()
    return true
}

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
    if let postId = userInfo["postId"] as? String {
        print(postId)

        let api = EVWordPressAPI(wordpressOauth2Settings: Wordpress.wordpressOauth2Settings, site: Wordpress.siteName)

        api.postById(postId) { post in
            if (post != nil) {
                self.navigationVC!.pushViewController(SimplePostViewController(), animated: true)
            } else {
                print("An error occurred")
            }
        }

    }
}

我在应用程序启动时保存我的 UINavigationViewController,并在收到通知时尝试推送一个新的 SimplePostViewController。但什么也没有发生。 我放置了断点,看到到达了我的 pushViewController 方法,但没有到达我的 SimplePostViewController 的 ViewWillAppear。

我还使用了“新增功能” View 添加执行我的 segue 但也没有任何反应。

解决方案:

for child in (self.rootViewController?.childViewControllers)! {
    if child.restorationIdentifier == "LastestPostsNavigationController" {
       let lastestPostsTableViewController = (child.childViewControllers[0]) as! LastestPostsTableViewController
       let simplePostVC = (self.storyboard?.instantiateViewControllerWithIdentifier("PostViewController"))! as! PostViewController

       simplePostVC.post = post
       lastestPostsTableViewController.navigationController?.pushViewController(simplePostVC, animated: true)
    }
 }

我用:

child.childViewControllers[0]

因为我的示例中只有一个 child 。

最佳答案

我创建了一个带有本地通知而不是远程通知的示例项目,以便于显示功能,但它应该像在应用程序委托(delegate)中设置窗口的 Root View Controller 一样简单 didreciveremote 通知。

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    // Subscribe for notifications - assume the user chose yes for now
    application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil))

    return true
}


func applicationDidEnterBackground(application: UIApplication) {
    //Crete a local notification
    let notification = UILocalNotification()
    notification.alertBody = "This is a fake notification"
    notification.fireDate  = NSDate(timeIntervalSinceNow: 2)
    UIApplication.sharedApplication().scheduleLocalNotification(notification)
}

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
    let sb = UIStoryboard(name: "Main", bundle: nil)
    let otherVC = sb.instantiateViewControllerWithIdentifier("otherVC") as! OtherViewController
    window?.rootViewController = otherVC;
}

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
    //Your code here
}

` 您需要担心管理您的 View 层次结构并向它发送您需要从通知用户数据发送的任何内容。

在我的示例中,我创建了一个本地通知,当您关闭在查看几秒后触发的应用程序时。如果您随后从通知启动该应用程序,它将打开“其他 View Controller ”,在您的情况下这将是“SimplePostViewController”。

此外,请确保您在 didFinishLaunchWithOptions 中注册了远程通知。

Github 非常简单的示例:https://github.com/spt131/exampleNotificationResponse

关于swift - 从远程通知打开 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39112114/

相关文章:

swift - 无法从数据源获取单元格?

ios - 在 Swift 中仅导入 ObjC header : No Such Module error

android - 使用 c2dm 一次发送多个推送

ios - 如何使用解析服务器通知设置 APNS 折叠 ID?

ios - 在服务器数据库更新时通知 iOS 应用程序的最佳方式

swift - 如何使用 SwiftUI 从 Apple Watch Digital Crown 触发 @State 变量更改的函数?

swift - 对 swift 中的类型判断感到困惑?

ios - didReceiveRemoteNotification 未调用 Swift

api - OneSignal-发送计划的通知

php - Laravel 5 android 聊天与推送通知像 Whatsapp