-
Notifications
You must be signed in to change notification settings - Fork 148
/
Copy pathflutter_apns_only.dart
224 lines (185 loc) · 6.46 KB
/
flutter_apns_only.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import 'dart:async';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart' hide MessageHandler;
class ApnsRemoteMessage {
ApnsRemoteMessage.fromMap(this.payload);
final Map<String, dynamic> payload;
String? get actionIdentifier => UNNotificationAction.getIdentifier(payload);
}
typedef ApnsMessageHandler = Future<void> Function(ApnsRemoteMessage);
typedef WillPresentHandler = Future<bool> Function(ApnsRemoteMessage);
class ApnsPushConnectorOnly {
final MethodChannel _channel = () {
assert(Platform.isIOS,
'ApnsPushConnectorOnly can only be created on iOS platform!');
return const MethodChannel('flutter_apns');
}();
ApnsMessageHandler? _onMessage;
ApnsMessageHandler? _onLaunch;
ApnsMessageHandler? _onResume;
void requestNotificationPermissions(
[IosNotificationSettings iosSettings = const IosNotificationSettings()]) {
_channel.invokeMethod(
'requestNotificationPermissions', iosSettings.toMap());
}
void getAuthorizationStatus() {
_channel.invokeMethod('getAuthorizationStatus', []);
}
final StreamController<IosNotificationSettings> _iosSettingsStreamController =
StreamController<IosNotificationSettings>.broadcast();
Stream<IosNotificationSettings> get onIosSettingsRegistered {
return _iosSettingsStreamController.stream;
}
/// Sets up [MessageHandler] for incoming messages.
void configureApns({
ApnsMessageHandler? onMessage,
ApnsMessageHandler? onLaunch,
ApnsMessageHandler? onResume,
ApnsMessageHandler? onBackgroundMessage,
}) {
_onMessage = onMessage;
_onLaunch = onLaunch;
_onResume = onResume;
_channel.setMethodCallHandler(_handleMethod);
_channel.invokeMethod('configure');
}
Future<dynamic> _handleMethod(MethodCall call) async {
switch (call.method) {
case 'onToken':
token.value = call.arguments;
return null;
case 'onIosSettingsRegistered':
final obj = IosNotificationSettings._fromMap(
call.arguments.cast<String, bool>());
isDisabledByUser.value = obj.alert == false;
return null;
case 'setAuthorizationStatus':
authorizationStatus.value = call.arguments;
return null;
case 'onMessage':
return _onMessage?.call(_extractMessage(call));
case 'onLaunch':
return _onLaunch?.call(_extractMessage(call));
case 'onResume':
return _onResume?.call(_extractMessage(call));
case 'willPresent':
return shouldPresent?.call(_extractMessage(call)) ??
Future.value(false);
default:
throw UnsupportedError('Unrecognized JSON message');
}
}
ApnsRemoteMessage _extractMessage(MethodCall call) {
final map = call.arguments as Map;
// fix null safety errors
map.putIfAbsent('contentAvailable', () => false);
map.putIfAbsent('mutableContent', () => false);
return ApnsRemoteMessage.fromMap(map.cast());
}
/// Handler that returns true/false to decide if push alert should be displayed when in foreground.
/// Returning true will delay onMessage callback until user actually clicks on it
WillPresentHandler? shouldPresent;
final isDisabledByUser = ValueNotifier(false);
final authorizationStatus = ValueNotifier<String?>(null);
final token = ValueNotifier<String?>(null);
String get providerType => "APNS";
void dispose() {
_iosSettingsStreamController.close();
}
/// https://developer.apple.com/documentation/usernotifications/declaring_your_actionable_notification_types
Future<void> setNotificationCategories(
List<UNNotificationCategory> categories) {
return _channel.invokeMethod(
'setNotificationCategories',
categories.map((e) => e.toJson()).toList(),
);
}
Future<void> unregister() async {
await _channel.invokeMethod('unregister');
token.value = null;
}
}
class IosNotificationSettings {
const IosNotificationSettings({
this.sound = true,
this.alert = true,
this.badge = true,
});
IosNotificationSettings._fromMap(Map<String, bool> settings)
: sound = settings['sound'],
alert = settings['alert'],
badge = settings['badge'];
final bool? sound;
final bool? alert;
final bool? badge;
Map<String, dynamic> toMap() {
return <String, bool?>{'sound': sound, 'alert': alert, 'badge': badge};
}
@override
String toString() => 'PushNotificationSettings ${toMap()}';
}
/// https://developer.apple.com/documentation/usernotifications/unnotificationcategory
class UNNotificationCategory {
final String identifier;
final List<UNNotificationAction> actions;
final List<String> intentIdentifiers;
final List<UNNotificationCategoryOptions> options;
Map<String, dynamic> toJson() {
return {
'identifier': identifier,
'actions': actions.map((e) => e.toJson()).toList(),
'intentIdentifiers': intentIdentifiers,
'options': _optionsToJson(options),
};
}
UNNotificationCategory({
required this.identifier,
required this.actions,
required this.intentIdentifiers,
required this.options,
});
}
/// https://developer.apple.com/documentation/usernotifications/UNNotificationAction
class UNNotificationAction {
final String identifier;
final String title;
final List<UNNotificationActionOptions> options;
static const defaultIdentifier =
'com.apple.UNNotificationDefaultActionIdentifier';
/// Returns action identifier associated with this push.
/// May be null, UNNotificationAction.defaultIdentifier, or value declared in setNotificationCategories
static String? getIdentifier(Map<String, dynamic> payload) {
final data = payload['data'] as Map?;
return data?['actionIdentifier'] ?? payload['actionIdentifier'];
}
UNNotificationAction({
required this.identifier,
required this.title,
required this.options,
});
dynamic toJson() {
return {
'identifier': identifier,
'title': title,
'options': _optionsToJson(options),
};
}
}
/// https://developer.apple.com/documentation/usernotifications/unnotificationactionoptions
enum UNNotificationActionOptions {
authenticationRequired,
destructive,
foreground,
}
/// https://developer.apple.com/documentation/usernotifications/unnotificationcategoryoptions
enum UNNotificationCategoryOptions {
customDismissAction,
allowInCarPlay,
hiddenPreviewsShowTitle,
hiddenPreviewsShowSubtitle,
allowAnnouncement,
}
List<String> _optionsToJson(List values) {
return values.map((e) => e.toString()).toList();
}