Send GetSocial Notifications from Android SDK¶
Prerequisite¶
- Finished Getting Started with GetSocial Android SDK guide.
Send Notifications¶
You can send GetSocial Notification to any user in you application. To send notification you have to:
- Create a list of receivers. The list should contain GetSocial user IDs.
- Create a notification content.
```java tab=”Java”
// The list of GetSocial user IDs whom we will send notification to
List
// The content of the notification
MediaAttachment media = … ; // Create and instance of MediaAttachment from image or video
Action action = … ; // Create an Action instance
NotificationContent notificationContent = NotificationContent
.notificationWithText(“Notification text”)
.withTitle(“Greetings!”)
.withTemplateName(“template-name”)
.withAction(action)
.withMediaAttachment(media);
GetSocial.User.sendNotification(receivers, notificationContent, new Callback
@Override
public void onSuccess(NotificationsSummary result) {
Log.d(“GetSocial”, “Successfully sent ” + result.getSuccessfullySentCount() + ” notifications!”);
}
1 2 3 4 |
|
});
kotlin tab=”Kotlin”
// The list of GetSocial user IDs whom we will send notification to
var receivers: Array
// The content of the notification
val media : MediaAttachment = … // Create and instance of MediaAttachment from image or video
val action : Action = … // Create an Action instance
val notificationContent = NotificationContent
.notificationWithText(“Notification text”)
.withTitle(“Greetings!”)
.withTemplateName(“template-name”)
.withAction(action)
.withMediaAttachment(media)
GetSocial.User.sendNotification(receivers, notificationContent, object : Callback
override fun onSuccess(result: NotificationsSummary) {
println(“Successfully sent ${result.successfullySentCount} notifications!”)
}
override fun onFailure(exception: GetSocialException) {
println(“Failed to send notification, error: ${exception.message}”)
}
})
```
Receivers¶
- Receivers list can not be empty.
-
It may contain up to 25 unique user IDs and one or many placeholders from
SendNotificationPlaceholders.Receivers
.```java tab=”Java”
Listreceivers = Arrays.asList(
SendNotificationPlaceholders.Receivers.REFERRED_USERS,
SendNotificationPlaceholders.Receivers.REFERRER,
“[put user 1 id here]”,
“[put user 2 id here]”);
```kotlin tab="Kotlin" val receivers = listOf( SendNotificationPlaceholders.Receivers.REFERRED_USERS, SendNotificationPlaceholders.Receivers.REFERRER, "[put user 1 id here]", "[put user 2 id here]")
-
If you will send more that 25 user IDs - method will fail and notification will not be sent.
- If you mentioned one user twice or user is in two or more placeholder groups - the notification will be sent only once.
- If our service can not send the notification to one or many users, it will be delivered to all other users. You can check the number of successfully sent notifications in the response.
Notification Content¶
There are two ways to create a notification content:
-
Create all the content on the client side. You can customize notification text (mandatory), title, configure notification click action and attach media or add action buttons.
```java tab=”Java”
NotificationContent notificationContent = NotificationContent
.notificationWithText(“Notification text”);
```kotlin tab="Kotlin" val notificationContent = NotificationContent .notificationWithText("Notification text")
-
Use templates provided by GetSocial or create your custom template on the GetSocial Dashboard. You can override any content in the template dynamically on the client side.
```java tab=”Java”
NotificationContent notificationContent = NotificationContent
.notificationFromTemplate(“template-name”);
```kotlin tab="Kotlin" val notificationContent = NotificationContent .notificationFromTemplate("template-name")
Action¶
To set an action to the notification click, use notification.withAction(Action action)
.
```java tab=”Java”
Action action = Action.builder(ActionTypes.OPEN_INVITES).build();
NotificationContent notificationContent = NotificationContent.notificationWithText(“Invite friends!”)
.withAction(action);
```kotlin tab="Kotlin"
val action = Action.builder(ActionTypes.OPEN_INVITES).build()
val notificationContent = NotificationContent.notificationWithText("Invite friends!")
.withAction(action)
Read how to create actions.
Action Buttons¶
To add an action button use notification.addActionButton(ActionButton button)
or notification.addActionButtons(List<ActionButton> actionButtons)
to add multiple buttons at once.
```java tab=”Java”
NotificationContent notificationContent = NotificationContent.notificationWithText(“Add ” + SendNotificationPlaceholders.CustomText.SENDER_DISPLAY_NAME + ” to friends”)
.withAction(Action.builder(“add_friend”).build())
.addActionButton(ActionButton.create(“Accept”, ActionButton.CONSUME_ACTION)
.addActionButton(ActionButton.create(“Decline”, ActionButton.IGNORE_ACTION);
```kotlin tab="Kotlin"
val notificationContent = NotificationContent
.notificationWithText("Add ${SendNotificationPlaceholders.CustomText.SENDER_DISPLAY_NAME} to friends")
.withAction(Action.builder("add_friend").build())
.addActionButton(ActionButton.create("Accept", ActionButton.CONSUME_ACTION))
.addActionButton(ActionButton.create("Decline", ActionButton.IGNORE_ACTION))
Media Attachment¶
It’s possible to send image and video content in notification. To set image or video use NotificationContent.withMediaAttachment()
:
```java tab=”Java”
MediaAttachment attachment = … ;
NotificationContent notificationContent = NotificationContent.notificationWithText(“Check this cool image!”)
.withMediaAttachment(attachment);
```kotlin tab="Kotlin"
val attachment : MediaAttachment = ...
val notificationContent = NotificationContent.notificationWithText("Check this cool image!")
.withMediaAttachment(attachment)
Check full Media Attachment guide to learn about supported image, GIF and video formats and limitations.
Notification Templates¶
To create a template for notifications:
-
Login to the GetSocial Dashboard.
-
Go to the Notifications section → Templates tab.
-
Press New Template button.
-
Create a new template by giving a unique name and meaningful description.
-
Setup the notification content. You can add translations, emojis, default placeholders (Sender/Receiver display name) or custom placeholders that can be replaced on the SDK. Also you can set the fallback value for each placeholder which will be used if it wasn’t sent from the SDK side.
-
To check the list of your custom templates switch to Custom tab using radio button.
Now create and setup notification using GetSocial Android SDK:
```java tab=”Java”
// Text for the template “new_level_achieved” on the Dashboard:
// “Your friend [SENDER_DISPLAY_NAME] just reached [USER_LEVEL] lvl! Try to beat his score!”
NotificationContent notification = NotificationContent.notificationFromTemplate(“new_level_achieved”);
… // set up your notification
notification.addTemplatePlaceholder(“USER_LEVEL”, “7”); // add replacement for your placeholders without brackets
GetSocial.User.sendNotification(users, notification, callback);
// Your recipients will receive text:
// “Your friend John Doe just reached 7 lvl! Try to beat his score!”
```kotlin tab="Kotlin"
// Text for the template "new_level_achieved" on the Dashboard:
// "Your friend [SENDER_DISPLAY_NAME] just reached [USER_LEVEL] lvl! Try to beat his score!"
val notificationContent = NotificationContent.notificationFromTemplate("new_level_achieved")
... // set up your notification
notificationContent.addTemplatePlaceholder("USER_LEVEL", "7") // add replacement for your placeholders without brackets
GetSocial.User.sendNotification(users, notificationContent, callback)
// Your recipients will receive text:
// "Your friend John Doe just reached 7 lvl! Try to beat his score!"
[SENDER_DISPLAY_NAME]
is automatically replaced with sender display name. You can check all possible placeholders in SendNotificationPlaceholders.CustomText
.
Notification Sounds¶
Sound customization is supported via Templates only. But first you have to add sound file to your application:
- Prepare your audio file. It should not contain any special symbols or spaces in its name.
- Add your file to raw resources of your application by putting it into your application’s
res/raw
directory. - After file is added you should create a template on the Dashboard and specify this file name without extension in Android Sound configuration.
- Build the application and run it on a device.
- Send notification to the user authenticated on this device. You can send notification via Smart Targeting or SDK. When the notification arrives to the device you should hear custom sound instead of the default one. Make sure your device is not muted and volume is on.
Badges¶
You can send badges only to iOS users, on Android it will be ignored.
- Make sure you’ve completed setup guide for badges for your iOS application.
-
Create badge:
- To send a specific value use
NotificationContent.Badge.setTo(3)
- this will set badge on the recepient side to3
. - To increase/decrease badge value use
NotificationContent.Badge.increaseBy(1)
. For example if badge was5
, it will be set to6
after this push notification arrives.
- To send a specific value use
-
Add badge to notification content:
notificationContent.withBadge(badge)
.
Example - send notification and increase badge by 3
:
```java tab=”Java”
NotificationContent notification = NotificationContent.notificationWithText(“You have 3 new messages!”)
.withBadge(NotificationContent.Badge.increaseBy(3));
GetSocial.User.sendNotification(users, notification, callback);
```kotlin tab="Kotlin"
val notification = NotificationContent.notificationWithText("You have 3 new messages!")
.withBadge(GetSocialNotificationBadge.increaseBy(3))
GetSocialUser.sendNotification(receivers, with: notification, success: onSuccess, failure: onFailure)
Badge is cleared automatically when application is opened. If you want to clear a badge along with push notification, use setTo
with value 0
.