ionic-team / ionic-team/capacitor-plugins

[@capacitor/push-notifications] On Android, custom data is missing from Notifications returned by `PushNotifications.getDeliveredNotifications()`

Open
#2,515 0 comments 1 reaction 0 assignees View on GitHub
platform: android
Dominant language
Java
Stars
678
Forks
685
Avg merge
4d 22h
Merged PRs (30d)
3

Description

## Bug Report

### Plugin(s)
@capacitor/push-notifications@7.0.6

### Capacitor Version

```
💊 Capacitor Doctor 💊

Latest Dependencies:

@capacitor/cli: 8.3.1
@capacitor/core: 8.3.1
@capacitor/android: 8.3.1
@capacitor/ios: 8.3.1

Installed Dependencies:

@capacitor/cli: 7.4.3
@capacitor/android: 7.4.3
@capacitor/core: 7.4.3
@capacitor/ios: 7.4.3

[success] iOS looking great! 👌
[success] Android looking great! 👌
```

### Platform(s)
Android (API version 36)

### Current Behavior

Currently on Android, `PushNotifications.getDeliveredNotifications()` returns an object containing the `title` and `body` of each delivered notification, along with a `data` key which contains misc metadata, but is missing any custom data passed from the backend.

This is different from what is returned by the `pushNotificationActionPerformed` event, which does include the custom data passed from the backend on the `data` key.

This is also different from the `iOS` behavior, where the custom data is provided in both places on the `data` key in equivalent structures.

Effectively, this means I can't pass app specific metadata to the frontend on Android in order to properly sync notifications between the app and the OS.

Here's an example of what is returned by `getDeliveredNotifications()` on Android:

```
{
"notifications": [
{
"id": 0,
"tag": "FCM-Notification:13435916",
"title": "You have a new notification on Communities",
"body": "Test User commented, \"Test\", on your post, \"Hey all!\"",
"groupSummary": false,
"data": {
"android.title": "You have a new notification on Communities",
"android.reduced.images": true,
"android.template": "android.app.Notification$BigTextStyle",
"android.showChronometer": false,
"android.text": "Test User commented, \"Test\", on your post, \"Hey all!\"",
"android.progress": 0,
"androidx.core.app.extra.COMPAT_TEMPLATE": "androidx.core.app.NotificationCompat$BigTextStyle",
"android.progressMax": 0,
"android.appInfo": "[REDACTED]",
"android.showWhen": true,
"android.bigText": "Test User commented, \"Test\", on your post, \"Hey all!\"",
"android.progressIndeterminate": false
}
}
]
}
```

Here's an example of what's returned in the event from `pushNotificationActionPerformed` on Android:

```
{
"actionId": "tap",
"notification": {
"id": "0:1776711644878440%d022fcc3d022fcc3",
"data": {
"google.delivered_priority": "normal",
"google.sent_time": 1776711644874,
"google.ttl": 2419200,
"google.original_priority": "normal",
"google.product_id": [REDACTED],
"from": "[REDACTED]",
"path": "/john-doe/c01ff906-3768-4f50-b3c8-136dfa04f849#comment-06534e92-5529-44d3-b596-6c419ac83adb", // Custom data.
"notificationId": "aa07dc5f-f02a-481f-80eb-e6aade1fd348", // Custom data.
"gcm.n.analytics_data": "Bundle[mParcelledData.dataSize=100]",
"collapse_key": "[REDACTED]"
}
}
}
```

In the above, the following keys are custom data passed from the back end:
```
"path": "/john-doe/c01ff906-3768-4f50-b3c8-136dfa04f849#comment-06534e92-5529-44d3-b596-6c419ac83adb",
"notificationId": "aa07dc5f-f02a-481f-80eb-e6aade1fd348",
```

On iOS the custom data keys are present in both places.

Here's an example of what is returned by `getDeliveredNotifications()` on iOS:

```
{
"notifications": [
{
"body": "Test User commented, \"Test\", on your post, \"Hey all!\"",
"data": {
"aps": {
"badge": 1,
"sound": "default",
"alert": {
"body": "Test User commented, \"Test\", on your post, \"Hey all!\"",
"title": "You have a new notification on Communities"
}
},
"notificationId": "e510d2d2-ca1a-4183-99a0-a2de711f668b",
"path": "/john-doe/c01ff906-3768-4f50-b3c8-136dfa04f849#comment-722e077c-f3dc-4530-ba4c-120a3f65d527"
},
"id": "D02E1986-2C64-43B5-96F1-BE038F0B2A38",
"title": "You have a new notification on Communities",
"subtitle": "",
"badge": 1
}
]
},
```

Here's an example of what's returned in the event from `pushNotificationActionPerformed` on iOS:

```
{
"notification": {
"subtitle": "",
"body": "Test User commented, \"Test\", on your post, \"Hey all!\"",
"id": "D02E1986-2C64-43B5-96F1-BE038F0B2A38",
"data": {
"path": "/john-doe/c01ff906-3768-4f50-b3c8-136dfa04f849#comment-722e077c-f3dc-4530-ba4c-120a3f65d527",
"aps": {
"sound": "default",
"badge": 1,
"alert": {
"body": "Test User commented, \"Test\", on your post, \"Hey all!\"",
"title": "You have a new notification on Communities"
}
},
"notificationId": "e510d2d2-ca1a-4183-99a0-a2de711f668b"
},
"title": "You have a new notification on Communities",
"badge": 1
},
"actionId": "tap"
}
```

### Expected Behavior

The expectation is that the custom data should be included in both objects on the same `data` path, matching the behavior seen in iOS.

### Code Reproduction
To reproduce the issue, trigger a notification from a backend server using Firebase Messaging and include custom data in a `data` element. EG.

```
// Not shown, authorizing with Firebase.

const { getMessaging } = require('firebase-admin/messaging')
const message = {
token: deviceToken,
notification: {
title: "You have a new notification on Communities",
body: "Test User commented, \"Test\", on your post, \"Hey all!\""
},
data: {
path: "/john-doe/c01ff906-3768-4f50-b3c8-136dfa04f849#comment-06534e92-5529-44d3-b596-6c419ac83adb",
notificationId: "aa07dc5f-f02a-481f-80eb-e6aade1fd348"
}
}
const response = await this.messaging.send(message)
```

### Other Technical Details
N/A

### Additional Context
N/A

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.