Considering as an initial situation that the application is closed, the device receives the notification normally, but does not open the application when tap at the notification.
That is, it does not execute what was defined in ‘pushNotificationActionPerformed’.
When the APP is opened the notification working ok ( ‘pushNotificationReceived’ is listened).
My code:
capacitor.config.js:
PushNotifications: {
"presentationOptions": ["badge", "sound", "alert"]
},
notification-app.service.ts:
import { Injectable, NgZone } from '@angular/core';
import { Capacitor } from '@capacitor/core';
import { ActionPerformed, PushNotifications, PushNotificationSchema, Token } from '@capacitor/push-notifications';
import { INotificacaoApp } from '../interfaces/INotificacaoApp';
@Injectable({
providedIn: 'root'
})
export class NotificationAppService {
_dadosNotificacao: INotificacaoApp;
constructor(
private zone: NgZone,
) { }
async setupFCM() {
if (Capacitor.getPlatform() == 'web') {
return;
} else {
try {
let permStatus = await PushNotifications.checkPermissions();
if (permStatus) {
if (permStatus.receive === 'prompt') {
let permStatus2 = await PushNotifications.requestPermissions();
if (permStatus2) {
if (permStatus2.receive !== 'granted') {
console.log('Notification not granted')
return;
} else {
await PushNotifications.register();
}
}
} else {
if (permStatus.receive !== 'granted') {
console.log('Notification not granted')
return;
} else {
await PushNotifications.register();
}
}
}
PushNotifications.addListener(
'registration',
async (token: Token) => {
console.log('My token: ' + JSON.stringify(token));
}
);
PushNotifications.addListener('registrationError', async (error: any) => {
console.log('Error no registro do token de notificacao: ' + JSON.stringify(error));
});
PushNotifications.addListener(
'pushNotificationReceived',
async (notification: PushNotificationSchema) => {
this.zone.run(() => {
if (notification.data
&& notification.data.dados) {
this._dadosNotificacao = JSON.parse(notification.data.dados) as INotificacaoApp;
console.log('recebeu notificacao')
console.log(this._dadosNotificacao)
}
});
}
);
PushNotifications.addListener(
'pushNotificationActionPerformed',
async (notification: ActionPerformed) => {
console.log('Action performed: ' + JSON.stringify(notification.notification));
}
);
} catch (err) {
console.log(`SetupFCM com erro`)
}
}
}
async RemoveListeners() {
PushNotifications.removeAllListeners();
}
}
The above setupFCM routine is called in tabs.page.ts
constructor(
private platform: Platform,
private notificationAppService: NotificationAppService
) {
this.platform.ready().then(async () => {
this.notificationAppService.setupFCM();
});
}
Versions:
npm --version
output: 9.1.1
node --version
output: 18.12.1
@capacitor/cli: 4.5.0
@capacitor/core: 4.5.0
@capacitor/android: 4.5.0
@capacitor/ios: 4.5.0
@capacitor/push-notifications: 4.1.2
@angular/core: 14.2.10
@ionic/angular: 6.3.7
@ionic/cli: 6.20.4
I tried several configuration adjustments, as shown in the files below:
In android/build.gradle: - added classpath for google-services:
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.2.2'
classpath 'com.google.gms:google-services:4.3.14'
}
}
In android/app/build.gradle:
added google-services in plugins and added firebase-messaging dependency
plugins {
id 'com.android.application'
id 'com.google.gms.google-services'
}
...
dependencies {
...
implementation 'com.google.firebase:firebase-messaging:23.0.5'
}
In variable.gradle: added firebaseMessagingVersion
ext {
minSdkVersion = 22
compileSdkVersion = 32
targetSdkVersion = 32
...
firebaseMessagingVersion = '23.0.5'
}
The file google-services.json is in android/app directory.
Can someone help me?