Capacitor - Ionic - Android foreground push notification - pushNotificationActionPerformed never called

2.5k Views Asked by At

I am new to Capacitor and ionic framework. I am trying to use FCM to trigger and deep link into my app. Following are the outputs:

Ios: App in background, foreground and killed state - when tap on push, pushNotificationActionPerformed listener is called properly.

Android: App in background and killed state - when tap on push, pushNotificationActionPerformed listener is called properly.

App in foreground- when tap on push, pushNotificationActionPerformed is never called, I can see pushNotificationReceived listener is called but I need to capture the tap from user to deep link them into certain page of my app. Basically, nothing happens when I try to tap on push.

Following is the code I have added till now: capacitor.config.json:

"PushNotifications": {
            "presentationOptions": [
                "badge",
                "sound",
                "alert"
            ]
        }

AndroidManifest.xml:

  <intent-filter>
              <action android:name="MOBILE-PUSH-DEEP-LINK" />
              <category android:name="android.intent.category.DEFAULT" />
          </intent-filter>

Push payload:

{
    "to": "FCM token",
    "notification": {
        "body": "test",
        "title": "Dummy",
        "click_action": "MOBILE-PUSH-DEEP-LINK"
    },
    "data": {
        "data1": "test",
        "data2": "12345",
    }
}

Thanks in advance for your help!

3

There are 3 best solutions below

0
sanvi srivastava On BEST ANSWER

Implemented the following solution till capacitor comes up with a proper update to deal with foreground push notifications.

private async fcmReceiver() {

    await PushNotifications.addListener(
      'pushNotificationReceived',
      async (notification: PushNotificationSchema) => {
        //only for android foreground push notification
        //since capacitor has still not implemented tap on android //foreground notif
        if (this.platformService.isNativeAndroid) {
          await PushNotifications.getDeliveredNotifications().then((x) => {
            PushNotifications.removeDeliveredNotifications(x);
          });
          this.foregrndLocalNotif(notification);
        }
      },
    );

    await PushNotifications.addListener('pushNotificationActionPerformed', (payload) => {
      if (payload.actionId === 'tap') {
        this.handlePushMessageAction(payload.notification.data);
      }
    });

    await LocalNotifications.addListener(
      'localNotificationActionPerformed',
      (payload: ActionPerformed) => {
        if (payload.actionId === 'tap') {
          this.handlePushMessageAction(payload.notification.extra);
        }
      },
    );
  }

  private foregrndLocalNotif(notification: any) {
    LocalNotifications.schedule({
      notifications: [
        {
          id: 1,
          title: notification.title,
          body: notification.body,
          schedule: { at: new Date(Date.now() + 1000 * 1) },
          extra: notification.data,
        },
      ],
    });
  }
4
Andrew On

I also had such a problem. I found this solution. https://github.com/ionic-team/capacitor-plugins/pull/1478

You can create a local npm package with the necessary changes (I did so)

  1. Install the official package

  2. Open: node_modules/@capacitor/push-notifications/android/src/main/java/com/capacitorjs/plugins/pushnotifications/PushNotificationsPlugin.java (Make changes from the Pull Request)

  3. In the terminal, open the node_modules/@capacitor/push-notifications path and use the npm pack (It will create a tgz file)

  4. Create a folder for example local_modules and put the tgz file there

  5. In package.json insted "@capacitor/push-notifications": "^4.1.2" use "@capacitor/push-notifications": "file:local_modules/push-notifications/capacitor-push-notifications-4.1.2.tgz"

  6. In the terminal, use npm i (don't forget to change the default path in the terminal)

0
Sai Kiran On

Adding in the below in android/app/src/main/AndroidManifest.xml worked, it should be added in the activity tag.

        <intent-filter>
            <action android:name="FCM_PLUGIN_ACTIVITY" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>