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...

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="...

Salesforce CPQ Pricing Models

This post briefly covers the different pricing models which Salesforce CPQ has currently :- 1. Discount Schedule It has two types :-     a> Slab     b> Range Steps to implement :- - Create a discount schedule record , select slab or Range as type and Percent/Amount as discount unit. - Create discount tiers with the unit range. Tier Name  Lower Boundary Upper Boundary Discount(%) 1-10 Units 1 11 0 11-20 Units 11 21 5 21-30 Units 21 31 10 30+ Units 31 41 15  - Add this discount schedule to the Product you want to apply the pricing. Calculation :- if Type = Slab , List price = $100 and Quantity = 11 Total Price = Tier 1 Quantity * List Price * (1- Disc.%) + Tier 2 Quantity * List Price * (1- Disc. %)                   =  10 * 100 * (1-0)  + 1* 100 * (1-0.05)          ...