Skip to content

Send GetSocial Notifications from SDK

Prerequisite

  • Finished Getting Started with GetSocial SDK.

Send Notifications

You can send GetSocial Notification to any user in you application. To send notification you have to:

  1. Create a list of receivers. The list should contain GetSocial user IDs.
  2. Create a notification content.
// The list of GetSocial user IDs whom we will send notification to
val receivers: UserIdList = ...
val notificationContent = NotificationContent.notificationWithText("Notification text")
                .withTitle("Greetings!")
                .withAction(action)
                .withMediaAttachment(attachment)

val target = SendNotificationTarget.users(receivers)

Notifications.send(notificationContent, target, {
    Log.d("Notifications", "Successfully sent notifications")
}, { error: GetSocialError ->
    Log.d("Notifications", "Failed to send notifications: $error")
})
// The list of GetSocial user IDs whom we will send notification to
let receivers : [String] = ...
let notificationContent = NotificationContent.withText("Notification text")
notificationContent.title = "Greetings!"
notificationContent.action = action
notificationContent.mediaAttachment = attachments

let target = SendNotificationTarget.users(UserIdList.create(receivers))

Notifications.send(notificationContent, target: target, success: {
    print("Successfully sent notifications!")
}, failure: { error in
    print("Failed to send notifications, error: \(error)")
})
// The list of GetSocial user IDs whom we will send notification to
List<string> receivers = null;
var notificationContent = NotificationContent.CreateWithText("Notification text");
notificationContent.WithTitle("Greetings");
notificationContent.WithAction(action);
notificationContent.WithMediaAttachment(mediaAttachment);

var target = SendNotificationTarget.Users(UserIdList.Create(receivers));

Notifications.Send(notificationContent, target,
    () => {
        Debug.Log("Successfully sent notifications!");
    },
    (error) => {
        Debug.Log("Failed to send notifications, error: " + error);
    });
// The list of GetSocial user IDs whom we will send notification to
const receivers: string[] = ... ;

// The content of the notification
const mediaAttachment: MediaAttachment = ... ; // Create and instance of MediaAttachment from image or video
const action: Action = ... ; // Create an Action instance
const notificationContent = NotificationContent.withText('Notification text');
notificationContent.title = 'Greetings!';
notificationContent.action = action;
notificationContent.mediaAttachment = media;

const target = SendNotificationTarget.usersWithIds(UserIdList.create(receivers));
Notifications.send(notificationContent, target).then(() => {
console.log('Successfully sent notifications!');
}, (error) => {
console.log('Failed to send notification.');
});
// The list of GetSocial user IDs whom we will send notification to
var receivers = List<String>();

// The content of the notification
var mediaAttachment = ... ; // Create and instance of MediaAttachment from image or video
var action = ... ; // Create a GetSocialAction instance
var notificationContent = NotificationContent();
notificationContent.text = 'Notification text';
notificationContent.title = 'Greetings!';
notificationContent.action = action;
notificationContent.mediaAttachment = media;

var target = SendNotificationTarget();
target.userIdList = UserIdList.create(receivers);
Notifications.send(notificationContent, target)
    .then((result) => print('Successfully sent notifications!'))
    .catchError((onError) => print('Failed to send notification.'));
// The list of GetSocial user IDs whom we will send notification to
const receivers = ['userId'] ;

// The content of the notification

// Create and instance of MediaAttachment from image or video
const mediaAttachment = GetSocialSDK.MediaAttachment.withImageFile(file);
// Create an Action instance
const action = GetSocialSDK.Action.create('custom_action', { key: 'value'});
const notificationContent = GetSocialSDK.NotificationContent.withText('Notification text');
notificationContent.title = 'Greetings!';
notificationContent.action = action;
notificationContent.mediaAttachment = mediaAttachment;

const target = GetSocialSDK.SendNotificationTarget.usersWithIds(GetSocialSDK.UserIdList.create(receivers));

GetSocialSDK.Notifications.send(notificationContent, target)
    .then(() => {
        console.log('Successfully sent notifications!');
    }, (error) => {
        console.log('Failed to send notifications.');
    });

