Push Notification
Castled uses Google Firebase Cloud Messaging(FCM) to send push notifications to Android devices. The following steps will walk you through the process of setting up your application to receive push notification from Castled.
Quick Demo
Enabling Push Notification in SDK
Set the flag enablePush
to true
when initializing the SDK
Permissions
Starting from Android 13 (API level 33), a new runtime permission, POST_NOTIFICATIONS
, has been introduced for apps to send non-exempt notifications, including those from Foreground Services (FGS).
You need to declare following permissions in the App’s manifest file.
Once the manifest file is updated, app should request the user for the permission at runtime. Here are some guidelines on requesting runtime permissions.
Firebase push messaging handling service
By default SDK takes care of FCM push token fetch and notification handling once app gets the permission to post notification. In case the app has registered its own Firebase message service class for handling FCM token, SDK needs to be notified of the new token and any push notification received from Castled platform. This can be done as follows:
Your app manifest would have a service class similar to MyAppMessagingService
to handle Firebase messaging events.
Add the following code snippets in the overrided onNewToken
and onMessageReceived
methods of your FCM service class.
Notification default configs
SDK provides option to specify default values for some of the push notification
payload fields. You need to create a castled.xml
file in the res/values
folder of your app project. Default values takes effect only if push payload doesn’t
have any values specified for these fields.
Notification channels
Add the following values to castled.xml
if you want to specify a default
channel for notifications from Castled. More details on android notifications
channels can be found here.
YOUR_NOTIFICATION_CHANNEL_IMPORTANCE_VALUE
can be any integer in the range [1,4]
, 1
being lowest importance and 5
being highest.
Small and large default icons
You can also optionally specify default small and large icons for push notifications in castled.xml
.
1. small icon 2. large icon
Notification Listeners
App can listen to Castled push notification events by registering a listener. Listener object should implement the interface CastledPushNotificationListener
.
Following code snippet uses an anonymous object as the listener.
FAQ
How to see all the logs from Castled SDK
How to see all the logs from Castled SDK
You can view all messages logged by the SDK in Android Studio Logcat using the filter tag CastledNotifications
Push notification is not showing in the device after sending from Castled dashboard
Push notification is not showing in the device after sending from Castled dashboard
There can be a lot of reasons why this could happen after a successfull SDK initialization. You can use the following checklist as a starter for debugging this issue.
- Ensure mobile device has network connection.
- Make sure
enablePush
config is set totrue
. - Firebase configs are setup in Castled dashboard.
- Make sure app has granted push notification permission. You can navigate to Settings > Notifications > App Notifications > Your App to see the notification permission status.
- If you are running the app in debug mode, make sure app is running. It can be in focus or out of focus.
- If sending notification using a Campaign or Workflow, check out the
Reports section for the Campaign or Workflow. If
Skipped
counter is non zero, it likely means SDK was not able to send push token to Castled server. Try removing the app and re-installing. If problem persists contact support. - Verify that the app is not force stopped. For e.g. some devices
have energy saving mode enabled that could kill the app. You can verify the
app status by running the following command in adb shell.
shell adb shell dumpsys package com.company.myapp | grep stopped
App state should bestopped=false
to receive push. - If you already tried the above checklist and the problem still persists, reach out to
tech@castled.io
or ping us in our Slack channel.