Skip to main content

Send Push Notification on Salesforce1 Mobile app using apex in Salesforce.com



Salesforce chatter API has sends a Push notification to salesforce1 mobile app is we add a post and mention a user to it.

The prerequisites are :-

1. User need to installed Salesforce1 mobile app on it mobile device.
Google play  : https://play.google.com/store/apps/details?id=com.salesforce.chatter
Apple store   : https://itunes.apple.com/app/id404249815?mt=8

2. The users's profile should have "Chatter enabled".

Apex code for mentioning user on a chatter post :-

Note : This code is works on class version : 33 , there might be change in method signature in latest versions.

 
// mentionIds - list of user ids who should recieve notification
// recordId - Id of the record iof any
// recordURL - custom record url
public static void mentionChatter(String body, Id recordId , List<Id> mentionIds , String recordURL){
         try{ 
            if(body!=null && body!=''){
         
                //Create FeedItemInput to hold message body and @mentions//
                ConnectApi.FeedItemInput feedItemInput = new ConnectApi.FeedItemInput();
                feedItemInput.subjectId = recordId;
                feedItemInput.feedElementType = ConnectApi.FeedElementType.FeedItem;
             
                //Create MessageBodyInput (child of FeedItemInput) to hold message segments
                feedItemInput.body = new ConnectApi.MessageBodyInput();
             
                // Create Message segment holder
                //List<ConnectApi.MessageSegmentInput> segments = feedItemInput.body.messageSegments;
                List<ConnectApi.MessageSegmentInput> segments = new List<ConnectApi.MessageSegmentInput>();
             
                // Create a @mention segment to hold the mention //
                for(Id mentionIdStr : mentionIds){
                    ConnectApi.MentionSegmentInput mention = new ConnectApi.MentionSegmentInput();
                    mention.id = mentionIdStr;
                     // Add mention to message segment //
                    segments.add(mention);
                }
                             
                // Create Text Segment to hold message body
                ConnectApi.TextSegmentInput textSegment = new ConnectApi.TextSegmentInput();
                textSegment.text = body ;
                //Add body to message segment//
                segments.add(textSegment);
             
                if(recordURL!='' && recordURL!=null){
               
                        ConnectApi.LinkSegmentInput linkSegment = new ConnectApi.LinkSegmentInput();
                        linkSegment.url = System.URL.getSalesforceBaseUrl() +'/'+recordURL+'';
                        segments.add(linkSegment);
                 
                }
               
                feedItemInput.body.messageSegments = segments ;
                                             
                //Create FeedItemPost//
                ConnectApi.FeedElement feedElement = ConnectApi.ChatterFeeds.postFeedElement(Network.getNetworkId(), feedItemInput, null);
            }

}catch(Exception ex){
// Handle exception
}

}

Comments

Popular posts from this blog

Einstein Bot user authentication

Using Bot for data manipulation use case in your company requires need of implementing some extra security layer to make it spoof proof. One of exciting salesforce feature we have is the well known Einstein Bot which many companies have implemented. I am going to cover detail step-by-step implementation of User validation use case using encrypted token. STEP-1: Create a Site & Site User go to setup > Sites & Domains > Sites Create a Site and make your user as "Site Contact". This is a prerequisites for live agent or Embedded Service setup. STEP-2 : Create a Embedded Service(formerly snap-ins) go to Service setup > Embedded Service Create a new Embedded Service Deployment record and copy the embedded service code snipped generated in a notepad. STEP-3  : Create a Visualforce page to test the chatbot (it will simulate the actual web portal which is going to consume the embedded service snipped.) BotController.apxc public class BotControlle...

Dynamically populate lightning:combobox

In order to dynamically populate values in lightning:combobox using javascript we can create a list variable and push the values. We also require the values like ids associated to the particular option for back-end logic. For example  : If we want to show the list of contacts using lightning:combobox component where the option should display the contact name but the value should be the contact id which will be used for back-end logic For this we can create a temporary Map variable in our lightning component and set the value and label parameters of the lightning:combobox. Here is the code for this component Combo.app <aura:application extends="force:slds" access="global" description="Lightning Combo Demo"> <c:Combo_Cmpt/> </aura:application> Combo_Cmpt.cmp <aura:component implements="flexipage:availableForAllPageTypes" access="global" controller="ComboController"> <!-- ...

Use of wrapper class in lightning:datatable

As you guys know the wrapper class concept in visualforce pages , we generally use it to create a data-table which fetches data from different objects or if we want to redirect user to some other page on click of a link as one of the column of data-table.        For example we want a column "Account Name" on the data-table which is a link and once user clicks it should redirect respective account record. Or , suppose we want to display a column with some images or icons as a visual indicator Or what not. These requirements require us to use a wrapper on the lighting data-table (lightning:datatable) I am going to use my previous account search example ( Account Search Lightning Component ) and explain the use of wrapper. AccountSearchWrapper.app <aura:application extends="force:slds" access="global" >     <c:AccountSearchWrapper /> </aura:application> AccountSearchWrapper.cmp <aura:component controller="...