Invoking a Messaging Service

A developer should register with Google Cloud Messaging (GCM) for Android services to get the deviceToken that is used to register with Messaging. Also a developer should fetch the deviceId and userfriendlyId to create an instance of messaging service.

The following are the methods you can use for a messaging service.

Register

Import the following Libraries:

  • com.google.android.gms.common.ConnectionResult
  • com.google.android.gms.common.GooglePlayServicesUtil
  • com.google.android.gms.gcm.GoogleCloudMessaging
  • com.kony.sdk.callback.MessagingCallback
  • com.kony.sdk.common.MessagingServiceException
  • com.kony.sdk.services.messaging.MessagingService
  • com.kony.sdk.client.KonyFactory
  • com.kony.sdk.common.KonyException

Sample Code

 // Sample code to register to messaging 

Context context = getApplicationContext();
String SENDER_ID = < your - project - number - from - google > ;
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
String gcmRegistrationId = gcm.register(SENDER_ID);
String deviceId = Secure.getString(context.getContentResolver(), Secure.ANDROID_ID);
String ufid = < your - ufid > ;

try {
    messagingClient = new KonyClient().getMessagingService();
} catch (KonyException exception) {
    Log.d("Exception", exception.getMessage());
}
messagingClient.registerInBackground(gcmRegistrationID, UFID, deviceID, new MessagingCallback() {
    @Override
    public void onSuccess(boolean result) {
        Log.d("Registration", "success");
    }

    @Override
    public void onFailure(MessagingServiceException exception) {
        Log.d("Registration Failure", exception.getErrorMessage());
    }
});

Unregister

Import the following Libraries:

  • com.kony.sdk.callback.MessagingCallback
  • com.kony.sdk.common.MessagingServiceException
  • com.kony.sdk.services.messaging.MessagingService

Sample Code

 // Sample code to unregister from messaging service
messagingClient.unregisterInBackground(new MessagingCallback() {
    @Override
    public void onSuccess(boolean result) {
        Log.d("Un Registration", "Success");
    }

    @Override
    public void onFailure(MessagingServiceException exception) {
        Log.d("Un Registration Failure" + exception.getErrorMessage());
    }
});

Update GeoLocation

Import the following Libraries:

  • import com.kony.sdk.callback.MessagingCallback
  • import com.kony.sdk.common.MessagingServiceException
  • import com.kony.sdk.services.messaging.MessagingService

Sample Code

 // Sample code to update the geolocation
Double latitude = < latitude - value > ;
Double longitude = < longitude - value > ;
String locationName = < location - name > ;
messagingClient.updateGeoLocationInBackground(latitude, longitude, locationName,
    new MessagingCallback() {
        @Override
        public void onSuccess(boolean result) {
            Log.d("Location Update", "Successful");
        });
}

@Override
public void onFailure(MessagingServiceException exception) {
    Log.d("Location Update Faliure", exception.getErrorCode());
});
}
});

Fetch All Messages

Import the following Libraries:

  • com.kony.sdk.callback.MessageContentCallback
  • com.kony.sdk.common.MessagingServiceException
  • com.kony.sdk.services.messaging.MessagingService

Sample Code

 // Sample code to get all messagesint startIndex = 0;
int pageSize = 1000;
messagingClient.fetchAllMessagesInBackground(startIndex, pageSize, new MessageContentCallback() {
    @Override
    public void onSuccess(JSONObject response) {
        Log.d("Fetched Messages", response.toString());
    }

    @Override
    public void onFailure(MessagingServiceException exception) {
        Log.d("Failed to fetch Messages", exception.getMessage());
    }
});

NOTE: The Fetch all messages method returns only part of a message. To get full content of the message, use the Get Message Content method.

Mark Message as Read

Import the following Libraries:

  • com.kony.sdk.callback.MessageContentCallback
  • com.kony.sdk.common.MessagingServiceException
  • com.kony.sdk.services.messaging.MessagingService

Sample Code

 // Sample code to mark messages as read
String messageid = < fetch - id - of - the - message > ;
messagingClient.markMessageReadInBackground(messageid, new MessageContentCallback() {
    @Override
    public void onSuccess(boolean result) {
        Log.d("Message", "marked");
    }

    @Override
    public void onFailure(MessagingServiceException exception) {      
        Log.d("Failed to mark message as read", exception.getErrorMessage());
    }
});

Get Message Content

Import the following Libraries:

  • com.kony.sdk.callback.MessageContentCallback
  • com.kony.sdk.common.MessagingServiceException
  • com.kony.sdk.services.messaging.MessagingService

Sample Code

 // Sample code to fetch message content from  Messaging 
String messageid = < fetch - id - of - the - message > ;
messagingClient.fetchMessageContentInBackground(messageid, new MessageContentCallback() {
    @Override
    public void onSuccess(JSONObject response) {
        Log.d("Message Content", response.toString());
    }

    @Override
    public void onFailure(MessagingServiceException exception) {     
        Log.d("Failed to fetch Message Content", exception.getErrorMessage());
    }
});