Skip to content

Commit

Permalink
Move MParticle-specific messaging logic into a router for reusability
Browse files Browse the repository at this point in the history
  • Loading branch information
dallasgutauckis authored and Sam Dozor committed Jun 1, 2016
1 parent 1fa2c86 commit d8762a2
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.mparticle.messaging;

import android.content.Context;
import android.content.Intent;

import com.mparticle.MPService;

public class MPMessagingRouter {
/**
* Parses the incoming intent and delegates functionality to the given {@code callback} if appropriate. This implementation checks for
* MParticle-specific actions and will not handle messages outside of that scope. MParticle actions can be found in
* {@link MPMessagingAPI} {@code BROADCAST_*} constants
*
* @param context
* @param intent
* @param callback
* @return {@code true} if the {@link Intent} was handled by MParticle
*/
public static boolean onReceive(Context context, Intent intent, PushAnalyticsReceiverCallback callback) {
if (MPMessagingAPI.BROADCAST_NOTIFICATION_TAPPED.equalsIgnoreCase(intent.getAction())) {
AbstractCloudMessage message = intent.getParcelableExtra(MPMessagingAPI.CLOUD_MESSAGE_EXTRA);
CloudAction action = intent.getParcelableExtra(MPMessagingAPI.CLOUD_ACTION_EXTRA);
if (!callback.onNotificationTapped(message, action)) {
intent.putExtra(MPMessagingAPI.CLOUD_MESSAGE_EXTRA, message);
intent.putExtra(MPMessagingAPI.CLOUD_ACTION_EXTRA, action);
MPService.runIntentInService(context, intent);
}
return true;
} else if (MPMessagingAPI.BROADCAST_NOTIFICATION_RECEIVED.equalsIgnoreCase(intent.getAction())) {
AbstractCloudMessage message = intent.getParcelableExtra(MPMessagingAPI.CLOUD_MESSAGE_EXTRA);
if (!callback.onNotificationReceived(message)) {
intent.putExtra(MPMessagingAPI.CLOUD_MESSAGE_EXTRA, message);
MPService.runIntentInService(context, intent);
}
return true;
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import android.content.Context;
import android.content.Intent;

import com.mparticle.MPService;

/**
* <code>BroadcastReceiver</code> to be used to listen for, manipulate, and react to GCM notifications.
*
Expand All @@ -26,63 +24,20 @@
* @see #onNotificationTapped(AbstractCloudMessage, CloudAction)
*
*/
public class PushAnalyticsReceiver extends BroadcastReceiver {
private Context mContext = null;

public class PushAnalyticsReceiver extends BroadcastReceiver implements PushAnalyticsReceiverCallback {

@Override
public final void onReceive(Context context, Intent intent) {
mContext = context;
if (MPMessagingAPI.BROADCAST_NOTIFICATION_TAPPED.equalsIgnoreCase(intent.getAction())){
AbstractCloudMessage message = intent.getParcelableExtra(MPMessagingAPI.CLOUD_MESSAGE_EXTRA);
CloudAction action = intent.getParcelableExtra(MPMessagingAPI.CLOUD_ACTION_EXTRA);
if (!onNotificationTapped(message, action)){
intent.putExtra(MPMessagingAPI.CLOUD_MESSAGE_EXTRA, message);
intent.putExtra(MPMessagingAPI.CLOUD_ACTION_EXTRA, action);
MPService.runIntentInService(context, intent);
}
return;
} else if (MPMessagingAPI.BROADCAST_NOTIFICATION_RECEIVED.equalsIgnoreCase(intent.getAction())){
AbstractCloudMessage message = intent.getParcelableExtra(MPMessagingAPI.CLOUD_MESSAGE_EXTRA);
if (!onNotificationReceived(message)){
intent.putExtra(MPMessagingAPI.CLOUD_MESSAGE_EXTRA, message);
MPService.runIntentInService(context, intent);
}
return;
}

}

/**
* Helper method to retrieve the Context that was passed into this <code>BroadcastReceiver</code>
*
* @return Context object
*/
protected final Context getContext(){
return mContext;
MPMessagingRouter.onReceive(context, intent, this);
}

/**
* Override this method to listen for when a notification has been received.
*
*
* @param message The message that was received. Depending on the push provider, could be either a {@link com.mparticle.messaging.MPCloudNotificationMessage} or a {@link com.mparticle.messaging.ProviderCloudMessage}
* @return True if you would like to handle this notification, False if you would like the mParticle to generate and show a {@link android.app.Notification}.
*/
protected boolean onNotificationReceived(AbstractCloudMessage message){
@Override
public boolean onNotificationReceived(AbstractCloudMessage message) {
return false;
}

/**
* Override this method to listen for when a notification has been tapped or acted on.
*
*
* @param message The message that was tapped. Depending on the push provider, could be either a {@link com.mparticle.messaging.MPCloudNotificationMessage} or a {@link com.mparticle.messaging.ProviderCloudMessage}
* @param action The action that the user acted on.
* @return True if you would like to consume this tap/action, False if the mParticle SDK should attempt to handle it.
*/
protected boolean onNotificationTapped(AbstractCloudMessage message, CloudAction action){
@Override
public boolean onNotificationTapped(AbstractCloudMessage message, CloudAction action) {
return false;
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.mparticle.messaging;

public interface PushAnalyticsReceiverCallback {
/**
* Override this method to listen for when a notification has been received.
*
*
* @param message The message that was received. Depending on the push provider, could be either a {@link com.mparticle.messaging.MPCloudNotificationMessage} or a {@link com.mparticle.messaging.ProviderCloudMessage}
* @return True if you would like to handle this notification, False if you would like the mParticle to generate and show a {@link android.app.Notification}.
*/
boolean onNotificationReceived(AbstractCloudMessage message);

/**
* Override this method to listen for when a notification has been tapped or acted on.
*
*
* @param message The message that was tapped. Depending on the push provider, could be either a {@link com.mparticle.messaging.MPCloudNotificationMessage} or a {@link com.mparticle.messaging.ProviderCloudMessage}
* @param action The action that the user acted on.
* @return True if you would like to consume this tap/action, False if the mParticle SDK should attempt to handle it.
*/
boolean onNotificationTapped(AbstractCloudMessage message, CloudAction action);
}

0 comments on commit d8762a2

Please sign in to comment.