Search code examples
androidionic-frameworkpush-notificationcapacitor-pluginforegroundnotification

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


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!


Solution

  • 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,
            },
          ],
        });
      }