Receivers

  • Receivers list can not be empty.
  • It may contain up to 25 unique user IDs and one or few placeholders:

    val target = SendNotificationTarget.users(UserIdList.create("user_id_1", "user_id_2"))
                    .addPlaceholder(NotificationReceiverPlaceholders.FRIENDS)
                    .addPlaceholder(NotificationReceiverPlaceholders.REFERRED_USERS)
                    .addPlaceholder(NotificationReceiverPlaceholders.FOLLOWERS)
    
    let target = SendNotificationTarget.users(UserIdList.create(["user1","user2"]))
        .addReceiver(NotificationReceiverPlaceholders.friends)
        .addReceiver(NotificationReceiverPlaceholders.referredUsers)
        .addReceiver(NotificationReceiverPlaceholders.followers)
    
    var target = SendNotificationTarget.Users(UserIdList.Create("user1", "user2"))
        .AddPlaceholder(NotificationReceiverPlaceholders.Friends)
        .AddPlaceholder(NotificationReceiverPlaceholders.ReferredUsers)
        .AddPlaceholder(NotificationReceiverPlaceholders.Followers);
    
    const target = SendNotificationTarget.usersWithIds(UserIdList.create(['user1', 'user2']))
        .addReceiverPlaceholder('friends')
        .addReceiverPlaceholder('referrer')
        .addReceiverPlaceholder('followers');
    
    var target = SendNotificationTarget();
    target.userIdList = UserIdList.create('user1');
    target.receiverPlaceholders.add(NotificationReceiverPlaceholder.friends);
    target.receiverPlaceholders.add(NotificationReceiverPlaceholder.referrer);
    target.receiverPlaceholders.add(NotificationReceiverPlaceholder.followers)
    
    const target = GetSocialSDK.SendNotificationTarget.usersWithIds(GetSocialSDK.UserIdList.create(['user1', 'user2']))
        .addReceiverPlaceholder('friends')
        .addReceiverPlaceholder('referrer')
        .addReceiverPlaceholder('followers');
    
  • 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:

  1. Create all the content on the client side. You can customize notification text (mandatory), title, configure notification click action and attach media.

    val notificationContent = NotificationContent.notificationWithText("Notification text")
    
    let notificationContent = NotificationContent.withText("Notification text")
    
    var notificationContent = NotificationContent.CreateWithText("Notification text");
    
    const notificationContent = NotificationContent.withText('Notification text');
    
    var notificationContent = NotificationContent();
    notificationContent.text = 'Notification text';
    
    const notificationContent = GetSocialSDK.NotificationContent.withText('Notification text');
    
  2. 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.

    val notificationContent = NotificationContent.notificationFromTemplate("template-name")
    
    let notificationContent = NotificationContent.withTemplate("template-name")
    
    var notificationContent = NotificationContent.CreateWithTemplate("template-name");
    
    const notificationContent = NotificationContent.withTemplate('template-name');
    
    var notificationContent = NotificationContent();
    notificationContent.templateName = 'template-name';
    
    const notificationContent = GetSocialSDK.NotificationContent.withTemplate('template-name');
    

Action

To set an action to the notification click:

val action = Action.create(ActionTypes.OPEN_INVITES)
val notificationContent = NotificationContent.notificationWithText("Invite Friends")
                .withAction(action)
let action = Action(ActionType.openInvites)
let notificationContent = NotificationContent.withText("Invite Friends")
notificationContent.action = action
var action = GetSocialAction.Create(GetSocialActionType.OpenInvites, null);
var notificationContent = NotificationContent.CreateWithText("Invite Friends");
notificationContent.WithAction(action);
const action = Action.create('open_invites', {});
const notificationContent = NotificationContent.withText('Invite friends!');
notificationContent.withAction(action);
var action = GetSocialAction('action', {});
var notificationContent = NotificationContent();
notificationContent.text = 'Push notification with Action';
notificationContent.action = action;
const action = GetSocialSDK.Action.create('open_invites', {});
const notificationContent = GetSocialSDK.NotificationContent.withText('Invite friends!');
notificationContent.action = action;

Check GetSocial Actions guide for Android and iOS.

Action Buttons

To add an action button:

val action = Action.create(ActionTypes.ADD_FRIEND, mapOf(ActionDataKeys.AddFriend.USER_ID to GetSocial.getCurrentUser()!!.id))

val notificationContent = NotificationContent.notificationWithText("Add ${SendNotificationPlaceholders.CustomText.SENDER_DISPLAY_NAME} to friends")
                .withAction(action)
                .addActionButton(NotificationButton.create("Accept", NotificationButton.CONSUME_ACTION))
                .addActionButton(NotificationButton.create("Decline", NotificationButton.IGNORE_ACTION))
