Swift Group推送通知

时间:2019-07-15 16:00:23

标签: swift push-notification notifications

我有社交网络应用,并且...我想知道是否有可能通过此应用在锁定屏幕上对通知进行分组。

我的远程服务器将通知发送到APN,然后我通过设备令牌收到通知。有什么方法可以按应用分组通知吗?我在中等级别上找到了代码,但是我不知道如何实现它。

https://medium.com/swift-india/lets-take-quick-dive-in-grouped-notifications-5d41af9d6463

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

    //over here add condition if same user has opened chat and notification is also from same user dont show these
    let extraPayLoad = (notification.request.content.userInfo["extraPayLoad"] as? [AnyHashable : Any])
    if extraPayLoad!["custom"] != nil {
        let info_string = extraPayLoad!["custom"] as? String

        let objectData: Data? = info_string?.data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue))
        var json: [AnyHashable : Any]? = nil
        if let aData = objectData {
            json = try! JSONSerialization.jsonObject(with: aData, options: .mutableContainers) as? [AnyHashable : Any]
        }
        if json?["sender_id"] != nil {
            if let aKey = json?["sender_id"] {
                print("sender_id : \(aKey)")
            }

            if(json?["code"] as? Int ?? 0 != 1003 && json?["code"] as? Int ?? 0 != 1004)
            {
                let unread_count_dict:[String: Bool] = ["unread_count":  true]
                NotificationCenter.default.post(name: NSNotification.Name(rawValue: "showNotificationView"), object: nil, userInfo: unread_count_dict)
            }
            if (UserDefaults.standard.value(forKey: "_targetId") as? String ?? "0") == "\((json?["sender_id"] as? Int ?? -1))" {
                //
                // NotificationCenter.default.post(name: NSNotification.Name("GetAllConversationsList"), object: self)
                //return
            }
            if (UserDefaults.standard.value(forKey: "_groupId") as? String ?? "0") == "\((json?["group_id"] as? Int ?? -1))" {
                // NotificationCenter.default.post(name: NSNotification.Name("GetAllConversationsList"), object: self)
                //return
            }

        }
    }
    completionHandler([.sound, .alert, .badge])
}

1 个答案:

答案 0 :(得分:0)

万一有人需要答案...

只需将具有任何标识符的 thread-id 字段添加到您的服务器,然后按这些ID即可将推送通知自动分组...)

enter image description here