Publishing to multiple devices

If you want to send push notifications to multiple devices, Device Interests are the way to go. This guide will demonstrate how to publish to multiple devices with Beams, using Device Interests.

This guide uses Device Interests to send public broadcasts to subscribed users. For increased security you can use Authenticated Users instead.

Add a Device Interest

In your chosen client SDK, you can call the addDeviceInterest method, passing in the Interest name.

For example, here we subscribe to the Interest "hello".

iOS

try? self.beamsClient.addDeviceInterest(interest: "hello")

Android

PushNotifications.addDeviceInterest("hello");

This device has now subscribed to the Interest "hello" and will receive any push notifications published to "hello".

Device Interest names are limited to 164 characters and can only contain ASCII upper/lower-case letters, numbers or one of _-=@,.;


Publish to the Device Interest

Now to publish to all devices that are subscribed to hello, call the publishToInterests method, passing an array containing hello.

  • Node.js
  • PHP
  • Go
  • python
  • Java
  • Kotlin
  • Ruby
  • API
beamsClient.publishToInterests(['hello'], {
  apns: {
    aps: {
      alert: 'Hello!'
    }
  },
  fcm: {
    notification: {
      title: 'Hello',
      body: 'Hello, world!'
    }
  }
}).then((publishResponse) => {
  console.log('Just published:', publishResponse.publishId);
}).catch((error) => {
  console.error('Error:', error);
});

Each publish requires at least one interest, up to a maximum of 100.


Differences between ios and Android

A publish request must target at least one of two platforms:

  • APNs (for iOS devices)
  • FCM (for Android devices)
The structure of the publish request is slightly different for each. Follow the links below for the full set of options.

For example, a publish request to only iOS devices would look something like this:

iOS (APNs only)

{
  apns: {
    aps: {
      alert: 'Hello!'
    }
  }
}

...whereas a publish request to both iOS and Android would have this structure:

iOS and Android (APNs & FCM)

1
2
3
4
5
6
7
8
9
10
11
12
13
{
  apns: {
    aps: {
      alert: 'Hello!'
    }
  },
  fcm: {
    notification: {
      title: 'Hello',
      body: 'Hello, world!'
    }
  }
}

Adding metadata to a notification

If you want to add metadata to your request, add a data property within the fcm and/or apns part of the payload, like so:

Adding metadata

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
{
  apns: {
    aps: {
      alert: 'Hello!'
    },
    data: {
      some: 'metadata',
      of: 'your',
      choosing: 'can',
      go: 'here 😏'
    }
  },
  fcm: {
    notification: {
      title: 'Hello',
      body: 'Hello, world!'
    },
    data: {
      put: 'whatever',
      you: 'need',
      here: '✨'
    }
  }
}