Ninja Turtle
Ninja Turtle

Reputation: 1363

Ionic 6 capacitor `pushNotificationActionPerformed ` event is not getting triggered on clicking push notification

I am implementing push notification in my Ionic 6 App. I am using @capacitor/push-notifications plugin to manage push notification in my Ionic App.

import { Injectable } from '@angular/core';
import { Capacitor } from '@capacitor/core';
import { PushNotifications } from '@capacitor/push-notifications';

import { Router } from '@angular/router';

@Injectable({
  providedIn: 'root',
})
export class FcmService {
  constructor(private router: Router) {}

  initPush() {
    const fcmtoken = localStorage.getItem('fcmtoken');
    if (Capacitor.platform !== 'web' && !fcmtoken) {
      this.registerNotifications();
    }
  }

  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();

    await PushNotifications.addListener('registration', token => {
      console.info('Registration token: ', token.value);
      localStorage.setItem('fcmtoken', 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 => {
      alert(JSON.stringify(notification));
      console.log('Push notification action performed', notification.actionId, notification.inputValue);
    });
  }

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

I am calling the initPush() from AppComponent and i am receiving notification in my app. But when i tap on that notification nothing happens. pushNotificationActionPerformed event is not getting triggered. Am i missing some configuration. I am using it in Android app.

Please help if someone has implemented it.

Upvotes: 0

Views: 2119

Answers (2)

Deval Kathrecha
Deval Kathrecha

Reputation: 11

Add this to AndroidManifest.xml file,

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

and add "click_action": "FCM_PLUGIN_ACTIVITY" to notification payload

{
    "message": {
        "token": "...",
        "notification": {
            "title": "...",
            "body": "..."
        },
        "android": {
            "notification": {
                "notification_count": 8,
                "notification_priority": "PRIORITY_HIGH",
                "click_action": "FCM_PLUGIN_ACTIVITY"
            }
        }
    }
}

Upvotes: 1

vishva
vishva

Reputation: 49

I also had the similar issue in Android, was able to receive push notification but nothing was happening when we click on the received notification.

I solved this by adding below line in AndroidManifest.xml

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

Please add above lines in activity tag

Upvotes: 1

Related Questions