ionic-team / capacitor

Build cross-platform Native Progressive Web Apps for iOS, Android, and the Web ⚑️
https://capacitorjs.com
MIT License
12.04k stars 999 forks source link

bug: LocalNotifications listener never fired #4017

Closed Gulivertx closed 3 years ago

Gulivertx commented 3 years ago

Bug Report

Capacitor Version

Latest Dependencies:

  @capacitor/cli: 2.4.5
  @capacitor/core: 2.4.5
  @capacitor/android: 2.4.5
  @capacitor/electron: 2.4.5
  @capacitor/ios: 2.4.5

Installed Dependencies:

  @capacitor/cli 2.4.3
  @capacitor/android 2.4.4
  @capacitor/ios 2.4.4
  @capacitor/core 2.4.3
  @capacitor/electron not installed

[success] Android looking great! πŸ‘Œ
  Found 3 Capacitor plugins for ios:
    cordova-plugin-device (2.0.3)
    cordova-plugin-network-information (2.0.2)
    phonegap-plugin-barcodescanner (8.1.0)
[success] iOS looking great! πŸ‘Œ

Platform(s)

Current Behavior

The listeners localNotificationReceived and localNotificationActionPerformed are never fired.

Expected Behavior

When a notification is received and actions clicked the listeners should be fired.

Code Reproduction

  useIonViewWillEnter(async () => {
    try {
      const notificationsPermission = await LocalNotifications.requestPermission();
      console.debug('Notifications granted ?', notificationsPermission.granted);

      // Create an action for notification
      await LocalNotifications.registerActionTypes({
        types: [
          {
            id: 'ESTABLISHMENT_MSG',
            actions: [
              {
                id: 'yes',
                title: 'Yes',
                destructive: false
              },
              {
                id: 'no',
                title: 'No',
                destructive: true
              }
            ]
          }
        ]
      });

      // Create a notification
      await LocalNotifications.schedule({
        notifications: [
          {
            title: `Test Notifications with actions` ,
            body: "Actions yes or no",
            id: new Date().getTime(),
            schedule: { at: new Date(Date.now() + 1000 * 5) },
            actionTypeId: 'ESTABLISHMENT_MSG',
            extra: {
              data: 'Pass data in your handler'
            }
          }
        ]
      });

      LocalNotifications.addListener('localNotificationReceived', (notification: LocalNotification) => {
        console.debug(notification);
      });

      LocalNotifications.addListener('localNotificationActionPerformed', (notification: LocalNotificationActionPerformed) => {
        console.debug(notification);
      });
    } catch (errors) {
      console.debug(errors);
    }
  });

Other Technical Details

npm --version output: 6.14.9

node --version output: v15.2.1

pod --version output (iOS issues only): 1.10.0

Thanks for your help.

Gulivertx commented 3 years ago

humm now it works!!!! I just moved all the notification code in a new async function and listeners are correctly fired! Sorry for this issue, I closed it.

ionitron-bot[bot] commented 1 year ago

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Capacitor, please create a new issue and ensure the template is fully filled out.