Push Notifications for Android apps
Integrate Apptics gradle plugin
Push notifications are available from Apptics version BOM 0.2.15.0-beta2.
Apptics gradle plugin integration and configuration steps are common for all the modules. By now, you would have already integrated Apptics gradle plugin if you are using any other module. If not, refer to the integration guide.
Install notification SDK
Include Apptics' push notification dependency using Apptics BOM.
Copieddependencies {
implementation platform('com.zoho.apptics:apptics-bom:[latest-version]')
// Since BoM version is specified, no need to explicitly specify the dependency version.
implementation("com.zoho.apptics:apptics-pns")
}
- Alternatively, if you choose not to use Apptics BoM, you can use Apptics' push notification dependency directly by mentioning the version.
Copieddependencies {
implementation("com.zoho.apptics:apptics-pns":[latest-version]')
}
Note: We highly recommend using Apptics BoM to avoid unnecessary compatibility issues.
You can find the latest version of Apptics dependencies in the Release notes as well as the integration guide.
Register for Apptics push notifications
- Initialize the Apptics SDK in the application's onCreate method to register the device for Apptics Push Notifications. You would have already initialized Apptics if you are using any other modules.
Copiedclass AppDelegate: Application() {
override fun onCreate() {
super.onCreate()
Apptics.init(this)
}
}
Configure Apptics notification service
- Add AppticsMessagingService to your app's manifest.
- AppticsMessagingService extends the FirebaseMessagingService and it automatically handles all the incoming messages from Apptics.
Copied<service
android:name="com.zoho.apptics.pns.AppticsMessagingService"
android:stopWithTask="false"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT />
</intent-filter>
</service>
If your app already has it's own FirebaseMessagingService implementation, you should configure Apptics Notifications by using AppticsPushNotification.handleMessage() in the onMessageReceived() method and AppticsPushNotification.handleTokenRefresh() in the onNewToken() method. This approach ensures that Apptics can properly manage it's notification and token register events with requiring you to register AppticsMessagingService. Since, Firebase only permits a single MESSAGING_EVENT service, this solution allows both Apptics and your app's custom push notification handling to coexist seamlessly.
AppticsPushNotification.handleMessage() will return true if the received message is from Apptics and is handled by Apptics.
Copiedclass MyMessagingService: FirebaseMessagingService() {
override fun onMessageReceived(message: RemoteMessage) {
// message will be handled and will return true, if the source of this message is Apptics.
if (AppticsPushNotification.handleMessage(applicationContext, message)) {
return;
}
// handle your app's notification
}
override fun onNewToken(token: String) {
AppticsPushNotification.handleTokenRefresh(token)
}
}
Notification channel
Starting from Android 8 (Oreo) and above, apps must assign a notification channel for every notification. You can configure the notification channel for push notifications sent by Apptics through the Notification Builder in Apptics web console when publishing a notification.
You can also configure a default channel ID as meta-data in your AndroidManifest.xml, which will be used if no notification channel is specified in the Notification Builder.
Copiedclass MyMessagingService: FirebaseMessagingService() {
override fun onMessageReceived(message: RemoteMessage) {
// message will be handled and will return true, if the source of this message is Apptics.
if (AppticsPushNotification.handleMessage(applicationContext, message)) {
return;
}
// handle your app's notification
}
override fun onNewToken(token: String) {
AppticsPushNotification.handleTokenRefresh(token)
}
}
Apptics SDK posts notification with a built-in channel, if no channel ID is configured.
Notification icon
Notification icon (small) can be configured from the Apptics web console when publishing a notification.
You can also configure a default icon as the meta-data in your AndroidManifest.xml which will be used if notification icon is not available in the incoming message.
Copied<application>
<meta-data android:name="apptics_default_notification_icon" android:resource="@drawable/small_notif_icon" />
</application>
If both the default icon and icon from the incoming messages are not available, App icon will be used.
Notification color
Notification color can be configured from the Apptics web console when publishing a notification.
You can also configure a default color as the meta-data in your AndroidManifest.xml which will be used if the color is not available in the incoming message.
Copied<application>
<meta-data android:name="apptics_default_notification_color" android:resource="@color/notif_accent" />
</application>
Notification sound
From Android 8 (Oreo) and above, notification sounds are based on the notification channels. Apptics doesn't provide any default notification channels. Refer to Notification channels to assign channels to notifications sent by Apptics.
For Android 7 and below, you can configure the notification sound from the Apptics web console while publishing the notification.
Add your custom sound file in res/raw folder and provide the name of the file with extension in additional info while publishing notification.
Note: .3gp, .mp4, .wav are some of the supported container formats.
Notification click action
Notification click action (when a user taps the push notification) can be configured from the Apptics web console while publishing the notification.
You can pass a URL, which can be either deep link URL to open a specific screen of the app or an external URL which opens in a web browser.
You can also provide a custom implementation for click action, refer to AppticsNotificationListener.
Notification action button
Notification action buttons can be configured from the Apptics web console while publishing a notification. Similar to notification click action, you can also provide a click action for action buttons.
You can pass a URL, which can be a deep link URL or an external URL.
Notification listener
You can configure event listener to listen for notification open event. This listener will be triggered with click action and custom payload configured in the notification builder.
Make sure to listen for these events as early as possible in Application onCreate ().
Copiedclass AppDelegate: Application() {
override fun onCreate() {
super.onCreate()
Apptics.init()
AppticsPushNotification.notificationListener = object: AppticsPushNotificationListener {
override fun onNotificationClick(clickAction: String?) {
// your custom notification click action
}
override fun onNotificationActionClick(actionId: String) {
// your custom notication action click
}
}
}
}
Custom click actions
For both notification and action button click actions, you can configure event listeners to write your own click action handler. However, adding this event listener will not override the default notification open behavior. You can suppress the default behavior by adding the below meta-data in your AndroidManifest.xml file.
Copied<application>
<meta-data android:name="apptics_disable_notification_open_action" android:value="false" />
</application>
Upon adding the above, Apptics will not launch the app on notification or notification action clicks. Instead the notification event listeners will alone be fired where you can handle redirections appropriately.