7

The Winter '21 CustomNotification Class offers two ways to set the target navigated to when the message displayed under the "bell" icon is clicked:

  • setTargetId(Id targetId) - Sets the target record of the custom notification
  • setTargetPageRef(String pageRef) - Sets the target page of the custom notification

What can the pageRef string contain? I'd like to use these notifications in a community (i.e. the bell notifications) where the page to go to would need to be set via that value. So far no value I have set results in the notification appearing; the notification does appear when the targetId is set.

PS

Based on Shamina's comment/link I tried passing this JSON (without the line feeds; this is one of the LWC PageReference Types) using API 50 and got:

Invalid parameter value for: targetPageRef

for:

{
    "state":{
        "eventId":"a053F000004MTJOQA4"
    },
    "attributes":{
        "name":"event-details"
    },
    "type":"comm__namedPage"
}

I also can't get this to work with e.g. the standard__webPage type either. But see Jeferson's answer where some of the standard__* types work.

What am I missing here? Are only some PageReference types supported?

PPS

Can this be set by any means e.g. via a flow or the REST API? I tried the REST API approach, and got:

Unexpected data type for targetPageRef: expected String

with JSON data for the targetPageRef and:

Invalid parameter value for: targetPageRef

with JSON data serialized to a string for the targetPageRef.

Note that the "Send Custom Notifications" permission must be granted to avoid this error:

Unsupported action type: CUSTOM_NOTIFICATION_ACTION

3
  • With regards to Case #40382890, support did cover that the pageRef parameter should be following the format as described in PageReference Types (Like Shamina pointed out). They are going to get the documentation updated to reflect that. The support case didn't cover using the notifications in communities. Feb 5 at 23:13
  • 1
    Thanks for the update @DanielBallinger. I do see that state is not documented for comm__namedPage (but is for others) so will try removing that, though I do need to inject a query string parameter somehow. I'll also try with the c__ prefix.
    – Keith C
    Feb 6 at 8:10
  • I even tried using JSONGenerator but nothing works in setTargetPageRef from anonymous apex. Feb 11 at 13:51
3
+150

I could see it working (outside of Communities, I have not tried that part yet) with the following scenarios:

  • Named Page Type (type = standard__namedPage): quite limited but useful.
  • Item Page Type (type = standard__navItemPage): navigates to tabs.
  • Record Page Type (type = standard__recordPage): navigates to a records, in modes such as view, edit (my example), and clone.

Conclusion so far: it seems that the comment from Bartheleway, setTargetPageRef using JSON format syntax is correct, based on my tests, at least.

Note: thanks for the references shared, that LWC PageReference Types & CustomNotification Class references served as the basis of the tests I made.

Code to send a notification to the current user:

// Named Page Type. Drives to Home Page.
/*String addressTest = 
'' +
'    {' +
'        type: \'standard__namedPage\', ' +
'        attributes: {' +
'            pageName: \'home\'' +
'        }'+
'   }'+
'';*/

// Item Page Type. Drives to a custom tab named NotificationTab.
/*String addressTest = 
'' +
'    {' +
'        type: \'standard__navItemPage\', ' +
'        attributes: {' +
'            apiName: \'NotificationTab\'' +
'        }'+
'   }'+
'';*/

// Record Page Type. Drives to the edit page for the given Account record id.
String addressTest = 
'' +
'{' +
'       type: \'standard__recordPage\',' +
'       attributes: {' +
'           recordId: \'0012X000022RUSLQA4\',' +
'           objectApiName: \'Account\',' +
'           actionName: \'edit\'' +
'       }' +
'}';

// Get the Id for our custom notification type
CustomNotificationType notificationType = 
    [SELECT Id, DeveloperName 
        FROM CustomNotificationType 
        WHERE DeveloperName = 'Custom_Notification'];

// Create a new custom notification
Messaging.CustomNotification notification = new Messaging.CustomNotification();
notification.setTitle('Apex Custom Notification');
notification.setBody('The notifications are coming from INSIDE the Apex!');
notification.setNotificationTypeId(notificationType.Id);
notification.setTargetPageRef(addressTest);

// Actually send the notification
try {
    notification.send(new Set<String>{UserInfo.getUserId()});
}
catch (Exception e) {
    System.debug('Problem sending notification: ' + e.getMessage());
}
1

Leaving out the "state" value at least results in the notification being posted. But as described in A click on a bell icon notification in Communities (now called Digital Experiences) doesn't navigate, I couldn't get the "comm__namedPage" approach to navigate on a click. So instead I'm using "standard__webPage" and that does work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.