-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
utils.dart
160 lines (150 loc) 路 5.52 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// 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';
/// Converts an [int] into it's [AndroidNotificationPriority] representation.
AndroidNotificationPriority convertToAndroidNotificationPriority(
int? priority) {
switch (priority) {
case -2:
return AndroidNotificationPriority.minimumPriority;
case -1:
return AndroidNotificationPriority.lowPriority;
case 0:
return AndroidNotificationPriority.defaultPriority;
case 1:
return AndroidNotificationPriority.highPriority;
case 2:
return AndroidNotificationPriority.maximumPriority;
default:
return AndroidNotificationPriority.defaultPriority;
}
}
/// Converts an [AndroidNotificationPriority] into it's [int] representation.
int convertAndroidNotificationPriorityToInt(
AndroidNotificationPriority? priority) {
switch (priority) {
case AndroidNotificationPriority.minimumPriority:
return -2;
case AndroidNotificationPriority.lowPriority:
return -1;
case AndroidNotificationPriority.defaultPriority:
return 0;
case AndroidNotificationPriority.highPriority:
return 1;
case AndroidNotificationPriority.maximumPriority:
return 2;
default:
return 0;
}
}
/// Converts an [int] into it's [AndroidNotificationVisibility] representation.
AndroidNotificationVisibility convertToAndroidNotificationVisibility(
int? visibility) {
switch (visibility) {
case -1:
return AndroidNotificationVisibility.secret;
case 0:
return AndroidNotificationVisibility.private;
case 1:
return AndroidNotificationVisibility.public;
default:
return AndroidNotificationVisibility.private;
}
}
/// Converts an [AndroidNotificationVisibility] into it's [int] representation.
int convertAndroidNotificationVisibilityToInt(
AndroidNotificationVisibility? visibility) {
switch (visibility) {
case AndroidNotificationVisibility.secret:
return -1;
case AndroidNotificationVisibility.private:
return 0;
case AndroidNotificationVisibility.public:
return 1;
default:
return 0;
}
}
/// Converts an [int] into it's [AuthorizationStatus] representation.
AuthorizationStatus convertToAuthorizationStatus(int? status) {
// Can be null on unsupported platforms, e.g. iOS < 10.
if (status == null) {
return AuthorizationStatus.notDetermined;
}
switch (status) {
case -1:
return AuthorizationStatus.notDetermined;
case 0:
return AuthorizationStatus.denied;
case 1:
return AuthorizationStatus.authorized;
case 2:
return AuthorizationStatus.provisional;
default:
return AuthorizationStatus.notDetermined;
}
}
/// Converts an [int] into it's [AppleNotificationSetting] representation.
AppleNotificationSetting convertToAppleNotificationSetting(int? status) {
// Can be null on unsupported platforms, e.g. iOS < 10.
if (status == null) {
return AppleNotificationSetting.notSupported;
}
switch (status) {
case -1:
return AppleNotificationSetting.notSupported;
case 0:
return AppleNotificationSetting.disabled;
case 1:
return AppleNotificationSetting.enabled;
default:
return AppleNotificationSetting.notSupported;
}
}
/// Converts an [int] into its [AppleShowPreviewSetting] representation.
AppleShowPreviewSetting convertToAppleShowPreviewSetting(int? status) {
switch (status) {
case -1:
return AppleShowPreviewSetting.notSupported;
case 0:
return AppleShowPreviewSetting.never;
case 1:
return AppleShowPreviewSetting.always;
case 2:
return AppleShowPreviewSetting.whenAuthenticated;
default:
return AppleShowPreviewSetting.notSupported;
}
}
/// Converts a [Map] into it's [NotificationSettings] representation.
NotificationSettings convertToNotificationSettings(Map<String, int> map) {
return NotificationSettings(
authorizationStatus:
convertToAuthorizationStatus(map['authorizationStatus']),
timeSensitive: convertToAppleNotificationSetting(map['timeSensitive']),
alert: convertToAppleNotificationSetting(map['alert']),
announcement: convertToAppleNotificationSetting(map['announcement']),
badge: convertToAppleNotificationSetting(map['badge']),
carPlay: convertToAppleNotificationSetting(map['carPlay']),
lockScreen: convertToAppleNotificationSetting(map['lockScreen']),
notificationCenter:
convertToAppleNotificationSetting(map['notificationCenter']),
showPreviews: convertToAppleShowPreviewSetting(map['showPreviews']),
sound: convertToAppleNotificationSetting(map['sound']),
);
}
// Default [NotificationSettings] for platforms which do not require permissions
const NotificationSettings defaultNotificationSettings = NotificationSettings(
authorizationStatus: AuthorizationStatus.authorized,
alert: AppleNotificationSetting.notSupported,
announcement: AppleNotificationSetting.notSupported,
badge: AppleNotificationSetting.notSupported,
carPlay: AppleNotificationSetting.notSupported,
lockScreen: AppleNotificationSetting.notSupported,
notificationCenter: AppleNotificationSetting.notSupported,
showPreviews: AppleShowPreviewSetting.notSupported,
sound: AppleNotificationSetting.notSupported,
timeSensitive: AppleNotificationSetting.notSupported,
);