Targeting Users with Custom Notification Messages

Tutorial 3 of 5

1. Introduction

In this tutorial, our goal is to learn how to send custom notification messages to specific user segments or individual devices. We will delve into the use of topics, device groups, and single device targeting.

By the end of this tutorial, you will be able to:

  • Understand the concepts of topics, device groups, and single device targeting.
  • Implement these concepts in your own projects to send custom notification messages.

Prerequisites: Basic knowledge of web development and programming is required. Familiarity with JavaScript will be helpful.

2. Step-by-Step Guide

To send custom notifications, we need to understand a few key concepts:

Topics: Topics are a way to send a message to multiple devices that have opted in to a particular topic.

Device groups: Device groups are a way to manage multiple devices as if they were one.

Single Device Targeting: Single device targeting is a way to send a notification to a specific device.

3. Code Examples

Let's see some practical examples:

Example 1: Creating a Topic

//This is a Node.js module to interact with the Firebase Cloud Messaging service
const admin = require('firebase-admin');

admin.initializeApp();

let topic = 'news';

let message = {
  data: {
    score: '850',
    time: '2:45'
  },
  topic: topic
};

// Send a message to devices subscribed to the provided topic.
admin.messaging().send(message)
  .then((response) => {
    console.log('Successfully sent message:', response);
  })
  .catch((error) => {
    console.log('Error sending message:', error);
  });

Example 2: Sending a notification to a Device Group

let registrationTokens = [
  'YOUR_REGISTRATION_TOKEN_1',
  // ...
  'YOUR_REGISTRATION_TOKEN_n'
];

// A device group name must be unique per app
let deviceGroupName = 'my-device-group';

admin.messaging().sendToDeviceGroup(deviceGroupName, {
  notification: {
    title: 'New message!',
    body: 'This is a notification from Device Group!'
  }
})
.then((response) => {
  console.log('Successfully sent message:', response);
})
.catch((error) => {
  console.log('Error sending message:', error);
});

Example 3: Sending a notification to a specific device

let registrationToken = 'YOUR_REGISTRATION_TOKEN';

let message = {
  notification: {
    title: 'Hello Device',
    body: 'This is a message for this device!'
  }
};

admin.messaging().sendToDevice(registrationToken, message)
  .then((response) => {
    console.log('Successfully sent message:', response);
  })
  .catch((error) => {
    console.log('Error sending message:', error);
  });

4. Summary

In this tutorial, we have learned how to send custom notifications to specific user segments or individual devices, with topics, device groups, and single device targeting.

To continue learning, you can:

  • Try implementing these concepts in your projects.
  • Explore more about Firebase Cloud Messaging and its applications.

Here are some additional resources to deepen your knowledge:

5. Practice Exercises

Exercise 1: Send a notification to a topic called 'sports' with the message 'New sports update!'.

Exercise 2: Create a device group 'my-group' and send a notification with the title 'Hello Group' and body 'This is a message for the group!'.

Exercise 3: Send a notification to a specific device with registration token 'YOUR_REGISTRATION_TOKEN' with the title 'Hello Device' and body 'This is a message for this device!'.

Solutions:

Solution for Exercise 1:

let topic = 'sports';

let message = {
  data: {
    title: 'New sports update!'
  },
  topic: topic
};

admin.messaging().send(message)
  .then((response) => {
    console.log('Successfully sent message:', response);
  })
  .catch((error) => {
    console.log('Error sending message:', error);
  });

Solution for Exercise 2:

let deviceGroupName = 'my-group';

admin.messaging().sendToDeviceGroup(deviceGroupName, {
  notification: {
    title: 'Hello Group',
    body: 'This is a message for the group!'
  }
})
.then((response) => {
  console.log('Successfully sent message:', response);
})
.catch((error) => {
  console.log('Error sending message:', error);
});

Solution for Exercise 3:

let registrationToken = 'YOUR_REGISTRATION_TOKEN';

let message = {
  notification: {
    title: 'Hello Device',
    body: 'This is a message for this device!'
  }
};

admin.messaging().sendToDevice(registrationToken, message)
  .then((response) => {
    console.log('Successfully sent message:', response);
  })
  .catch((error) => {
    console.log('Error sending message:', error);
  });

For further practice, try sending different types of messages (e.g., data messages or notification messages) to your topics or device groups.