From b039f5e4c7222b848dced97bdf9178f3540fd237 Mon Sep 17 00:00:00 2001 From: otsmr Date: Tue, 8 Apr 2025 14:14:54 +0200 Subject: [PATCH] cancel al notifications on ios --- ios/NotificationService/NotificationService.swift | 15 +++++++++------ lib/src/views/chats/chat_item_details_view.dart | 7 ++++++- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/ios/NotificationService/NotificationService.swift b/ios/NotificationService/NotificationService.swift index 3cfd33a..592bd55 100644 --- a/ios/NotificationService/NotificationService.swift +++ b/ios/NotificationService/NotificationService.swift @@ -30,6 +30,7 @@ class NotificationService: UNNotificationServiceExtension { if data != nil { bestAttemptContent.title = data!.title; bestAttemptContent.body = data!.body; + bestAttemptContent.threadIdentifier = String(format: "%d", data!.notificationId) } else { bestAttemptContent.title = "\(bestAttemptContent.title) [failed to decrypt]" } @@ -65,7 +66,7 @@ import CryptoKit import Foundation import Security -func getPushNotificationData(pushDataJson: String) -> (title: String, body: String)? { +func getPushNotificationData(pushDataJson: String) -> (title: String, body: String, notificationId: Int)? { // Decode the pushDataJson guard let pushData = decodePushData(pushDataJson) else { NSLog("Failed to decode push data") @@ -74,6 +75,7 @@ func getPushNotificationData(pushDataJson: String) -> (title: String, body: Stri var pushKind: PushKind? var displayName: String? + var fromUserId: Int? // Check the keyId if pushData.keyId == 0 { @@ -88,6 +90,7 @@ func getPushNotificationData(pushDataJson: String) -> (title: String, body: Stri pushKind = tryDecryptMessage(key: key.key, pushData: pushData) if pushKind != nil { displayName = userKeys.displayName + fromUserId = userId break } } @@ -106,11 +109,11 @@ func getPushNotificationData(pushDataJson: String) -> (title: String, body: Stri let bestAttemptContent = UNMutableNotificationContent() if pushKind == .testNotification { - return ("Test Notification", "This is a test notification.") - } else if displayName != nil { - return (displayName!, getPushNotificationText(pushKind: pushKind)) + return ("Test Notification", "This is a test notification.", 0) + } else if displayName != nil && fromUserId != nil { + return (displayName!, getPushNotificationText(pushKind: pushKind), fromUserId!) } else { - return ("", getPushNotificationTextWithoutUserId(pushKind: pushKind)) + return ("", getPushNotificationTextWithoutUserId(pushKind: pushKind), 1) } } else { @@ -367,4 +370,4 @@ func getPushNotificationTextWithoutUserId(pushKind: PushKind) -> String { // Return the corresponding message or an empty string if not found return pushNotificationText[pushKind] ?? "" -} \ No newline at end of file +} diff --git a/lib/src/views/chats/chat_item_details_view.dart b/lib/src/views/chats/chat_item_details_view.dart index 0d1f09d..e4cb866 100644 --- a/lib/src/views/chats/chat_item_details_view.dart +++ b/lib/src/views/chats/chat_item_details_view.dart @@ -1,6 +1,7 @@ import 'dart:async'; import 'dart:collection'; import 'dart:convert'; +import 'dart:io'; import 'package:flutter/material.dart'; import 'package:font_awesome_flutter/font_awesome_flutter.dart'; import 'package:twonly/globals.dart'; @@ -254,7 +255,11 @@ class _ChatItemDetailsViewState extends State { twonlyDatabase.messagesDao.watchAllMessagesFrom(widget.userid); messageSub = msgStream.listen((msgs) { if (!context.mounted) return; - flutterLocalNotificationsPlugin.cancel(widget.userid); + if (Platform.isAndroid) { + flutterLocalNotificationsPlugin.cancel(widget.userid); + } else { + flutterLocalNotificationsPlugin.cancelAll(); + } var updated = false; List displayedMessages = []; // should be cleared