You need to sign in to do that
Don't have an account?
Karel Michek
How to cleanup CustomNotification sent by Page reference
Hi, I have developed a custom notification in Apex using a page reference to an Aura component, and it works fine (user clicks on notifications under the Bell icon top right and get forwarded to the aura component). But I cannot find a way how to cleanup old notifications, is there a way, please? The system doesn't cleanup automatically, it probably must be done programmatically.
My apex code:
Messaging.CustomNotification notification = new Messaging.CustomNotification();
notification.setTitle('...');
notification.setBody('...');
notification.setSenderId(UserInfo.getUserId());
notification.setNotificationTypeId([SELECT Id FROM CustomNotificationType WHERE DeveloperName ... LIMIT 1].Id);
User user = [SELECT Id FROM User WHERE ... LIMIT 1];
Map<String, Object> pageRef = new Map<String, Object>{
'type' => 'standard__component',
'attributes' => new Map<String, Object>{
'componentName' => 'c__...'
},
'state' => new Map<String, Object>{
'c__jobId' => ...,
'c__userId' => ...
}
notification.setTargetPageRef(JSON.serialize(pageRef));
notification.send(new Set<String>{
user.Id
});
then the Aura component:
implemets="flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,lightning:isUrlAddressable">
My apex code:
Messaging.CustomNotification notification = new Messaging.CustomNotification();
notification.setTitle('...');
notification.setBody('...');
notification.setSenderId(UserInfo.getUserId());
notification.setNotificationTypeId([SELECT Id FROM CustomNotificationType WHERE DeveloperName ... LIMIT 1].Id);
User user = [SELECT Id FROM User WHERE ... LIMIT 1];
Map<String, Object> pageRef = new Map<String, Object>{
'type' => 'standard__component',
'attributes' => new Map<String, Object>{
'componentName' => 'c__...'
},
'state' => new Map<String, Object>{
'c__jobId' => ...,
'c__userId' => ...
}
notification.setTargetPageRef(JSON.serialize(pageRef));
notification.send(new Set<String>{
user.Id
});
then the Aura component:
implemets="flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,lightning:isUrlAddressable">
This batch job deletes the CustomNotification records that were created more than 30 days ago (this is defined in the CUT_OFF_DATE constant). You can schedule the batch job to run daily or weekly using a Scheduled Apex class, or you can run it manually by calling it in the Developer Console.