Bug fixes in notification and support iOS Communication Notification!

This commit is contained in:
LittleSheep 2024-07-21 23:43:18 +08:00
parent dbd05dbb79
commit 62edab0131
3 changed files with 53 additions and 40 deletions

View File

@ -8,54 +8,64 @@
import UserNotifications import UserNotifications
import Intents import Intents
enum ParseNotificationPayloadError: Error {
case noMetadata(String)
case noAvatarUrl(String)
}
class NotificationService: UNNotificationServiceExtension { class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)? var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent? var bestAttemptContent: UNMutableNotificationContent?
func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) async { override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent) bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
if let bestAttemptContent = bestAttemptContent { if let bestAttemptContent = bestAttemptContent {
switch bestAttemptContent.categoryIdentifier { do {
case "messaging.message", "messaging.callStart": switch bestAttemptContent.categoryIdentifier {
let metadata = bestAttemptContent.userInfo["metadata"] as! [AnyHashable : Any] case "messaging.message", "messaging.callStart":
let handle = INPersonHandle(value: String(metadata["user_id"] as! Int), type: .unknown) guard let metadata = bestAttemptContent.userInfo["metadata"] as? [AnyHashable : Any] else {
let avatar = INImage( throw ParseNotificationPayloadError.noMetadata("The notification has no metadata.")
url: URL(string: bestAttemptContent.userInfo["avatar"] as! String)! }
)! let userId = metadata["user_id"] as! Int
let sender = INPerson(personHandle: handle, let userName = metadata["user_name"] as? String
nameComponents: nil, let userNick = metadata["user_nick"] as? String
displayName: metadata["user_nick"] as? String,
image: avatar,
contactIdentifier: nil,
customIdentifier: nil)
let intent = INSendMessageIntent(recipients: nil,
outgoingMessageType: .outgoingMessageText,
content: bestAttemptContent.body,
speakableGroupName: nil,
conversationIdentifier: String(metadata["channel_id"] as! Int),
serviceName: nil,
sender: sender,
attachments: nil)
let interaction = INInteraction(intent: intent, response: nil) guard let avatarUrl = bestAttemptContent.userInfo["avatar"] as? String else {
interaction.direction = .incoming throw ParseNotificationPayloadError.noMetadata("The notification has no avatar url.")
}
do { let handle = INPersonHandle(value: String(userId), type: .unknown)
try await interaction.donate() let avatar = INImage(
url: URL(string: avatarUrl)!
)!
let sender = INPerson(personHandle: handle,
nameComponents: nil,
displayName: userNick,
image: avatar,
contactIdentifier: nil,
customIdentifier: userName)
let intent = INSendMessageIntent(recipients: nil,
outgoingMessageType: .outgoingMessageText,
content: bestAttemptContent.body,
speakableGroupName: nil,
conversationIdentifier: String(metadata["channel_id"] as! Int),
serviceName: "PostPigeon",
sender: sender,
attachments: nil)
let updatedContent = try request.content.updating(from: intent) let interaction = INInteraction(intent: intent, response: nil)
contentHandler(updatedContent) interaction.direction = .incoming
} catch { interaction.donate(completion: nil)
return break
default:
contentHandler(bestAttemptContent)
break
} }
} catch {
break
default:
contentHandler(bestAttemptContent) contentHandler(bestAttemptContent)
break
} }
} }
} }

View File

@ -9,7 +9,7 @@ class Notification {
String? avatar; String? avatar;
String? picture; String? picture;
int? senderId; int? senderId;
int recipientId; int accountId;
Notification({ Notification({
required this.id, required this.id,
@ -22,7 +22,7 @@ class Notification {
required this.avatar, required this.avatar,
required this.picture, required this.picture,
required this.senderId, required this.senderId,
required this.recipientId, required this.accountId,
}); });
factory Notification.fromJson(Map<String, dynamic> json) => Notification( factory Notification.fromJson(Map<String, dynamic> json) => Notification(
@ -40,7 +40,7 @@ class Notification {
avatar: json['avatar'], avatar: json['avatar'],
picture: json['picture'], picture: json['picture'],
senderId: json['sender_id'], senderId: json['sender_id'],
recipientId: json['recipient_id'], accountId: json['account_id'],
); );
Map<String, dynamic> toJson() => { Map<String, dynamic> toJson() => {
@ -54,6 +54,6 @@ class Notification {
'avatar': avatar, 'avatar': avatar,
'picture': picture, 'picture': picture,
'sender_id': senderId, 'sender_id': senderId,
'recipient_id': recipientId, 'account_id': accountId,
}; };
} }

View File

@ -127,6 +127,8 @@ class WebSocketProvider extends GetxController {
if (deviceUuid == null || deviceUuid.isEmpty) { if (deviceUuid == null || deviceUuid.isEmpty) {
log("Unable to active push notifications, couldn't get device uuid"); log("Unable to active push notifications, couldn't get device uuid");
} else {
log('Device UUID is $deviceUuid');
} }
if (PlatformInfo.isIOS || PlatformInfo.isMacOS) { if (PlatformInfo.isIOS || PlatformInfo.isMacOS) {
@ -136,6 +138,7 @@ class WebSocketProvider extends GetxController {
provider = 'firebase'; provider = 'firebase';
token = await FirebaseMessaging.instance.getToken(); token = await FirebaseMessaging.instance.getToken();
} }
log('Device Push Token is $token');
final client = auth.configureClient('auth'); final client = auth.configureClient('auth');