let action = Action.create(type: ActionType.addFriend, data: [ActionDataKey.addFriend_UserId: GetSocial.currentUser()!.userId])
let notificationContent = NotificationContent.withText("Add \(NotificationContentPlaceholders.senderDisplayName) to friends")
notificationContent.action = action
let acceptButton = NotificationButton(title: "Accept", actionId: NotificationButton.actionIdConsume)
let declineButton = NotificationButton(title: "Decline", actionId: NotificationButton.actionIdIgnore)
notificationContent.actionButtons = [acceptButton, declineButton]
var actionData = new Dictionary<string, string>() {
    { GetSocialActionKeys.AddFriend.UserId, GetSocial.GetCurrentUser().Id }
};
var action = GetSocialAction.Create(GetSocialActionType.AddFriend, actionData);
var notificationContent = NotificationContent.CreateWithText("Add " + SendNotificationPlaceholders.CustomText.SenderDisplayName + " to friends");
notificationContent.WithAction(action);
var acceptButton = NotificationButton.Create("Accept", NotificationButton.ConsumeAction);
var declineButton = NotificationButton.Create("Decline", NotificationButton.IgnoreAction);
notificationContent.AddActionButton(acceptButton);
notificationContent.AddActionButton(declineButton);
const notificationContent = NotificationContent.withText('Add [SENDER_DISPLAY_NAME] to friends');
notificationContent.action = new Action('add_friend', {'$user_id', userId});
notificationContent.buttons.push(NotificationButton.create('Accept', 'consume'));
notificationContent.buttons.push(NotificationButton.create('Decline', 'ignore'));
var notificationContent = NotificationContent();
notificationContent.text = 'Add [SENDER_DISPLAY_NAME] to friends';
notificationContent.action = GetSocialAction('add_friend', {'$user_id', userId});
notificationContent.actionButtons.add(NotificationButton('Accept', 'consume'));
notificationContent.actionButtons.add(NotificationButton('Decline', 'ignore'));
const notificationContent = GetSocialSDK.NotificationContent.withText('Add [SENDER_DISPLAY_NAME] to friends');
notificationContent.action = GetSocialSDK.Action.create('add_friend', {'$user_id': userId});
notificationContent.actionButtons.push(GetSocialSDK.NotificationButton.create('Accept', 'consume'));
notificationContent.actionButtons.push(GetSocialSDK.NotificationButton.create('Decline', 'ignore'));

Media Attachment

It’s possible to send image and video content in notification. To set image or video:

val mediaAttachment: MediaAttachment = ...
val notificationContent = NotificationContent.notificationWithText("Check this cool image")
                .withMediaAttachment(mediaAttachment)
let mediaAttachment: MediaAttachment = ...
let content = NotificationContent.withText("Check this cool image")
content.mediaAttachment(mediaAttachment)
MediaAttachment mediaAttachment = ...
var notificationContent = NotificationContent.CreateWithText("Check this cool image");
notificationContent.WithMediaAttachment(mediaAttachment);
const mediaAttachment = ... ;
const notificationContent = NotificationContent.withText('Check this cool image!');
notificationContent.mediaAttachment = mediaAttachment;
var mediaAttachment = ... ;
var notificationContent = NotificationContent();
notificationContent.mediaAttachment = mediaAttachment;
const mediaAttachment = ... ;
const notificationContent = GetSocialSDK.NotificationContent.withText('Check this cool image!');
notificationContent.mediaAttachment = mediaAttachment;

Check full Media Attachment guide to learn about supported image, GIF and video formats and limitations.

Notification Templates

To create a template for notifications:

  1. Login to the GetSocial Dashboard.

  2. Go to the Notifications section → Templates tab.

  3. Press New Template button.

    GetSocial Dashboard - Create New Template

  4. Create a new template by giving a unique name and meaningful description.

  5. 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.

  6. To check the list of your custom templates switch to Custom tab using radio button.

Now create and setup notification using GetSocial Android SDK:

// 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")
                .addTemplatePlaceholder("USER_LEVEL", "7")  // add replacement for your placeholders without brackets

Notifications.send(notificationContent, target, {
    Log.d("Notifications", "Successfully sent notifications")
}, { error: GetSocialError ->
    Log.d("Notifications", "Failed to send notifications: $error")
})
// Your recipients will receive text:
// "Your friend John Doe just reached 7 lvl! Try to beat his score!"
// 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!"

let notificationContent = NotificationContent.withTemplate("new_level_achieved")
... // set up your notification
notificationContent.templatePlaceholders = ["USER_LEVEL": "7"] // add replacement for your placeholders without brackets

Notifications.send(notificationContent, target: receivers, success: onSuccess, failure: onFailure)
// Your recipients will receive text:
// "Your friend John Doe just reached 7 lvl! Try to beat his score!"
// 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!"

var notificationContent = NotificationContent.CreateWithTemplate("new_level_achieved");
... // set up your notification
notificationContent.AddTemplatePlaceholder("USER_LEVEL", "7"); // add replacement for your placeholders without brackets

