Enable Push Notifications
Learn how to enable Android push notifications in Brand Messenger Legacy's SDK
The SDK supports Firebase Cloud Messaging (FCM) for push notifications. See FCM Architectural Overview to learn more about the service and requirements for implementation.
To get started with push notifications with FCM, you'll need to:
- Generate an FCM configuration file for your project
- Send your Server API Key and Sender ID to Khoros
- Create a notification service class
- Add your service to the application's manifest
Generate a FCM configuration file for your Android project
Enable Cloud Messaging for your app and create a server API key.
- Go to Google’s Firebase Console.
- If you do not have a project already, create a new one.
- Once created, click Add Firebase to your Android app and follow the instructions to generate the google-services.json file. For your package name, copy and paste the package used in your application’s AndroidManifest.xml file.
- Save the google-services.json file generated by the tool at the root of your Android application (//google-services.json).
- Follow the Gradle instructions and modify your build.gradle files.
The project-level build.gradle (/build.gradle):
buildscript {
dependencies {
// Add this line
classpath 'com.google.gms:google-services:3.2.0'
}
}
Module-level build.gradle (//build.gradle):
dependencies {
// Add this line
compile 'com.google.firebase:firebase-core:12.0.1'
}
...
// Add to the bottom of the file
apply plugin: 'com.google.gms.google-services'
Finally, select Sync Now in the bar that appears in the IDE.
If your app has its own token registration, you need to trigger Brand Messenger Legacy push notifications by calling the
triggerBrandMessengerNotification
method onFcmServiceclass
.
Send your Server API Key and Sender ID to Khoros
Send your Server API Key and Sender ID to Khoros Support. To retrieve the key and ID, click on the Settings icon (a cogwheel) next to Project Overview on the sidebar, and then select the Cloud Messaging tab).
Create a Notification Service Class
After FCM enablement and configuration, create a notification service class and extend FirebaseMessagingService.
public class NotificationService extends FirebaseMessagingService {
@Override
public void onMessageReceived(final RemoteMessage message) {
final Map data = message.getData();
FcmService.triggerBrandMessengerNotification(data, this);
}
}
Add Your Service to the Application's Manifest
Add your created service to the Manifest.xml under application.
Here is an example:
<service
android:name="PACKAGE_NAME.NotificationService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
If you are using Android Nougat or later, include the following to ensure your app's notifications are visible to the user and that user has the ability to turn them off in their settings.
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/default_notification_channel_id"/>
default_notification_channel_id
is the notification channel id which can be custom to your app.
Updated 10 months ago