【问题标题】:iOS handling multiple push notificationsiOS 处理多个推送通知
【发布时间】:2015-11-12 18:32:21
【问题描述】:

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

在我的 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(message) 会显示所有消息 - 但只显示第一条消息。 我怀疑,对于每个推送通知,消息都会添加到 PushVC 的不同实例中。

任何帮助将不胜感激。

【问题讨论】:

  • 如何加载表格视图的其余部分?还是您只希望有一个条目?

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


【解决方案1】:

这是因为您一次又一次地更改根视图控制器的不同实例,我们有一个新的 VC 实例,您在其中有您的表视图。为了解决您的问题,请将所有通知保存在数组中并将该数组存储在用户默认值中。

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-25
    • 1970-01-01
    相关资源
    最近更新 更多