Notifications.Send(notificationContent, target, OnSuccess, OnError);
// Your recipients will receive text:
// "Your friend John Doe just reached 7 lvl! Try to beat his score!"
// 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!'

const notification = NotificationContent.withTemplate('new_level_achieved');
... // set up your notification
notification.templatePlaceholders['USER_LEVEL'] = '7' // add replacement for your placeholders without brackets

const target = ..// SendNotificationTarget instance

Notifications.send(notification, target).then();
// Your recipients will receive text:
// "Your friend John Doe just reached 7 lvl! Try to beat his score!"
// 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!'

var notification = NotificationContent();
notification.templateName = 'new_level_achieved';
... // set up your notification
notification.templatePlaceholders['USER_LEVEL'] = '7'; // add replacement for your placeholders without brackets

var target = ..// SendNotificationTarget instance

Notifications.send(notification, target)
    .then((result) => {});
// Your recipients will receive text:
// "Your friend John Doe just reached 7 lvl! Try to beat his score!"
// 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!'

const notification = GetSocialSDK.NotificationContent.withTemplate('new_level_achieved');
... // set up your notification
notification.templatePlaceholders['USER_LEVEL'] = '7' // add replacement for your placeholders without brackets

const target = ..// GetSocialSDK.SendNotificationTarget instance

GetSocialSDK.Notifications.send(notification, target);
// 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 NotificationContentPlaceholders class.

Notification Sounds

Sound customization is supported via Templates only. But first you have to add sound file to your application.

  1. Prepare your audio file. It should not contain any special symbols or spaces in its name.
  2. Add your file to raw resources of your application by putting it into your application’s res/raw directory.
  3. After file is added you should create a template on the Dashboard and specify this file name without extension in Android Sound configuration.
  4. Build the application and run it on a device.
  5. 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.
  1. Prepare your audio file. It should be in aiff, wav, or caf formats and not longer than 30 seconds. You can read more about this on the official Apple documentation in Preparing Custom Alert Sounds section.
  2. Add your file to your application target in the Xcode. Make sure it’s added by checking if the file is listed in Copy Bundle Resources section of your target.
  3. After file is added you should create a template on the Dashboard and specify this file name with extension in iOS Sound configuration.
  4. Build the application and run it on a device.
  5. 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

Badges are displayed only on iOS, on Android devices it will be ignored.

Make sure you’ve completed setup guide for badges on iOS.

Example - send notification and increase badge by 3:

val notificationContent = NotificationContent.notificationWithText("You have 3 new messages!")
                // increase badge by 3
                .withBadge(NotificationContent.Badge.increaseBy(3))
                // set badge to 5
                .withBadge(NotificationContent.Badge.setTo(5))
Notifications.send(notificationContent, target, {
    Log.d("Notifications", "Successfully sent notifications")
}, { error: GetSocialError ->
    Log.d("Notifications", "Failed to send notifications: $error")
})
let notificationContent = GetSocialNotificationContent.withText("You have 3 new messages!")
// increase badge by 3
notificationContent.setBadge(GetSocialNotificationBadge.increaseBy(3))
// set badge to 5
notificationContent.setBadge(GetSocialNotificationBadge.set(5))
Notifications.send(notificationContent, target: receivers, success: onSuccess, failure: onFailure)
var notificationContent = NotificationContent.CreateWithText("You have 3 new messages!");
// increase badge by 3
notificationContent.WithBadge(Badge.IncreaseBy(3));
// set badge to 5
notificationContent.WithBadge(Badge.SetTo(5));

Notifications.Send(notificationContent, target, OnSuccess, OnFailure);
const content = NotificationContent.withText('You have 3 new messages');
// increase badge by 3
content.badge = NotificationBadge.increase(3);
// set badge to 5
content.badge = NotificationBadge.set(5);

Notifications.send(content, target);
var content = NotificationContent();
// increase badge by 3
content.badge = NotificationBadge.increaseBy(3);
// set badge to 5
content.badge = NotificationBadge.setValue(5);

Notifications.send(content, target);
const content = GetSocialSDK.NotificationContent.withText('You have 3 new messages');
// increase badge by 3
content.badge = GetSocialSDK.NotificationBadge.increase(3);
// set badge to 5
content.badge = GetSocialSDK.NotificationBadge.set(5);

Notifications.send(content, target);

Badge is cleared automatically when application is opened. If you want to clear a badge along with push notification, use set() with value 0.

Next Steps

Give us your feedback! Was this article helpful?

😀 🙁