capacitor-plugins icon indicating copy to clipboard operation
capacitor-plugins copied to clipboard

PushNotifications.addListener('pushNotificationReceived') is triggered twice for single push notification in iOS 18

Open mdivya-symplr opened this issue 1 year ago • 7 comments

Bug Report

Plugin(s)

@capacitor/push-notifications

Capacitor Version

💊 Capacitor Doctor 💊

Latest Dependencies:

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

Installed Dependencies:

@capacitor/cli: 6.0.0 @capacitor/core: 6.0.0 @capacitor/android: 6.0.0 @capacitor/ios: 6.1.2

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

Platform(s)

iOS

Current Behavior

PushNotifications.addListener('pushNotificationReceived') event is triggered twice for single push notification in iOS18. This works fine in Android as well as lower iOS 17.* versions

Expected Behavior

PushNotifications.addListener('pushNotificationReceived') event to trigger once for single push notification

Code Reproduction

import { PushNotifications } from '@capacitor/push-notifications';

const addListeners = async () => {
  await PushNotifications.addListener('registration', token => {
    console.info('Registration token: ', token.value);
  });

  await PushNotifications.addListener('registrationError', err => {
    console.error('Registration error: ', err.error);
  });

  await PushNotifications.addListener('pushNotificationReceived', notification => {
    console.log('Push notification received: ', notification);
  });

  await PushNotifications.addListener('pushNotificationActionPerformed', notification => {
    console.log('Push notification action performed', notification.actionId, notification.inputValue);
  });
}

const registerNotifications = async () => {
  let permStatus = await PushNotifications.checkPermissions();

  if (permStatus.receive === 'prompt') {
    permStatus = await PushNotifications.requestPermissions();
  }

  if (permStatus.receive !== 'granted') {
    throw new Error('User denied permissions!');
  }

  await PushNotifications.register();
}

const getDeliveredNotifications = async () => {
  const notificationList = await PushNotifications.getDeliveredNotifications();
  console.log('delivered notifications', notificationList);
}

Additional Context

Similar behaviour observed for capacitor keyboard events for window addEventListener

    window.addEventListener('keyboardWillShow', () => {
      console.log('addEventListener - keyboardWillShow');
    });

mdivya-symplr avatar Sep 18 '24 14:09 mdivya-symplr

With further investigation it seems like willPresent method is being triggered twice in iOS 18 and there is a open issue for it - https://forums.developer.apple.com/forums/thread/761597?answerId=799771022#799771022 They are suggesting workaround to use UNNotificationRequest.identifier or UNNotificationContent.threadIdentifier. When can we expect fix for this from capacitor side? or Any suggestions further on this?

mdivya-symplr avatar Sep 19 '24 04:09 mdivya-symplr

This issue needs more information before it can be addressed. In particular, the reporter needs to provide a minimal sample app that demonstrates the issue. If no sample app is provided within 15 days, the issue will be closed.

Please see the Contributing Guide for how to create a Sample App.

Thanks! Ionitron 💙

Ionitron avatar Oct 16 '24 08:10 Ionitron

I'm having the same problem! 😱

Captura de ecrã 2024-10-23, às 13 24 25

marcoagsa avatar Oct 23 '24 12:10 marcoagsa

I am having the issue with Android.

co-dax avatar Nov 05 '24 15:11 co-dax

For me all methods are working except pushNotificationReceived on Android. This only works sporadically

mleister97 avatar Mar 21 '25 16:03 mleister97

Same issue...

rg4sun avatar Jun 15 '25 14:06 rg4sun

@rg4sun I generate a custom push notification id as a guid and send it in the push notification as data and then check if the event pushNotificationReceived has already received/processed the push notification with that thus ensuring it pushNotificationReceived processes the push notification only once.

co-dax avatar Jun 15 '25 15:06 co-dax