-
Notifications
You must be signed in to change notification settings - Fork 230
/
Copy pathpush_controller.dart
129 lines (104 loc) · 4.09 KB
/
push_controller.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
import 'dart:async';
import 'dart:io';
import 'package:firebase_core/firebase_core.dart';
import 'package:get/get.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:google_api_availability/google_api_availability.dart';
import 'package:openim_common/openim_common.dart';
import 'firebase_options.dart';
enum PushType { FCM, none }
const appID = 'your-app-id';
const appKey = 'your-app-key';
const appSecret = 'your-app-secret';
class PushController extends GetxService {
PushType pushType = PushType.none;
/// Logs in the user with the specified alias to the push notification service.
///
/// Depending on the push type configured, it either logs in using the Getui or
/// FCM push service.
///
/// If using Getui, it binds the alias to the Getui service.
///
/// If using FCM, it listens for token refresh events and logs in, invoking the
/// provided callback with the new token.
///
/// Throws an assertion error if the FCM push type is selected but the
/// `onTokenRefresh` callback is not provided.
///
/// - Parameters:
/// - alias: The alias to bind to the push notification service for getui.
/// - onTokenRefresh: A callback function that is invoked with the refreshed
/// token when using FCM. Required if the push type is FCM.
static void login(String alias, {void Function(String token)? onTokenRefresh}) {
if (PushController().pushType == PushType.FCM) {
assert((PushController().pushType == PushType.FCM && onTokenRefresh != null));
FCMPushController()._initialize().then((_) {
FCMPushController()._getToken().then((token) => onTokenRefresh!(token));
FCMPushController()._listenToTokenRefresh((token) => onTokenRefresh);
});
}
}
static void logout() {
if (PushController().pushType == PushType.FCM) {
FCMPushController()._deleteToken();
}
}
}
class FCMPushController {
static final FCMPushController _instance = FCMPushController._internal();
factory FCMPushController() => _instance;
FCMPushController._internal();
Future<void> _initialize() async {
GooglePlayServicesAvailability? availability = GooglePlayServicesAvailability.success;
if (Platform.isAndroid) {
availability = await GoogleApiAvailability.instance.checkGooglePlayServicesAvailability();
}
if (availability != GooglePlayServicesAvailability.serviceInvalid) {
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
} else {
Logger.print('Google Play Services are not available');
return;
}
await _requestPermission();
_configureForegroundNotification();
_configureBackgroundNotification();
return;
}
Future<void> _requestPermission() async {
NotificationSettings settings = await FirebaseMessaging.instance.requestPermission();
print('User granted permission: ${settings.authorizationStatus}');
}
void _configureForegroundNotification() {
FirebaseMessaging.onMessage.listen((RemoteMessage message) async {
print('Foreground notification received: ${message.notification?.title}');
if (message.notification != null) {}
});
}
void _configureBackgroundNotification() {
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
print('App opened from background: ${message.notification?.title}');
});
FirebaseMessaging.instance.getInitialMessage().then((RemoteMessage? message) {
if (message != null) {
print('App opened from terminated state: ${message.notification?.title}');
}
});
}
Future<String> _getToken() async {
final token = await FirebaseMessaging.instance.getToken();
Logger.print("FCM Token: $token");
if (token == null) {
throw Exception('FCM Token is null');
}
return token;
}
Future<void> _deleteToken() {
return FirebaseMessaging.instance.deleteToken();
}
void _listenToTokenRefresh(void Function(String token) onTokenRefresh) {
FirebaseMessaging.instance.onTokenRefresh.listen((String newToken) {
print("FCM Token refreshed: $newToken");
onTokenRefresh(newToken);
});
}
}