[英]iOS handling multiple push notifications
我想在表格視圖中顯示多個(遠程)通知。 我的問題是,僅顯示一條消息。
在我的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中的一種方法,用於向表視圖添加新的Notifications:
public func addPushNote(message: String){
pushNotes.append(message)
table.reloadData()
print(message)
}
當接收到多條消息時,print(message)會向我顯示所有消息-但只顯示第一個消息。 我懷疑,對於每個推送通知,消息都會添加到另一個PushVC實例中。
任何幫助將不勝感激。
這是因為您要反復更改根視圖控制器的不同實例,而我們又有了一個具有表視圖的VC的新實例。 為了解決您的問題,請將所有通知保留在Array中,並將該數組存儲為用戶默認設置。
以后,每當有新的通知到達時,請首先從UserDefaults中檢索該數組,並將通知附加到那里,然后將該數組保存回userdefaults中。
還要確保將表視圖的數據源作為存儲的NSUserDefault數組提供。
您還可以將通知存儲在CoreData,Sqlite等中
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.