Configuring push notifications
Configuring Firebase
Google Firebase is necessary to handle push notifications sent from Synerise.
- Follow the instructions in this article.
- Integrate the Firebase plugin with your Application.
Firebase Cloud Messaging
- Follow the instructions in this article.
- Integrate the FCM plugin with your Application.
Note: It is important that the Firebase plugin is initialized as early as possible in the application lifecycle and also after the Synerise SDK. Late initialization may result compilation problems.
Setting up - Android
Requirements
After configuring Firebase, add the google-services.json
file to your project/android/app catalog.
-
Add the google-services dependency to your project’s
build.gradle
file.dependencies { ... classpath 'com.google.gms:google-services:4.3.3' ... }
-
In your MainApplication class, include
FirebaseMessaging.instance.onTokenRefresh.listen((event) {
to listen for the changes of the Firebase token.
Setting up - iOS
Requirements
Configure handling Push Notifications in your application. See Apple Notifications.
Add a Firebase configuration file
Download GoogleService-Info.plist from Firebase to obtain Apple platforms config file (GoogleService-Info.plist).
Make sure the config file name is not appended with additional characters, like (2).
Move your config file into the root of your Xcode project. If prompted, select to add the config file to all targets.
If you have multiple bundle IDs in your project, you must associate each bundle ID with a registered app in the Firebase console so that each app can have its own GoogleService-Info.plist file.
Make sure your info.plist file contains the following snippet:
<false/> <key>FirebaseAppDelegateProxyEnabled</key> <false/>
Receiving push notifications
The following code shows how to receive push notifications.
In order to handle Synerise push notifications you need to:
Request for permission from the user:
await FirebaseMessaging.instance.requestPermission(
alert: true,
announcement: false,
badge: true,
carPlay: false,
criticalAlert: false,
provisional: false,
sound: true,
);
set Foreground Notification Presentation Options
FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
alert: true,
sound: true,
badge: true
);
then set Background Message handler like:
FirebaseMessaging.onBackgroundMessage(backgroundHandlerForFCM);
@pragma('vm:entry-point')
Future<void> backgroundHandlerForFCM(RemoteMessage message) async {
await Firebase.initializeApp();
await Synerise.initializer()
.withClientApiKey("622376f8-4a8f-8b24-d7f9-070fa956e963")
.withBaseUrl("https://api.snrapi.com")
.withDebugModeEnabled(true)
.init();
developer.log('log push flutter', name: 'onMessage background');
bool isPushSynerise = await Synerise.notifications.handleNotification(message.toMap());
print("push is synerise: $isPushSynerise");
If you're going to use other Firebase services in the background, such as Firestore, make sure you call `initializeApp` before using other Firebase services.
After that you can call the registerForPush methods using firebase getToken Token object to register for Synerise Push Notifications
FirebaseMessaging.instance.getToken().then((value) {
if (value != null) {
Synerise.notifications.registerForNotifications(value, true);
firebaseToken = value;
}
});
or register for push from the Firebase onChange Token listener
When the firebase token changes register the new token for Synerise Push Notifications.
FirebaseMessaging.instance.onTokenRefresh.listen((event) {
FirebaseMessaging.instance.getToken().then((value) {
if (value != null) {
Synerise.notifications.registerForNotifications(value, true);
}
});
});
onRegistrationRequired Notification listener
Sometimes Synerise SDK may send a callback to register for Push Notifications again.
Synerise.notifications.listener((listener) {
listener.onRegistrationRequired = () {
Synerise.notifications.registerForNotifications(firebaseToken!, true);
};
Handling Synerise push notifications
The following code shows how to handle push notifications:
-
Handle Notification onMessage listener
Handle the incoming Push Notification.
FirebaseMessaging.onMessage.listen((RemoteMessage message,) {
Synerise.notifications.handleNotification(message.toMap());
});
-
Handle Notification onMessageOpenedApp handleNotificationClick
Handle the Push Notification Click.
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
Synerise.notifications.handleNotificationClick(message.toMap());
});