iOS处理多个推送通知

时间:2015-11-12 18:32:22

标签: ios swift notifications apple-push-notifications tableview

我想在表格视图中显示多个(远程)通知。 我的问题是,只显示了一条消息。

在我的AppDelegate.swift中,我得到了这个:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]){

    let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let rVC = storyboard.instantiateViewControllerWithIdentifier("PushVC")
    let push = rVC as! PushVC
    self.window!.rootViewController = rVC

    if let aps = userInfo["aps"] as? NSDictionary {
        if let alert = aps["alert"] as? NSDictionary {
            if let message = alert["message"] as? NSString {
                push.addPushNote(message as String)
            }
        } else if let alert = aps["alert"] as? NSString {
            push.addPushNote(alert as String)
        }
    }
}

addPushNote是我的ViewController中的一个方法,用于向表格视图添加新的通知:

public func addPushNote(message: String){
    pushNotes.append(message)
    table.reloadData()
    print(message)
}

当收到多条消息时,print(消息)会显示所有消息 - 但只显示第一条消息。 我怀疑,对于每个推送通知,消息都会添加到PushVC的不同实例中。

任何帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分:3)

由于您不断更改根视图控制器的不同实例,我们有一个VC的新实例,您可以在其中拥有表视图。为了解决您的问题,请将所有通知保存在Array中,并将该数组存储在用户默认值中。

稍后,每当有任何新通知到达时,首先从UserDefaults检索数组并在那里附加通知并将该数组保存回用户默认值。

还要确保将表视图的数据源作为存储的NSUserDefault数组。

您还可以将通知存储在CoreData,Sqlite等

相关问题