Skip to content

Commit a836d8e

Browse files
authored
feat: Add support for push notifications via ParsePush, ParseNotification (#914)
1 parent f8942ac commit a836d8e

File tree

6 files changed

+209
-3
lines changed

6 files changed

+209
-3
lines changed

packages/flutter/CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## [5.1.0](https://github.com/parse-community/Parse-SDK-Flutter/compare/flutter-5.0.1...flutter-5.1.0) (2023-05-22)
2+
3+
### Features
4+
5+
* Add support for push notifications via `ParsePush`, `ParseNotification` ([#914](https://github.com/parse-community/Parse-SDK-Flutter/pull/914))
6+
17
## [5.0.1](https://github.com/parse-community/Parse-SDK-Flutter/compare/flutter-5.0.0...flutter-5.0.1) (2023-05-20)
28

39
### Bug Fixes

packages/flutter/PUSH.md

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Push Notifications
2+
3+
Push notifications are a great way to keep your users engaged and informed about your app. You can reach your user base quickly and effectively. This guide will help you through the setup process and the general usage of Parse Platform to send push notifications.
4+
5+
To configure push notifications in Parse Server, check out the [push notification guide](https://docs.parseplatform.org/parse-server/guide/#push-notifications).
6+
7+
## Installation
8+
9+
1. Install [Firebase Core](https://firebase.flutter.dev/docs/overview) and [Cloud Messaging](https://firebase.flutter.dev/docs/messaging/overview). For more details review the [Firebase Core Manual](https://firebase.flutter.dev/docs/manual-installation/).
10+
11+
2. Add the following code after `Parse().initialize(...);`:
12+
13+
```dart
14+
ParsePush.instance.initialize(FirebaseMessaging.instance);
15+
FirebaseMessaging.onMessage.listen((message) => ParsePush.instance.onMessage(message));
16+
```
17+
18+
3. For you app to process push notification while in the background, add the following code:
19+
20+
```dart
21+
FirebaseMessaging.onBackgroundMessage(onBackgroundMessage);
22+
```
23+
24+
```dart
25+
Future<void> onBackgroundMessage(RemoteMessage message) async => ParsePush.instance.onMessage(message);
26+
```
27+
28+
## Implementation Example
29+
30+
The following is a code example for a simple implementation of push notifications:
31+
32+
```dart
33+
Future<void> main() async {
34+
WidgetsFlutterBinding.ensureInitialized();
35+
36+
// Initialize Firebase Core
37+
await Firebase.initializeApp(
38+
options: DefaultFirebaseOptions.currentPlatform,
39+
);
40+
41+
// Initialize Parse
42+
await Parse().initialize("applicationId", "serverUrl",
43+
clientKey: "clientKey", debug: true);
44+
45+
// Initialize Parse push notifications
46+
ParsePush.instance.initialize(FirebaseMessaging.instance);
47+
FirebaseMessaging.onMessage
48+
.listen((message) => ParsePush.instance.onMessage(message));
49+
50+
// Process push notifications while app is in the background
51+
FirebaseMessaging.onBackgroundMessage(onBackgroundMessage);
52+
53+
runApp(const MyApp());
54+
}
55+
56+
Future<void> onBackgroundMessage(RemoteMessage message) async =>
57+
ParsePush.instance.onMessage(message);
58+
59+
class MyApp extends StatelessWidget {
60+
const MyApp({super.key});
61+
62+
// This widget is the root of your application.
63+
@override
64+
Widget build(BuildContext context) {
65+
return MaterialApp(
66+
title: 'Flutter Demo',
67+
theme: ThemeData(
68+
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
69+
useMaterial3: true,
70+
),
71+
home: const MyHomePage(title: 'Flutter Demo Home Page'),
72+
);
73+
}
74+
}
75+
...
76+
```

packages/flutter/lib/parse_server_sdk_flutter.dart

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
library flutter_parse_sdk_flutter;
22

3+
import 'dart:convert';
34
import 'dart:async';
45
import 'dart:io';
5-
6+
import 'dart:math';
7+
import 'package:awesome_notifications/awesome_notifications.dart';
68
import 'package:connectivity_plus/connectivity_plus.dart';
79
import 'package:flutter/foundation.dart';
810
import 'package:flutter/material.dart';
@@ -20,6 +22,8 @@ export 'package:parse_server_sdk/parse_server_sdk.dart'
2022
part 'src/storage/core_store_sp_impl.dart';
2123
part 'src/utils/parse_live_grid.dart';
2224
part 'src/utils/parse_live_list.dart';
25+
part 'src/notification/parse_notification.dart';
26+
part 'src/push//parse_push.dart';
2327

2428
class Parse extends sdk.Parse
2529
with WidgetsBindingObserver
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
part of flutter_parse_sdk_flutter;
2+
3+
class ParseNotification {
4+
static final ParseNotification instance = ParseNotification._internal();
5+
static String keyNotificationChannelName = "parse";
6+
7+
factory ParseNotification() {
8+
return instance;
9+
}
10+
11+
ParseNotification._internal() {
12+
// Initialize notifications helper package
13+
AwesomeNotifications().initialize(
14+
null,
15+
[
16+
NotificationChannel(
17+
channelKey: keyNotificationChannelName,
18+
channelName: keyNotificationChannelName,
19+
channelDescription: 'Notification channel for parse')
20+
],
21+
);
22+
}
23+
24+
/// Show notification
25+
void showNotification(title) {
26+
AwesomeNotifications().createNotification(
27+
content: NotificationContent(
28+
id: Random().nextInt(1000),
29+
channelKey: keyNotificationChannelName,
30+
title: title,
31+
));
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
}

packages/flutter/pubspec.yaml

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: parse_server_sdk_flutter
22
description: The Flutter SDK to connect to Parse Server. Build your apps faster with Parse Platform, the complete application stack.
3-
version: 5.0.1
3+
version: 5.1.0
44
homepage: https://github.com/parse-community/Parse-SDK-Flutter
55

66
environment:
@@ -10,7 +10,7 @@ dependencies:
1010
flutter:
1111
sdk: flutter
1212

13-
parse_server_sdk: ^5.1.0
13+
parse_server_sdk: ^5.1.1
1414
# Uncomment for local testing
1515
#parse_server_sdk:
1616
# path: ../dart
@@ -28,6 +28,7 @@ dependencies:
2828
sembast: ^3.4.0+6
2929
sembast_web: ^2.1.0+4
3030
path: ^1.8.2 # required for transitive use only
31+
awesome_notifications: ^0.7.4+1
3132

3233
dev_dependencies:
3334
flutter_lints: ^2.0.1

0 commit comments

Comments
 (0)