2015-11-12 97 views
2

我想在表格视图中显示多个(远程)通知。 我的问题是,只有一条消息正在显示。iOS处理多个推送通知

在我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) 
} 

当接收多条消息,打印(信息)显示我所有 - 但只是第一个显示。 我怀疑,对于每个推送通知,该消息都被添加到不同的PushVC实例。

任何帮助将不胜感激。

+0

你是如何加载你的表视图的其余部分?或者你只希望有一个入口? – beyowulf

回答

3

它因为不同的实例,你一次又一次地改变根视图控制器,我们有一个你有你的表视图的VC的新实例。为了解决您的问题,请将所有通知保留在Array中,并将该阵列存储为用户默认值。然后,每当有新的通知到达时,首先从UserDefaults中检索数组,然后在那里附加通知并将该数组保存在userdefaults中。

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

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