Setup Push Notifications on Android¶
Prerequisite¶
- Finished Getting Started with GetSocial Android SDK guide.
Setup Android Push Notifications¶
GetSocial supports sending push notifications via Firebase Cloud Messaging (FCM).
- Follow FCM official guide.
- Login to the GetSocial Dashboard.
- Go to the Notifications section → Settings tab.
-
Enable Android notifications by clicking on the switch and fill in API Key and Sender ID values.
You can find API Key (Server key) and Sender ID in your application settings in Firebase developer console.
-
Go to Templates tab and select what kind of notifications users will receive:
-
Check notifications texts and translations.
-
Send a test notification to your test device to check your setup.
Autoregister for Push Notification on the Client Side¶
By default GetSocial SDK automatically registers at the push server, and the user will start receiving push notifications.
To prevent auto registration and register for push notifications manually:
-
Add
autoRegisterForPush
property to the GetSocial configuration in thegetsocial.json
:// getsocial.json { ... "pushNotifications": { ... "autoRegister": false } }
-
To start receiving GetSocial push notifications call:
Notifications.registerDevice();
If you’re not using GetSocial Gradle Plugin check how to disable auto registration for push notifications in the Manual Integration Guide.
Handle Click on Push Notifications¶
Select Activity to Open¶
By default, click on the GetSocial push notification will start launcher activity. If you want to open other activity on notification click, add the <intent-filter>
with action getsocial.intent.action.NOTIFICATION_RECEIVE
to the activity that should be opened:
<activity ...>
...
<intent-filter>
<action android:name="getsocial.intent.action.NOTIFICATION_RECEIVE"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
GetSocial UI¶
If you are using GetSocial UI, SDK will open corresponding view after clicking on GetSocial push notification. For instance, if someone liked your activity or commented under your activity, this activity will be shown on GetSocial notification click.
Important
If GetSocial View is opened automatically by GetSocial UI, you can not set the custom title, UiActionListener, action button handler, etc. In this case, we recommend you to override the default behavior and open GetSocial View by yourself.
Notification Listener¶
Receive Listener¶
You can set OnNotificationReceivedListener
, which will be triggered when notification is received and app is in foreground:
Notifications.setOnNotificationReceivedListener { notification: Notification ->
Log.d("Notifications", "Notification received: " + notification.text)
}
Click Listener¶
You can customize the default behavior and handle clicks on GetSocial push notifications on your own. To do so:
-
Set
customListener
property totrue
in the GetSocial configuration filegetsocial.json
:// getsocial.json { ... "pushNotifications": { ... "customListener": true } }
-
Set a listener:
Notifications.setOnNotificationClickedListener { notification, context -> Log.d("Notifications", "Notification clicked: " + notification.text) if (!handleAction(notification.action)) { GetSocial.handle(notification.action) // fallback to default behaviour if notification was not handled by your code } }
Read how to handle actions.
Configuration flag customListener
Don’t forget to set this flag, otherwise listener won’t be invoked. If you have it set to true
, GetSocial won’t perform any default handling, you have to call GetSocial.handle(...)
explicitly.
Tip
If you are not using GetSocial UI, we recommend to handle push notifications by yourself - better user experience would be to react to notification actions.
Show Push Notifications In Foreground¶
If you want to show GetSocial push notifications while app is in foreground - set foreground
to true
in the GetSocial configuration file getsocial.json
:
// getsocial.json
{
...
"pushNotifications": {
...
"foreground": true
}
}
OnNotificationReceivedListener
will still be invoked.
Disable Push Notifications For User¶
If you want to disable push notifications for the user, use Notifications.setPushNotificationsEnabled(false, callback, failure)
. When push notifications are disabled, you still can query for GetSocial Notifications via data API.
To enable push notifications use Notifications.setPushNotificationsEnabled(true, callback, failure)
. To check current setting value use Notifications.isPushNotificationsEnabled(callback, failure)
.
Customize Push Notification Icon¶
Notification icon has to be monochromic white image on the transparent background.
By default, GetSocial SDK will use application icon as the push notification icon.
There are two ways to customize the icon. The first option is to put an image with name getsocial_notification_icon
into res/drawable
folder.
If you want to use an existing icon for notifications, you can set the resource name in the getsocial.json
. For instance to use ic_notification_custom.png
from res/drawable
:
// getsocial.json
{
...
"pushNotifications": {
...
"android": {
...
"icon": "ic_notification_custom"
}
}
}
As a result all notifications coming from GetSocial SDK will use custom icon:
To change the color of the notification(in hex format):
// getsocial.json
{
...
"pushNotifications": {
...
"android": {
...
"color": "#FFCCBBDD"
}
}
}
Also, you can customize large notification icon using getsocial.json
. To use ic_large_notification_icon.png
from res/drawable
:
// getsocial.json
{
...
"pushNotifications": {
...
"android": {
...
"largeIcon": "ic_large_notification_icon"
}
}
}
Customize Push Notifications Channel¶
All GetSocial notifications come to a separate notifications channel with ID getsocial_channel_id
. By default, channel is called Social
and has empty description. Name is localized to all GetSocial supported languages.
To customize channel name or description, add the following meta data pointing to the string resource to getsocial.json
:
// getsocial.json
{
...
"pushNotifications": {
...
"android": {
...
"channelName": "getsocial_notifications_channel_title",
"channelDescription": "getsocial_notifications_channel_description"
}
}
}
You have to create the string resource in strings.xml
with the same name, it will be picked up. You can also localize it as any other string resource.
To check your customization, on your phone (Android O, API 26 and higher) go to Settings → Apps and Notifications → [YOUR APP NAME] → Notifications and find your channel. Description can be checked after pressing the channel name at the bottom of the screen.
Priority¶
By default GetSocial Push Notification Channel is created with High
priority. If you want to change it, add "priority"
property to getsocial.json
:
// getsocial.json
{
...
"pushNotifications": {
...
"android": {
...
"priority": "high",
...
}
}
}
Supported priorities:
urgent
: Makes a sound and appears as a heads-up notification.high
: Makes a sound. (Default)min
: No sound.low
: No sound and does not appear in the status bar.
Background Images¶
GetSocial supports adding a background image to a notification, which helps your notifications to stand out from the crowd of notifications.
Using Smart Templates¶
You can specify a background image when you create a Smart Targeting template:
Besides configuring the background image, you can also define the color of the title and text.
Using SDK¶
It is also possible to send a notification using GetSocial SDK:
val notificationContent = NotificationContent
.notificationWithText("Wow, what a background!")
val backgroundImageUrl = ... // url of the background image
val customization = NotificationCustomization
.withBackgroundImageConfiguration(backgroundImageUrl)
.withTitleColor("#000000") // set color as ARGB string
.withTextColor("#000000"); // set color as ARGB string
notificationContent.withCustomization(customization)
Notifications.sendNotification... // send notification
Best Practices
Add your application’s icon to the left side of the background image, so your users will know immediately to which application it belongs.
To support portrait and landscape mode with all the possible devices we suggest to set an image in 2600x256 size.
Next Steps¶
- Send targeted notifications from GetSocial Dashboard
- Send user to user notifications from the client
- Understand push notifications Analytics.