Push Notifications with the Service Chat SDK for Android
To take advantage of push notifications from your org to your app, set up an Apex
trigger and configure your app for notifications. Pass relevant notification information, such
as case feed activity, to the Service Chat SDK using your PushNotificationListener implementation.
The following activities can cause notifications:
- Agent has connected
- Agent sent a message
- Agent requested a file transfer
- Agent canceled a file transfer
- Agent ended a session
To implement notifications, follow these steps:
- Set up authentication in your app. To learn more, see Authentication with the Service Chat SDK for Android.
-
Add FCM (Firebase Cloud Messaging) to your app. To learn more, see Google's
documentation, Add Firebase to Your Android Project.
-
Add a connected-app dependency in your module's
build.gradle file.
Service Chat SDK
1dependencies { 2 3 // Add the connected-app dependency 4 implementation "com.salesforce.service:connected-app:8.0.6" 5 6 // ... your other dependencies go here too ... 7} - Create a connected app and an Apex trigger to send a notification from your org. Follow the instructions in the Salesforce Mobile Push Notifications Implementation Guide for Creating a Connected App.
- Implement PushNotificationListener and handle the push notification event in the onPushNotificationReceived method.
-
Configure using the Sender ID, AuthTokenProvider, and the site URL.
Pass this object your push notification listener and then register your device for push notifications.
In Java:
1// Create a connected app object 2SalesforceConnectedApp connectedApp = 3 SalesforceConnectedApp.create(this, new ConnectedAppConfiguration.Builder() 4 .gcmSenderId(uniqueProjectId) 5 .salesforceInstanceURL(siteUrl) 6 .authTokenProvider(authProvider) 7 .build()); 8 9// Add the push notification listener 10connectedApp.addPushNotificationListener(myPushNotificationListener); 11 12// Register for push notifications 13connectedApp.registerDeviceForPushNotifications().onError(new Async.ErrorHandler() { 14 @Override public void handleError (Async<?> operation, @NonNull Throwable throwable) { 15 // TO DO: Handle error 16 } 17});In Kotlin:
1// Create a connected app object 2val connectedApp = SalesforceConnectedApp.create(this, ConnectedAppConfiguration.Builder() 3 .gcmSenderId(uniqueProjectId) 4 .salesforceInstanceURL(siteUrl) 5 .authTokenProvider(authProvider) 6 .build()) 7 8// Add the push notification listener 9connectedApp.addPushNotificationListener(myPushNotificationListener) 10 11// Register for push notifications 12connectedApp.registerDeviceForPushNotifications().onError(object: Async.ErrorHandler { 13 override fun handleError(operation: Async<*>?, throwable: Throwable) { 14 // TO DO: Handle error 15 } 16})