-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move MParticle-specific messaging logic into a router for reusability
- Loading branch information
1 parent
1fa2c86
commit d8762a2
Showing
3 changed files
with
69 additions
and
52 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
android-core/src/main/java/com/mparticle/messaging/MPMessagingRouter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
android-core/src/main/java/com/mparticle/messaging/PushAnalyticsReceiverCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |