Navigation

Push Notifications (FCM)

MongoDB Realm supports integrating mobile (Android and iOS) apps with the Firebase Cloud Messaging (FCM) service to provide push notifications. You configure and send notification messages from within the Realm console, while clients register with Firebase Cloud Messaging (FCM) for messages sent to specific topics.

{
"%%args.notification.title": "Test Notification Please Ignore"
}
{
"%%args.to": "%%values.validTopics"
}
{
"%%true": {
"%function": {
"name": "allUserIdsAreValid",
"arguments": [
"%%args.userIds"
]
}
}
}
Info With Circle IconCreated with Sketch.Note

This template calls an example function named allUserIdsAreValid that does the following:

  1. Accepts the list of User IDs provided in the userIds argument
  2. Queries MongoDB for a user document that matches the current user's id
  3. Compares the provided phone number to the number listed in the user document
  4. Returns the boolean result of the comparison
exports = function(toPhone) {
const mdb = context.services.get('mongodb-atlas');
const users = mdb.db('demo').collection('users');
const user = users.findOne({ _id: context.user.id });
return user.phoneNumber === toPhone;
}

To specify rules for Push Notifications, click on the Rules tab on the Push Notifications page.

Info With Circle IconCreated with Sketch.Note

Unlike other services in MongoDB Realm, rules for push notifications are optional. By default, all push notifications are allowed. However, once you specify a rule, the restrictions imposed by that rule will take effect.

The Push Notifications rules permit the following arguments. You can access them with the "%%args" expansion:

FieldTypeDescription
userIdsArray of strings.The user ids of the message recipients.
toStringThe recipient of the message. The value can be a device's registration token, a device group's notification key, or a single topic (prefixed with /topics/).
registrationTokensArray of strings.The list of registration tokens for the devices receiving the multicast message.
prioritystringThe priority of the notification. Value is either "high" or "normal". Corresponds to the priority option for HTTP JSON messages via FCM. See the FCM HTTP protocol reference.
collapseKeystringThe collapse key associated with collapsible messages. Corresponds to the collapse_key option for HTTP JSON messages via FCM. See the FCM HTTP protocol reference.
contentAvailablebooleanA flag that determines whether to awake idle client apps upon receipt of the message. Corresponds to the content_available option for HTTP JSON messages via FCM. See the FCM HTTP protocol reference.
mutableContentbooleanA flag that determines whether the notification content can be modified before being displayed to the user. Corresponds to the mutable_content option for HTTP JSON messages via FCM. See the FCM HTTP protocol reference.
timeToLiveint

Maximum time (in milliseconds) to retain the message if the device is offline. Valid value range from 0 to 2419200.

Corresponds to the time_to_live option for HTTP JSON messages via FCM. See the FCM HTTP protocol reference.

dataJSON documentPayload for data message. data document consists of custom key-value pairs. Corresponds to the data option for HTTP JSON messages via FCM. See the FCM HTTP protocol reference.
notificationJSON documentPayload for notification. notification document consists of predefined fields. Corresponds to the notification option for HTTP JSON messages via FCM. See the FCM HTTP protocol reference.

The following table lists the fields of the notification document that is available as a permitted field for Push Notifications rules. To access one of these fields in a rule, use "%%args.notification.<field>".

FieldTypeDescription
titlestringThe title of the notification. Corresponds to the title option for HTTP JSON messages via FCM. See the FCM HTTP protocol reference.
bodystringThe body of the notification. Corresponds to the body option for HTTP JSON messages via FCM. See the FCM HTTP protocol reference.
soundstringThe sound to play upon receipt of the notification. Corresponds to the sound option for HTTP JSON messages via FCM. See the FCM HTTP protocol reference.
clickActionstringThe action to take when a user click on the notification. Corresponds to the click_action option for HTTP JSON messages via FCM. See the FCM HTTP protocol reference.
bodyLocKeystringThe key for localization of the body string. Corresponds to the body_loc_key option for HTTP JSON messages via FCM. See the FCM HTTP protocol reference.
bodyLocArgsstringThe string values to replace format specifiers for localization in the body string. Corresponds to the body_loc_args option for HTTP JSON messages via FCM. See the FCM HTTP protocol reference.
titleLocKeystringThe key for localization of the title string. Corresponds to the title_loc_key option for HTTP JSON messages via FCM. See the FCM HTTP protocol reference.
titleLocArgsstringThe string values to replace format specifiers for localization in the title string. Corresponds to the title_loc_args option for HTTP JSON messages via FCM. See the FCM HTTP protocol reference.
iconstringFor Android only. The notification icon. Corresponds to the icon option for HTTP JSON messages via FCM. See the FCM HTTP protocol reference.
colorstringFor Android only. Indicates the icon color in #rrggbb format. Corresponds to the color option for HTTP JSON messages via FCM. See the FCM HTTP protocol reference.
tagstringFor Android only. If specified, each notification does not result in a new entry but replaces an existing entry with the specified tag. If unset, each notificaiton results in a new entry. Corresponds to the tag option for HTTP JSON messages via FCM. See the FCM HTTP protocol reference.
badgestringFor iOS only. The badge on the gclient app home icon. Corresponds to the badge option for HTTP JSON messages via FCM. See the FCM HTTP protocol reference.
Give Feedback