Android - Push Message

To send users push messages Visilabs servers need to receive tokens. To achieve this customEvent method need to be called passing OM.sys.AppID and OM.sys.TokenID parameters. To learn the value of OM.sys.AppID parameter send a request to RMC support. The value of token which is generated by you application will be the value of OM.sys.TokenID key.

Generated tokens can be retrieved by a service class which is a subclass of InstanceIDListenerService as below:

package com.visilabs.visilabsandroiddemo;


import android.util.Log;
import com.google.android.gms.gcm.GoogleCloudMessaging;
import com.google.android.gms.iid.InstanceID;
import com.google.android.gms.iid.InstanceIDListenerService;
import java.util.HashMap;


public class ListenerService extends InstanceIDListenerService {
   @Override
   public void onTokenRefresh() {
       String token = null;
       try{
          InstanceID instanceID = InstanceID.getInstance(getApplicationContext());
          token = instanceID.getToken(getString(R.string.gcm_defaultSenderId), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);}
       catch (Exception e){
           Log.e("Tag", e.getMessage());
       }
       HashMap<String, String> parameters= new HashMap<String, String>();
       parameters.put("OM.sys.AppID","AppID");
       parameters.put("OM.sys.TokenID",token);
       Visilabs.CallAPI().customEvent("RegisterToken", parameters);
   }

   @Override
   public void onCreate() {
       String token = null;
       try{
          InstanceID instanceID = InstanceID.getInstance(getApplicationContext());
          token = instanceID.getToken(getString(R.string.gcm_defaultSenderId), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);}
       catch (Exception e){
           Log.e("Tag", e.getMessage());
       }
       HashMap<String, String> parameters= new HashMap<String, String>();
       parameters.put("OM.sys.AppID","AppID");
       parameters.put("OM.sys.TokenID",token);
       Visilabs.CallAPI().customEvent("RegisterToken", parameters);
   }
}

 

This service should be declared in AndroidManifest.xml file: 

 

<service android:name="com.visilabs.visilabsandroiddemo.ListenerService"
   android:exported="false">
   <intent-filter>
       <action android:name="com.google.android.gms.iid.InstanceID" />
   </intent-filter>
</service>

 

To show push messages you should create a subclass of GCMListenerService and handle incoming data in this class. This service should be declared in AndroidManifest.xml file: 


package com.visilabs.visilabsandroiddemo;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.google.android.gms.gcm.GcmListenerService;


public class VisilabsGcmListenerService extends GcmListenerService  {
   @Override
   public void onMessageReceived(String from, Bundle data) {
       String message = data.getString("message");
       String url = data.getString("url");
       sendNotification(message, url);
   }

   private void sendNotification(String message,String link) {
       Intent intent = new Intent(this, MainActivity.class);
       intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
       PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
               PendingIntent.FLAG_ONE_SHOT);
    	Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_stat_ic_notification).setContentTitle("GCM Message").setContentText(message).setAutoCancel(true).setSound(defaultSoundUri).setContentIntent(pendingIntent);
        NotificationManager notificationManager =
               (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, notificationBuilder.build());
   }
}


<service android:name="com.visilabs.visilabsandroiddemo.VisilabsGcmListenerService"
   android:exported="false" >
   <intent-filter>
       <action android:name="com.google.android.c2dm.intent.RECEIVE" />
   </intent-filter>
</service>



Parent Topic: Android - MobileTagging











Copyright 2020 Related Digital