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 the app, MMXChannel, and MMXMessage. For example, pushes can contain more detailed 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 APNS.
  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 custom a 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 Template

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 Template

To create the 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.

  1. Create a new project in XCode and add this code, for creating a user, to the default view controller class.

    func createUser() { let dispatch_group = dispatch_group_create() let user = MMUser() user.firstName = "John" user.lastName = "Doe" user.userName = user.firstName user.password = user.firstName dispatch_group_enter(dispatch_group) user.register({ u in dispatch_group_leave(dispatch_group) }, failure: { error in if error.code == 409 { dispatch_group_leave(dispatch_group) } }) dispatch_group_notify(dispatch_group, dispatch_get_main_queue(), { let credential = NSURLCredential(user: user.userName, password: user.password, persistence: .None) MMUser.login(credential, success: { MMX.start() //call method that sends messages self.sendMessage() }, failure: { error in print("error \(error.localizedDescription)") }) }) }

  2. Now create a new method to send messages.

    func sendMessage() { //create channels and send messages here }

  3. In the sendMessage method add the following code to create one channel with the default push config and one channel with a custom push config.

    //Channels to be populated var channel1: MMXChannel! var channel2: MMXChannel! //create dispatch_group for loading channel objects let dispatch_group = dispatch_group_create() //Let’s first get the channel that will use the app level template*(by default)*. //Unspecified push config, will use the default Push template dispatch_group_enter(dispatch_group) MMXChannel.channelForName("MyChannel", isPublic: true, success: { channel in if channel.isSubscribed { //add channel and leave group channel1 = channel dispatch_group_leave(dispatch_group) } else { channel.subscribeWithSuccess({ channel1 = channel dispatch_group_leave(dispatch_group) }, failure: nil) } }, failure: { error in MMXChannel.createWithName("MyChannel", summary: "This is a test channel", isPublic: true, publishPermissions: .Anyone, success: { (channel) in //add channel and leave group channel1 = channel dispatch_group_leave(dispatch_group) }) { (error) in print("error \(error.localizedDescription)") } }) //Let’s get the second channel that uses the custom template. //Specifies custom Push template dispatch_group_enter(dispatch_group) MMXChannel.channelForName("MyChannel2", isPublic: true, success: { channel in //add channel and leave group if channel.isSubscribed { //add channel and leave group channel2 = channel dispatch_group_leave(dispatch_group) } else { channel.subscribeWithSuccess({ channel2 = channel dispatch_group_leave(dispatch_group) }, failure: nil) } }, failure: { error in MMXChannel.createWithName("MyChannel2", summary: "This is my second test channel", isPublic: true, publishPermissions: .Anyone, subscribers: nil, pushConfigName: "MyChannelTemplate", success: { (channel) in //add channel and leave group channel2 = channel dispatch_group_leave(dispatch_group) }) { (error) in print("error \(error.localizedDescription)") } })

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

    //wait for channel objects to be populated dispatch_group_notify(dispatch_group, dispatch_get_main_queue(), { let content = ["content_key":"Hello World"] let message1 = MMXMessage(toChannel: channel1, messageContent: content) let message2 = MMXMessage(toChannel: channel2, messageContent: content) message1.sendWithSuccess({ invaildUsers in print("message 1 success") }, failure: { error in print("error \(error.localizedDescription)") }) message2.sendWithSuccess({ invaildUsers in print("message 2 success") }, failure: { error in print("error \(error.localizedDescription)") }) })

  5. There is one last thing to do. In the viewDidLoad method call our createUser method.

    override func viewDidLoad() { super.viewDidLoad() createUser() }

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

  1. Build and run the app on a physical device. (After launch, press the Home button to background your app, and then bring up the device's lock screen.)

  2. Update the createUser method and enter a new first name (e.g., user.firstName = "Jane").

  3. Build and run the app on the simulator.

  4. You should see two push notifications like the ones below on the physical device.

Using Push on an MMXMessage

Using push templates on an MMXMessage is similar to using one on a channel. Go back to Magnet Studio and 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 code 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. Update the sendMessage method we created earlier. Replace these lines of code:

    let message1 = MMXMessage(toChannel: channel1, messageContent: content) let message2 = MMXMessage(toChannel: channel2, messageContent: content)

    With these lines of code:

    let message1 = MMXMessage(toChannel: channel1, messageContent: content) let message2 = MMXMessage(toChannel: channel2, messageContent: content, pushConfigName: "MyMessageTemplate")

  3. Build and run the app again on the simulator.

  4. You should see two push notifications like the ones below, on the physical device.

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})