-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
utils.dart
102 lines (92 loc) 路 3.32 KB
/
utils.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// ignore_for_file: require_trailing_commas
// Copyright 2020, the Chromium project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:firebase_messaging_platform_interface/firebase_messaging_platform_interface.dart';
import 'interop/messaging.dart';
/// Converts an [String] into it's [AuthorizationStatus] representation.
///
/// See https://developer.mozilla.org/en-US/docs/Web/API/Notification/requestPermission
/// for more information.
AuthorizationStatus convertToAuthorizationStatus(String status) {
switch (status) {
case 'granted':
return AuthorizationStatus.authorized;
case 'denied':
return AuthorizationStatus.denied;
case 'default':
return AuthorizationStatus.notDetermined;
default:
return AuthorizationStatus.notDetermined;
}
}
/// Returns a [NotificationSettings] instance for all Web platforms devices.
NotificationSettings getNotificationSettings(String status) {
return NotificationSettings(
authorizationStatus: convertToAuthorizationStatus(status),
alert: AppleNotificationSetting.notSupported,
announcement: AppleNotificationSetting.notSupported,
badge: AppleNotificationSetting.notSupported,
carPlay: AppleNotificationSetting.notSupported,
lockScreen: AppleNotificationSetting.notSupported,
notificationCenter: AppleNotificationSetting.notSupported,
showPreviews: AppleShowPreviewSetting.notSupported,
sound: AppleNotificationSetting.notSupported,
);
}
/// Converts a messaging [MessagePayload] into a Map.
Map<String, dynamic> messagePayloadToMap(MessagePayload messagePayload) {
String? senderId;
int? sentTime;
Map<String, dynamic> data = {};
if (messagePayload.data != null) {
messagePayload.data!.forEach((key, value) {
if (key == 'google.c.a.c_id') {
senderId = value as String;
}
if (key == 'google.c.a.ts') {
int seconds = int.tryParse(value as String)!;
sentTime = seconds * 1000; // sentTime is ms
}
// Skip any internal keys
if (!key.startsWith('aps') &&
!key.startsWith('gcm.') &&
!key.startsWith('google.')) {
data[key] = value;
}
});
}
return <String, dynamic>{
'senderId': senderId,
'category': null,
'collapseKey': messagePayload.collapseKey,
'contentAvailable': null,
'data': data,
'from': messagePayload.from,
'messageId': messagePayload.messageId,
'mutableContent': null,
'notification': messagePayload.notification == null
? null
: notificationPayloadToMap(
messagePayload.notification!, messagePayload.fcmOptions),
'sentTime': sentTime,
'threadId': null,
'ttl': null,
};
}
/// Converts a messaging [NotificationPayload] into a Map.
///
/// Since [FcmOptions] are web specific, we pass these down to the upper layer
/// as web properties.
Map<String, dynamic> notificationPayloadToMap(
NotificationPayload notificationPayload, FcmOptions? fcmOptions) {
return <String, dynamic>{
'title': notificationPayload.title,
'body': notificationPayload.body,
'web': <String, dynamic>{
'image': notificationPayload.image,
'analyticsLabel': fcmOptions?.analyticsLabel,
'link': fcmOptions?.link,
},
};
}