Hansel push

Whenever you make a change live though Hansel dashboard, the app is notified about this change using a Push Notification. This notifies the app that it should pull the changes from the server whenever the app starts or comes to foreground. 

Project setup

Hansel supports both Firebase Cloud Messaging as well as the legacy Google Cloud Messaging.  Please skip to the appropriate sections FCM / GCM based on your existing setup. 

If you don't use any of the push services please configure Firebase Cloud Messaging.

 

Using Firebase Cloud Messaging (FCM)

(A) Push token registration with Hansel SDK

For Hansel to be able to send a push notification to a device, the Hansel SDK needs to have the push token. The push token should be registered with Hansel SDK as soon as the application gets a call back for push token in the method onTokenRefresh() of the class which extends from FirebaseInstanceIdService

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
   ...
   public void onTokenRefresh() {
        // Get updated InstanceID token.
        String refreshedToken = FirebaseInstanceId.getInstance().getToken()
        ....
        
        // register the push token with Hansel
        Hansel.setNewToken(this, token);
    }
    ...
}

(B) Handing over push message to Hansel SDK

As you receive the push in your FCM service, pass the payload to Hansel SDK using the following snippet.

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    ...
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getData().size() > 0) { if(Hansel.isPushFromHansel(remoteMessage.getData())){ Hansel.handlePushPayload(this, remoteMessage.getData()); } else { /** * Handle other notifications */ } } .... } }

 

Using Google Cloud Messaging (GCM)

(A) Push token registration with Hansel SDK

For Hansel to be able to send a push notification to a device, the Hansel SDK needs to have the push token. The push token should be registered with Hansel SDK as soon as the application gets a call back for push token in the configured IntentService

 

public class RegistrationIntentService extends IntentService {
   ...
   public void onHandleIntent(Intent intent) {
        // Get updated InstanceID token.
       InstanceID instanceID = InstanceID.getInstance(this);
       String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
                    GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
        ....
        
        // register the push token with Hansel
        Hansel.setNewToken(this, token);
    }
    ...
}

(B) Handing over push message to Hansel SDK

As you receive the push in your GCM service, pass the payload to Hansel SDK using the following snippet.

public class MyGCMListenerService extends GcmListenerService {
    ...
    @Override
    public void onMessageReceived(String from, Bundle data) {
        
if( null == data)return; if(Hansel.isPushFromHansel(data)){ Hansel.handlePushPayload(this, data); } else { /** * Handle other notifications */ } .... } }

Dashboard setup

Copy the Sever Key from the GCM/FCM console and add it to the settings section of your app on Hansel Dashboard (Not sure where to find the Server Key refer to Getting FCM/GCM Server Key ).

 

 

Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.