GetSocial Notifications on Android

Prerequisite

Setup Android Push Notifications

GetSocial supports sending push notifications via Firebase Cloud Messaging (FCM) or Google Cloud Messaging (GCM).

  1. Follow FCM official guide (or GCM official guide) to setup push notifications in your app.

    CDM Permissions for GCM

    If you’re using Google Cloud Messaging for Push Notifications, don’t forget to add CDM permissions to your AndroidManifest.xml:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    <application ...>
        ...
        <permission
            android:name="${applicationId}.permission.C2D_MESSAGE"
            android:protectionLevel="0x2" />
        <uses-permission
            android:name="android.permission.WAKE_LOCK" />
        <uses-permission
            android:name="com.google.android.c2dm.permission.RECEIVE" />
        <uses-permission
            android:name="${applicationId}.permission.C2D_MESSAGE" />
    </application>
    
  2. Login to the GetSocial Dashboard.

  3. Go to the “App settings” section → “Notifications” tab.
  4. 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.

    GetSocial Dashboard - Enable Push Notifications

  5. Select what kind of notifications users will receive:

    GetSocial Dashboard - Notifications Type

  6. Press “Save” button in the bottom right corner.

Enable/disable GetSocial Notifications on the Client Side

By default GetSocial SDK automatically register at push server and user will start receiving push notifications.

To prevent auto registration and register for push notifications manually:

  1. Add autoRegisterForPush property to the GetSocial configuration in the Android application build.gradle:

    1
    2
    3
    4
    getsocial {
        ...
        autoRegisterForPush false
    }
    
  2. To start receiving GetSocial push notifications call:

    1
    GetSocial.registerForPushNotification();
    

Handle Click on Push Notifications

Select Activity to Open

By default, click on the GetSocial 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:

1
2
3
4
5
6
7
<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 respective view on click on the GetSocial 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 custom title, UiActionListener, action button handler etc. In this case we recommend you to override default behavior and open GetSocial View by yourself.

Notification Action Listener

You can customize the default behaviour and handle clicks on GetSocial notifications on your own. To do so, set the NotificationActionListener in onCreate method of your Application or Activity:

1
2
3
4
5
6
7
8
GetSocial.setNotificationActionListener(new NotificationActionListener() {

    public boolean onActionReceived(NotificationAction action) {
        boolean isHandled = handleNotificationAction(action);
        return isHandled;
    }

});

In the example above return a boolean: true, if notification is handled by your method handleNotificationAction(action), or false if it is not. If you will return false, SDK will handle the notification action.

For example, if you want to prevent of showing GetSocial Activity Feed UI on notification click:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
boolean handleNotificationAction(NotificationAction action) {
    switch (action.getAction()) {
        case OPEN_ACTIVITY:
            // add custom logic here
            Log.i("GetSocial", "Activity Notification received");
            return true;

        default:
            return false;
    }
}

Tip

If you are not using GetSocial UI, we recommend to handle notifications by yourself - better user experience would be to react to notification actions.

Customize Notification Icon

By default GetSocial SDK will use application icon as notification icon.

There are two ways to customize the icon. First option is to put image with name getsocial_notification_icon into any res/drawable folder. Second option is to set icon resource name in Android Manifest meta data:

1
2
3
4
5
6
7
<application ... >
    ...
    <meta-data
        android:name="im.getsocial.sdk.NotificationIcon"
        android:resource="@drawable/ic_notification_custom" />
    ...
</application>

As a result all notifications coming from GetSocial SDK will use custom icon:

Customized Notification Icon

Give us your feedback! Was this article helpful?

😀 🙁