|
| 1 | +part of flutter_parse_sdk_flutter; |
| 2 | + |
| 3 | +class ParsePush { |
| 4 | + static final ParsePush instance = ParsePush._internal(); |
| 5 | + |
| 6 | + static String keyType = "gcm"; |
| 7 | + static String keyPushType = 'pushType'; |
| 8 | + |
| 9 | + factory ParsePush() { |
| 10 | + return instance; |
| 11 | + } |
| 12 | + |
| 13 | + ParsePush._internal(); |
| 14 | + |
| 15 | + /// Initialize ParsePush; for web a [vapidKey] is required. |
| 16 | + Future<void> initialize( |
| 17 | + firebaseMessaging, { |
| 18 | + String? vapidKey, |
| 19 | + }) async { |
| 20 | + // Get Google Cloud Messaging (GCM) token |
| 21 | + firebaseMessaging |
| 22 | + .getToken(vapidKey: vapidKey) |
| 23 | + .asStream() |
| 24 | + .listen((event) async { |
| 25 | + // Set token in installation |
| 26 | + sdk.ParseInstallation parseInstallation = |
| 27 | + await sdk.ParseInstallation.currentInstallation(); |
| 28 | + |
| 29 | + parseInstallation.deviceToken = event; |
| 30 | + parseInstallation.set(keyPushType, keyType); |
| 31 | + |
| 32 | + await parseInstallation.save(); |
| 33 | + }); |
| 34 | + } |
| 35 | + |
| 36 | + /// Handle push notification message |
| 37 | + void onMessage(message) { |
| 38 | + String pushId = message.data["push_id"] ?? ""; |
| 39 | + String timestamp = message.data["time"] ?? ""; |
| 40 | + String dataString = message.data["data"] ?? ""; |
| 41 | + String channel = message.data["channel"] ?? ""; |
| 42 | + |
| 43 | + Map<String, dynamic>? data; |
| 44 | + try { |
| 45 | + data = json.decode(dataString); |
| 46 | + } catch (_) {} |
| 47 | + |
| 48 | + _handlePush(pushId, timestamp, channel, data); |
| 49 | + } |
| 50 | + |
| 51 | + void _handlePush(String pushId, String timestamp, String channel, |
| 52 | + Map<String, dynamic>? data) { |
| 53 | + if (pushId.isEmpty || timestamp.isEmpty) { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + if (data != null) { |
| 58 | + // Show push notification |
| 59 | + ParseNotification.instance.showNotification(data["alert"]); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /// Subscribes the device to a channel of push notifications |
| 64 | + Future<void> subscribeToChannel(String value) async { |
| 65 | + sdk.ParseInstallation parseInstallation = |
| 66 | + await sdk.ParseInstallation.currentInstallation(); |
| 67 | + |
| 68 | + await parseInstallation.subscribeToChannel(value); |
| 69 | + } |
| 70 | + |
| 71 | + /// Unsubscribes the device to a channel of push notifications |
| 72 | + Future<void> unsubscribeFromChannel(String value) async { |
| 73 | + sdk.ParseInstallation parseInstallation = |
| 74 | + await sdk.ParseInstallation.currentInstallation(); |
| 75 | + |
| 76 | + await parseInstallation.unsubscribeFromChannel(value); |
| 77 | + } |
| 78 | + |
| 79 | + /// Returns an <List<String>> containing all the channel names this device is subscribed to |
| 80 | + Future<List<dynamic>> getSubscribedChannels() async { |
| 81 | + sdk.ParseInstallation parseInstallation = |
| 82 | + await sdk.ParseInstallation.currentInstallation(); |
| 83 | + |
| 84 | + return await parseInstallation.getSubscribedChannels(); |
| 85 | + } |
| 86 | +} |
0 commit comments