我有社交网络应用,并且...我想知道是否有可能通过此应用在锁定屏幕上对通知进行分组。
我的远程服务器将通知发送到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])
}