Using Apex Triggers to Send Push Notifications
After registering with the mobile OS vendor for push notification service and creating a external client app, you can send push notifications to a mobile client app using Apex triggers.
Legacy Cloud Messaging API users are required to migrate to the new Firebase Cloud Messaging API (HTTP v1) by June 2024. We recommend that you test your app’s compatibility with HTTP v1 before migrating.
Push notification triggers use methods in the Apex Messaging.PushNotification and Messaging.PushNotificationPayload classes. The external client app in the Salesforce organization represents the mobile client app that will receive the notifications.
To send push notifications to a external client app, either of the following conditions must be met:
- Apex triggers are added in the same organization in which the external client app is created.
- Apex triggers are installed as part of a partner-provided managed package, along with the external client app.
This sample Apex trigger sends push notifications to the external client app named Test_App, which corresponds to a mobile app on iOS mobile clients. The trigger fires after cases have been updated and sends the push notification to two users: the case owner and the user who last modified the case.
The trigger sample uses Messaging.PushNotificationPayload to create a payload for an iOS notification. Unlike iOS, Android doesn’t have special attributes or requirements for the payload; it just needs to be in JSON format. In Apex, you create the Android payload as a MAP<String,ANY> object. The Messaging.PushNotification class handles conversion to JSON. Here’s a sample Android payload.
Send a mobile push notification to client apps on users’ devices. This resource is only accessible when the session is established with a client app that is developed in the same org or installed in the same package as the recipient’s client app.
Resource:
Available version: 31.0
HTTP methods: POST
Request body for POST:
Root XML tag: <pushNotification>
JSON example:
Properties
| Name | Type | Description | Required or Optional | Available Version |
|---|---|---|---|---|
appName | String | The API name of the client app that the push notification is sent to. | Required | 31.0 |
namespace | String | The namespace of the client app that the push notification is sent to. | Required if the namespace is set | 31.0 |
payload | String | The push notification payload in JSON format. | Required | 31.0 |
userIds | String[] | The push notification recipient user ids. | Required | 31.0 |
Request parameters for POST
| Parameter Name | Type | Description | Required or Optional | Available Version |
|---|---|---|---|---|
appName | String | The API name of the client app that the push notification is sent to. | Required | 31.0 |
namespace | String | The namespace of the client app that the push notification is sent to. | Required if the namespace is set | 31.0 |
payload | String | The push notification payload in JSON format. | Required | 31.0 |
userIds | String[] | The push notification recipient user ids. | Required | 31.0 |