Push Templates

By default, Magnet Max notifies offline MMXChannel subscribers using push notifications (APNS, GCM). Push templates are FreeMarker templates that allow you to customize push notification content on the basis of an app, MMXChannel, and MMXMessage. For example, pushes can contain more information like: "John just replied to your message" or "Jane just posted a photo". This allows users to get more detailed and useful pushes in a few steps.

  1. First, configure your app to use Magnet Max. See Getting Started.
  2. Then set up your app for push. See Set up GCM.
  3. Next, log on to Magnet Studio.
  4. Once logged on, select the button labeled Push Templates located in the panel on the left.

The default system push templates will be used if no other push templates are specified. Push templates always use the most descriptive template available and will override any global push templates. For example, if an app has a custom push template named "MyAppTemplate" and an MMXChannel has an associated push template named "MyChannelTemplate", the channel template will be used. On the other hand, if the MMXChannel does not have an associated push template, the app level template "MyAppTemplate" will be used. The same rule applies for MMXMessages.

App-Wide Templates

To create an app wide template, select the Create Template button located in the top right corner of the Magnet Studio console and enter "MyAppTemplate" as the name for your template. Use the FreeMarker syntax to customize your push notification. (See Push Template Context Variables for accessible variables.)

mmx.pubsub.notification.type=push mmx.pubsub.notification.title=${application.name} mmx.pubsub.notification.body=New Message mmx.pubsub.notification.sound=default

Channel Templates

To create a channel template, select the Create Template button located in the top right corner and enter "MyChannelTemplate" as the name for your template. In the Applies To section, select channels. Use the FreeMarker syntax to customize your push notification for MMXChannels. (See Push Template Context Variables for accessible variables.)

mmx.pubsub.notification.type=push mmx.pubsub.notification.title=${channel.name} mmx.pubsub.notification.body=${msg.from} is saying something mmx.pubsub.notification.sound=default

Try Out Templates

Now that you have two templates defined, you can try them out by applying them to the MMXChannel.

  1. Create one channel with the default push config and one channel with a custom push config.

    //Let’s first get the channel that will use the app level template*(by default)*. //Unspecified push config, will use the default Push template MMXChannel channelWithAppDefaultPushConfig = null; MMXChannel.create(name, summary, false, MMXChannel.PublishPermission.SUBSCRIBER, users, new MMXChannel.OnFinishedListener<MMXChannel>() { public void onSuccess(MMXChannel result) { channelWithAppDefaultPushConfig = result; } public void onFailure(MMXChannel.FailureCode code, Throwable ex) { } }); //Let’s get the second channel that uses the custom template. //Specifies custom Push template MMXChannel channelWithPushConfig = null; MMXChannel.create(name, summary, false, MMXChannel.PublishPermission.SUBSCRIBER, users, "channelWithPushConfig", new MMXChannel.OnFinishedListener<MMXChannel>() { public void onSuccess(MMXChannel result) { channelWithPushConfig = result; } public void onFailure(MMXChannel.FailureCode code, Throwable ex) { } });

  2. Now that we have two channels created, append the following code to the sendMessage method to send a message to both channels.

    HashMap<String, String> content = new HashMap<String, String>(); content.put("foo", "bar"); // Send message in first channel channelWithAppDefaultPushConfig.publish(new MMXMessage.Builder().content(content).build(), MMXChannel.OnFinishedListener<String>() { public void onSuccess(String s) { } public void onFailure(MMXMessage.FailureCode failureCode, Throwable e) { // do something } }); // Send message in first channel channelWithPushConfig.publish(new MMXMessage.Builder().content(content).build(), MMXChannel.OnFinishedListener<String>() { public void onSuccess(String s) { } public void onFailure(MMXMessage.FailureCode failureCode, Throwable e) { // do something } });

  3. If a channel subscriber is offline, the notification received will be different.

  4. The first channel message will be "New Message".
  5. The second channel message will be "Joe Doe is saying something".

Important: If you have not done so already, configure your app for push notifications. See Set up GCM.

Using Push on an MMXMessage

Using push templates on an MMXMessage is similar to using one on a channel. Go back to Magnet Studio create another push template following the steps above; name the push template "MyMessageTemplate". In the Applies To section select channels. See Push Template Context Variables for accessible variables.

  1. Create custom values for your template or use the one below.

    mmx.pubsub.notification.type=push mmx.pubsub.notification.title=Message From ${msg.from} mmx.pubsub.notification.body=${msg.content.content_key} - ${msg.date?datetime} mmx.pubsub.notification.sound=default

  2. Specify the push config name when constructing the MMXMessage:

    HashMap<String, String> content = new HashMap<String, String>(); content.put("foo", "bar"); MMXMessage message = new MMXMessage.Builder().content(content).channel(channelWithPushConfig).pushConfigName("MyMessageTemplate").build(); message.send(new MMXMessage..OnFinishedListener<String>() { public void onSuccess(String s) { } public void onFailure(MMXMessage.FailureCode failureCode, Throwable e) { // do something } });

  3. If some channel subscriber is offline, the notification received will be "bar - 2016-5-10".

Push Template Context Variables

Object variable Type Description
application
name String Server side application name
channel
name String Channel name
desc String Channel description
msg
from String User first and last name ("FirstName LastName")
date java.util.Date Base on server locale; user locale is not available. Use date/time/datetime formatter as ${msg.date?datetime}
content java.util.Map Dictionary containing MMXMessage messageContent (e.g. ${msg.content.content_key})