Creating a Push Message

Besides sending in-app messages, Magnet Message also provides a simplified API to create a push message and deliver it to a user using the platform push technology. Developers may include a subset of pre-defined elements used by Apple Push Notification Service and Android Notification in the payload: title, body, sound, badge, and icon.

import com.magnet.mmx.client.api.MMXPushMessage; import com.magnet.mmx.client.api.MMXPushMessage.OnFinishedListener; import com.magnet.mmx.protocol.PushResult; ... User.search("userName:john.doe", 1, 0, null, new ApiCallback<List<User>>() { public void failure(ApiError apiError) { Log.e(TAG, "User search failed: " + apiError, apiError.getCause()); } public void success(List<User> users) { User user = users.get(0); Map<String, Object> payload = new HashMap<String, Object>(); payload.put("title", "some-title"); payload.put(("body", "some-content-text"); payload.put("badge", 2); payload.put("key1", "some-string-value"); payload.put("key2", 3.14159); MMXPushMessage pushMessage = new MMXPushMessage.Builder() .type("MyPushMessage") .recipient(user) .content(payload) .build(); pushMessage.send(new OnFinishedListener<PushResult> listener() { public void onSuccess(PushResult result) { ... } public void onFailure(FailureCode code, Throwable exception) { ... } }); } });

Receiving a Push Message

The GCM set up and implementation to receive push messages are the same as handling Wakeup users.

  1. Implement and declare a BroadcastReceiver to handle push message. It is up to you to decide how you should handle the push message. Here the receiver processes the payload.
    import java.util.Map; import com.magnet.mmx.client.api.MMXPushEvent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class MyBroadcastReceiver extends BroadcastReceiver { ... public void onReceive(Context context, Intent intent) { MMXPushEvent event = MMXPushEvent.fromIntent(intent); if (event == null) { // Received a non-MMX GCM; developer should handle the intent directly } else if ("MyPushMessage".equals(event.getType())) { // Push message from another client; get the custom payload as a Map Map<String, Object> payload = event.getCustomMap(); ... } else { ... } } } ...
  2. Declare it in the AndroidManifest.xml file
    <application ...> ... <receiver android:name=".MyBroadcastReceiver" android:exported="false"> <intent-filter> <action android:name="MY_WAKEUP_ACTION" /> </intent-filter> </receiver> ... </application>
  3. Register your implementation with MMX
    // register a wakeup listener for GCM MMX.registerWakeupBroadcast(context, new Intent("MY_WAKEUP_ACTION"));