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