Newer Version Available

This content describes an older version of this product. View Latest

Step 4. Using Apex Triggers to Send Push Notifications

After registering with the mobile OS vendor for push notification service and creating a connected app, you can send push notifications to a mobile client app using Apex triggers.

Push notification triggers use methods in the Apex Messaging.PushNotification and Messaging.PushNotificationPayload classes. The connected app in the Salesforce organization represents the mobile client app that will receive the notifications.

To send push notifications to a connected app, either of the following conditions must be met:
  • Apex triggers are added in the same organization in which the connected app is created.
  • Apex triggers are installed as part of a partner-provided managed package, along with the connected app.

Important

Sample Apex Trigger

This sample Apex trigger sends push notifications to the connected 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.

1trigger caseAlert on Case (after update) {    
2    
3    for(Case cs : Trigger.New)  
4    {
5        // Instantiating a notification
6        Messaging.PushNotification msg = 
7            new Messaging.PushNotification();
8
9        // Assembling the necessary payload parameters for Apple.
10        // Apple params are: 
11        // (<alert text>,<alert sound>,<badge count>,
12        // <free-form data>)
13        // This example doesn't use badge count or free-form data.
14        // The number of notifications that haven't been acted
15        // upon by the intended recipient is best calculated
16        // at the time of the push. This timing helps
17        // ensure accuracy across multiple target devices.
18        Map<String, Object> payload = 
19            Messaging.PushNotificationPayload.apple(
20                'Case ' + cs.CaseNumber + ' status changed to: ' 
21                + cs.Status, '', null, null);
22
23        // Adding the assembled payload to the notification
24        msg.setPayload(payload);
25
26        // Getting recipient users
27        String userId1 = cs.OwnerId;
28        String userId2 = cs.LastModifiedById;
29
30        // Adding recipient users to list
31        Set<String> users = new Set<String>();
32        users.add(userId1);
33        users.add(userId2);                       
34
35        // Sending the notification to the specified app and users.
36        // Here we specify the API name of the connected app.  
37        msg.send('Test_App', users);
38    }
39}

Sample Android Payload

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 is a sample Android payload.

1Map<String, Object> androidPayload = new Map<String, Object>();
2androidPayload.put('number', 1);
3androidPayload.put('name', 'test');