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.
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.
Sample Apex Trigger
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.
1trigger caseAlert on Case(after update){23 for(Case cs : Trigger.New)4{5 // Instantiating a notification6 Messaging.PushNotification msg =7 new Messaging.PushNotification();89 // 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 acted15 // upon by the intended recipient is best calculated16 // at the time of the push. This timing helps17 // 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);2223 // Adding the assembled payload to the notification24 msg.setPayload(payload);2526 // Getting recipient users27 String userId1 = cs.OwnerId;28 String userId2 = cs.LastModifiedById;2930 // Adding recipient users to list31 Set<String>users = new Set<String>();32 users.add(userId1);33 users.add(userId2);3435 // Sending the notification to the specified app and users.36 // Here we specify the API name of the external client 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’s a sample Android payload.
1Map<String, Object>androidPayload = new Map<String, Object>();2androidPayload.put('number', '1');3androidPayload.put('name', 'test');
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.