Skip to main content

Notifications Android

Configure Push notifications.

Note

All the public APIs in the SDK should be called after initializing the SDK via Helpshift.Install() API

Configure push notifications via Helpshift

urbanAirshipNotif.png

Helpshift enables you to send notifications to your users. This is particularly useful when you have multiple users on multiple platforms like iOS and Android. Notifications are useful to tell your users when you reply to an issue that they submitted. When the app is backgrounded, the notification that is sent from Helpshift appears as a notification.

To know more about the FCM Push, refer:- Firebase Cloud Messaging

Prerequisites

Implement FCM push in your app.

For FCM, refer to the Firebase Cloud Messaging documentation.

Configure Helpshift Agent Dashboard

To enable the Helpshift system to send push notifications to your users you will have to add an Android as a platform in your app (if you have not added already).

Click on configure button and then click on the push notifications option.

Enter your FCM key credentials per app, via the Settings page > App listing in the left navigation > Scroll down to Push Notifications settings section for the app.

For FCM users, the Auth key file can be found at your Firebase API Console. For more information, click here.

Configure the Helpshift Xamarin SDK to handle notifications

Push notifications

Configure FCM push notification plugins

Before you begin

  1. Integrate FCM Xamarin Android as documented here: Firebase Cloud Messaging Guide

If you have already integrated push notifications, Use the Helpshift.RegisterPushToken(string deviceToken) API to register the device token with the Helpshift SDK. Helpshift can now start sending push notifications to your app.

Once this is done, you can use the Helpshift.HandlePush(object pushNotificationData, bool isAppLaunch) API to send the payload received in the notification.

Note that pushNotificationData needs to be of IDictionary<string, string> type and iAppLaunch paramater is ignored for Android.

To check whether this notification is being sent from the Helpshift's push notification service, please check the origin field of the notification. If it is "helpshift, the notification is a Helpshift notification.


// Registering push token with Helpshift SDK
[Service(Exported = true)]
[IntentFilter(new[] {"com.google.firebase.INSTANCE_ID_EVENT"})]
public class MyFirebaseIIDService : FirebaseInstanceIdService
{
const string TAG = "MyFirebaseIIDService";

public override void OnTokenRefresh()
{
var refreshedToken = FirebaseInstanceId.Instance.Token;
Log.Debug(TAG, "Refreshed token: " + refreshedToken);
Helpshift.RegisterPushToken(refreshedToken);
}
}

// Delegate push handling to Helpshift SDK if the notification is from Helpshift.
[Service(Exported = true)]
[IntentFilter(new[] {"com.google.firebase.MESSAGING_EVENT"})]
public class MyFirebaseMessagingService : FirebaseMessagingService
{
const string TAG = "MyFirebaseMsgService";

public override void OnMessageReceived(RemoteMessage message)
{
IDictionary<string, string> data = message.Data;
string origin = data.ContainsKey("origin") ? data["origin"] : null;
if (origin != null && origin.Equals("helpshift"))
{
// Let Helpshift SDK handle this notification.
Helpshift.HandlePush(data, false);
return;
} else {
// Handle other notifications.
}
}
}

In-app notifications

In-app notifications are similar to notifications in the notification drawer . Unlike push notifications, they appear only when your app is running.

These notifications are sent when an agent replies to a customer's issue. Your customers can go straight into the conversation screen when they tap on the notification.

Note

If the FCM device token is registered for push notifications, then in-app notifications will be disabled. In-app notifications are disabled to avoid duplicate notifications from both push notifications and in-app notifications.

Showing notification count when replies are sent to the user

To fetch unread messages count from the server you can call Helpshift.RequestUnreadMessageCount(shouldFetchFromServer) API. This API will return unread messages count via delegate. Based on the value of shouldFetchFromServer, the locally stored count will be returned if shouldFetchFromServer is false else from the server by fetching remotely when shouldFetchFromServer is true. An example use of this count is to update the badge count to indicate unread messsages. Please note that before calling this method, you need to set the listener for Helpshift events by calling the Helpshift.SetHelpshiftEventsListener(eventsListener) API.


namespace HelpshiftXamarinTestApp.Droid
{
[Application]
public class MyApplication : Application
{
public const string CHANNEL_ID = "HelpshiftXamarinDemo";

public MyApplication(IntPtr handle, JniHandleOwnership transfer) : base(handle, transfer)
{
// do any initialisation you want here (for example initialising properties)
}

override public void OnCreate()
{
base.OnCreate();

Dictionary<String, Object> installConfig = new Dictionary<String, Object>();
installConfig.Add("enableLogging", true);
installConfig.Add("screenOrientation", ScreenOrientation.Portrait);
installConfig.Add("notificationIcon", Resource.Drawable.app_icon);
Helpshift.Install("gayatri_platform_20181018063833353-6d863ba814cb367",
"gayatri.helpshift.com",
installConfig);

Helpshift.SetHelpshiftEventsListener(new ExampleHelpshiftEventListener());
}

// Requesting unread count from Helpshift
public void RequestUnreadCount()
{
// When you call this API, you receive the unread count in your event listener which implements `IHelpshiftEventsListener`.
// In this example we have set ExampleHelpshiftEventListener as the listener.
Helpshift.RequestUnreadMessageCount(true);
}
}
}

public class ExampleHelpshiftEventListener : IHelpshiftEventsListener
{
public void OnEventOccurred(string eventName, IDictionary<string, object> data)
{
Console.WriteLine("Received event: " + eventName);

if(eventName.Equals(HelpshiftEvent.RECEIVED_UNREAD_MESSAGE_COUNT))
{
Debug.Log("Unread count: " + eventData[HelpshiftEvent.DATA_MESSAGE_COUNT]);
Debug.Log("Is Unread count from cache: " + eventData[HelpshiftEvent.DATA_MESSAGE_COUNT_FROM_CACHE]);

// Do something with the unread count here, for example, update the badge count.
}
}

public void OnUserAuthenticationFailure(HelpshiftUserAuthFailureReason authFailureReason)
{
Console.WriteLine("Helpshift User auth failed: " + authFailureReason);
}
}

  • The notification count is fetched via this API from the SDK cache and Helpshift's servers (indicated by the value of fromCache in the example above). However, the count from Helpshift’s servers is rate limited and it returns the value only if a subsequent call is made to the API after the reset timeout or when the user just closes the chat screen (whichever is earlier). For an active issue, the reset timeout is 1 minute and 5 minutes for inactive issues.