Customising push messages
The Notification Plugin by default handles a specific format of push notification message data sent by the NDS:
{
"notification_type": "payment_received",
"notification_payload": "{ \"payment_hash\": \"\" }",
"app_data": "..."
}
If you want to customise the structure or field names of the push notification message data handled by the Notification Plugin, you can follow the code example below for adding these to iOS and Android. In these examples we replace the notification_type and notification_payload fields with custom_type and custom_payload.
Swift
getTaskFromNotification function in the implementation of the SDKNotificationService class. Then from content.userInfo use the custom fields to get the notification type and payload:
class NotificationService: SDKNotificationService {
// Override the `getTaskFromNotification` function
override func getTaskFromNotification() -> TaskProtocol? {
guard let content = bestAttemptContent else { return nil }
guard let notificationType = content.userInfo["custom_type"] as? String else { return nil }
self.logger.log(tag: TAG, line: "Notification payload: \(content.userInfo)", level: "INFO")
self.logger.log(tag: TAG, line: "Notification type: \(notificationType)", level: "INFO")
guard let payload = content.userInfo["custom_payload"] as? String else {
contentHandler!(content)
return nil
}
self.logger.log(tag: TAG, line: "\(notificationType) data string: \(payload)", level: "INFO")
switch(notificationType) {
case Constants.MESSAGE_TYPE_ADDRESS_TXS_CONFIRMED:
return RedeemSwapTask(payload: payload, logger: self.logger, contentHandler: contentHandler, bestAttemptContent: bestAttemptContent)
case Constants.MESSAGE_TYPE_LNURL_PAY_INFO:
return LnurlPayInfoTask(payload: payload, logger: self.logger, config: self.config, contentHandler: contentHandler, bestAttemptContent: bestAttemptContent)
case Constants.MESSAGE_TYPE_LNURL_PAY_INVOICE:
return LnurlPayInvoiceTask(payload: payload, logger: self.logger, config: self.config, contentHandler: contentHandler, bestAttemptContent: bestAttemptContent)
case Constants.MESSAGE_TYPE_PAYMENT_RECEIVED:
return ReceivePaymentTask(payload: payload, logger: self.logger, contentHandler: contentHandler, bestAttemptContent: bestAttemptContent)
default:
return nil
}
}
}
Kotlin
RemoteMessage.asMessage function in the implementation of the MessagingService class. Then from data use the custom fields to get the notification type and payload:
class ExampleFcmService : MessagingService, FirebaseMessagingService() {
// Change the `RemoteMessage.asMessage` function
private fun RemoteMessage.asMessage(): Message? {
return data["custom_type"]?.let {
Message(data["custom_type"], data["custom_payload"])
}
}
}