-
-
Notifications
You must be signed in to change notification settings - Fork 208
feat: Add support for push notifications via ParsePush
, ParseNotification
#914
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mtrezza
merged 13 commits into
parse-community:master
from
mbfakourii:add_parse_notification
May 22, 2023
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
4ca5fdf
add parsePush and parseNotification
mbfakourii 4ad444e
add doc
mbfakourii 8fb97cd
Update PUSH.md
mbfakourii c3ece28
Formatting fixes
mtrezza 08b900d
style fixes
mtrezza 3c194af
format Implementation Example in PUSH.md
mbfakourii 814b72a
add number 3 in Installation
mbfakourii 114e348
add doc and vapidKey for web
mbfakourii f0d5e02
re-add intentation
mtrezza 34b393f
Update PUSH.md
mtrezza b458b48
style
mtrezza 857a4a4
bump version
mbfakourii 617903a
Update packages/flutter/CHANGELOG.md
mtrezza File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# Push Notifications | ||
|
||
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. | ||
|
||
To configure push notifications in Parse Server, check out the [push notification guide](https://docs.parseplatform.org/parse-server/guide/#push-notifications). | ||
|
||
## Installation | ||
|
||
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/). | ||
|
||
2. Add the following code after `Parse().initialize(...);`: | ||
|
||
```dart | ||
ParsePush.instance.initialize(FirebaseMessaging.instance); | ||
FirebaseMessaging.onMessage.listen((message) => ParsePush.instance.onMessage(message)); | ||
``` | ||
|
||
3. For you app to process push notification while in the background, add the following code: | ||
|
||
```dart | ||
FirebaseMessaging.onBackgroundMessage(onBackgroundMessage); | ||
``` | ||
|
||
```dart | ||
Future<void> onBackgroundMessage(RemoteMessage message) async => ParsePush.instance.onMessage(message); | ||
``` | ||
|
||
## Implementation Example | ||
|
||
The following is a code example for a simple implementation of push notifications: | ||
|
||
```dart | ||
Future<void> main() async { | ||
WidgetsFlutterBinding.ensureInitialized(); | ||
|
||
// Initialize Firebase Core | ||
await Firebase.initializeApp( | ||
options: DefaultFirebaseOptions.currentPlatform, | ||
); | ||
|
||
// Initialize Parse | ||
await Parse().initialize("applicationId", "serverUrl", | ||
clientKey: "clientKey", debug: true); | ||
|
||
// Initialize Parse push notifications | ||
ParsePush.instance.initialize(FirebaseMessaging.instance); | ||
FirebaseMessaging.onMessage | ||
.listen((message) => ParsePush.instance.onMessage(message)); | ||
|
||
// Process push notifications while app is in the background | ||
FirebaseMessaging.onBackgroundMessage(onBackgroundMessage); | ||
|
||
runApp(const MyApp()); | ||
} | ||
|
||
Future<void> onBackgroundMessage(RemoteMessage message) async => | ||
ParsePush.instance.onMessage(message); | ||
|
||
class MyApp extends StatelessWidget { | ||
const MyApp({super.key}); | ||
|
||
// This widget is the root of your application. | ||
@override | ||
Widget build(BuildContext context) { | ||
return MaterialApp( | ||
title: 'Flutter Demo', | ||
theme: ThemeData( | ||
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), | ||
useMaterial3: true, | ||
), | ||
home: const MyHomePage(title: 'Flutter Demo Home Page'), | ||
); | ||
} | ||
} | ||
... | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
packages/flutter/lib/src/notification/parse_notification.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
part of flutter_parse_sdk_flutter; | ||
|
||
class ParseNotification { | ||
static final ParseNotification instance = ParseNotification._internal(); | ||
static String keyNotificationChannelName = "parse"; | ||
|
||
factory ParseNotification() { | ||
return instance; | ||
} | ||
|
||
ParseNotification._internal() { | ||
// Initialize notifications helper package | ||
AwesomeNotifications().initialize( | ||
null, | ||
[ | ||
NotificationChannel( | ||
channelKey: keyNotificationChannelName, | ||
channelName: keyNotificationChannelName, | ||
channelDescription: 'Notification channel for parse') | ||
], | ||
); | ||
} | ||
|
||
/// Show notification | ||
void showNotification(title) { | ||
AwesomeNotifications().createNotification( | ||
content: NotificationContent( | ||
id: Random().nextInt(1000), | ||
channelKey: keyNotificationChannelName, | ||
title: title, | ||
)); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
part of flutter_parse_sdk_flutter; | ||
|
||
class ParsePush { | ||
static final ParsePush instance = ParsePush._internal(); | ||
|
||
static String keyType = "gcm"; | ||
static String keyPushType = 'pushType'; | ||
|
||
factory ParsePush() { | ||
return instance; | ||
} | ||
|
||
ParsePush._internal(); | ||
|
||
/// Initialize ParsePush; for web a [vapidKey] is required. | ||
Future<void> initialize( | ||
firebaseMessaging, { | ||
String? vapidKey, | ||
}) async { | ||
// Get Google Cloud Messaging (GCM) token | ||
firebaseMessaging | ||
.getToken(vapidKey: vapidKey) | ||
.asStream() | ||
.listen((event) async { | ||
// Set token in installation | ||
sdk.ParseInstallation parseInstallation = | ||
await sdk.ParseInstallation.currentInstallation(); | ||
|
||
parseInstallation.deviceToken = event; | ||
parseInstallation.set(keyPushType, keyType); | ||
|
||
await parseInstallation.save(); | ||
}); | ||
} | ||
|
||
/// Handle push notification message | ||
void onMessage(message) { | ||
String pushId = message.data["push_id"] ?? ""; | ||
String timestamp = message.data["time"] ?? ""; | ||
String dataString = message.data["data"] ?? ""; | ||
String channel = message.data["channel"] ?? ""; | ||
|
||
Map<String, dynamic>? data; | ||
try { | ||
data = json.decode(dataString); | ||
} catch (_) {} | ||
|
||
_handlePush(pushId, timestamp, channel, data); | ||
} | ||
|
||
void _handlePush(String pushId, String timestamp, String channel, | ||
Map<String, dynamic>? data) { | ||
if (pushId.isEmpty || timestamp.isEmpty) { | ||
return; | ||
} | ||
|
||
if (data != null) { | ||
// Show push notification | ||
ParseNotification.instance.showNotification(data["alert"]); | ||
} | ||
} | ||
|
||
/// Subscribes the device to a channel of push notifications | ||
Future<void> subscribeToChannel(String value) async { | ||
sdk.ParseInstallation parseInstallation = | ||
await sdk.ParseInstallation.currentInstallation(); | ||
|
||
await parseInstallation.subscribeToChannel(value); | ||
} | ||
|
||
/// Unsubscribes the device to a channel of push notifications | ||
Future<void> unsubscribeFromChannel(String value) async { | ||
sdk.ParseInstallation parseInstallation = | ||
await sdk.ParseInstallation.currentInstallation(); | ||
|
||
await parseInstallation.unsubscribeFromChannel(value); | ||
} | ||
|
||
/// Returns an <List<String>> containing all the channel names this device is subscribed to | ||
Future<List<dynamic>> getSubscribedChannels() async { | ||
sdk.ParseInstallation parseInstallation = | ||
await sdk.ParseInstallation.currentInstallation(); | ||
mtrezza marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return await parseInstallation.getSubscribedChannels(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.