Hi guys, i implemented the push notifications in my ionic-angular project with the FCM Capacitor. I followed the documentation on installing the Capacitor plugin in the project. But in the part where I get the notification with the app open on the iphone it doesn’t the way i want, i just want to pop up a toast and not a normal notification, only on android i can see that toast working not on iphone. From what I saw it is a bug of the Capacitor itself but I didn’t see anyone that I can solve.
Capacitor Link Docs.:
Has anyone gone through the same? If yes, has anyone solved it? I found no solution.
My approach.:
import { Injectable } from "@angular/core";
import { AlertController, ToastController } from '@ionic/angular';
import { FirestoreService } from './firestore.service';
import { Plugins, PushNotification, PushNotificationToken, PushNotificationActionPerformed } from '@capacitor/core';
import { User } from "../_models/user.model";
const { PushNotifications } = Plugins;
@Injectable({ providedIn: 'root' })
export class PushNotificationService {
constructor(
private firestoreService: FirestoreService,
private toastController: ToastController,
private alertController: AlertController
){}
initPush(user: User){
alert('teste de notificações');
// Request permission to use push notifications
// iOS will prompt user and return if they granted permission or not
// Android will just grant without prompting
PushNotifications.requestPermission().then( result => {
if (result.granted) {
// Register with Apple / Google to receive push via APNS/FCM
PushNotifications.register();
} else {
// Show some error
}
});
// On success, we should be able to receive notifications
PushNotifications.addListener('registration',
(token: PushNotificationToken) => {
this.firestoreService.updateDeviceToken(user._id, token.value);
alert('Push registration success, token: ' + token.value);
}
);
// Some issue with our setup and push will not work
PushNotifications.addListener('registrationError',
(error: any) => {
alert('Foi encontrado erro em: ' + JSON.stringify(error));
}
);
// Show us the notification payload if the app is open on our device
PushNotifications.addListener('pushNotificationReceived',
(notification: PushNotification) => {
alert('Push received: ' + JSON.stringify(notification));
}
);
// Method called when tapping on a notification
PushNotifications.addListener('pushNotificationActionPerformed',
(notification: PushNotificationActionPerformed) => {
alert('Push action performed: ' + JSON.stringify(notification));
}
);
}
async notificationToast() {
const toast = await this.toastController.create({
position: 'top',
color: 'dark',
message: 'Nova notificação',
duration: 2000
});
toast.present();
}
}