Displaying notifications while app is running in Android #3660
Replies: 4 comments 3 replies
-
Still struggling with this, would really appreciate any help! |
Beta Was this translation helpful? Give feedback.
-
You could show an ion-alert when you are inside the app and receive a push notification, like:
|
Beta Was this translation helpful? Give feedback.
-
I've managed to solve my problem by using another plugin called I call the import {
PushNotificationSchema,
PushNotifications,
} from '@capacitor/push-notifications';
import { LocalNotifications, ScheduleOptions } from '@capacitor/local-notifications';
// ...
PushNotifications.addListener(
'pushNotificationReceived',
async (notification: PushNotificationSchema) => {
let not: ScheduleOptions = {
notifications: [{
id: Date.now(),
body: notification.body,
title: notification.title,
ongoing: false,
}]
};
const result = await LocalNotifications.schedule(not)
console.log(result)
}
); Hope it helps |
Beta Was this translation helpful? Give feedback.
-
I have the same problem only for Android, on iOS native push comes even with opened app, so I had to make a toast for Android: import {NavController, Platform, ToastController} from '@ionic/angular';
...
if (this.platform.is('android')) {
PushNotifications.addListener('pushNotificationReceived', (notification: PushNotificationSchema) => {
this.toastCtrl.create({
message: `${notification.title}: ${notification.body}`,
position: 'top',
buttons: [{
icon: 'chevron-forward-outline',
side: 'start',
role: 'show',
handler: () => this.navCtrl.navigateForward(YOUR_URL)
}, {
icon: 'close',
side: 'end',
role: 'cancel'
}]
}).then(toast => toast.present());
}
);
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I managed to get Push notifications working as intended on Android, unfortunately these only work when the app is in the background or closed.
I found a couple of sources telling me that I need to handle these within the app like this article and this answer, but I have not been able to get these to work.
Where exactly should I be extending that class to handle notifications while the app is open? What packages do I need to import? Why doesn't the capacitor notification plugin handle these out of the box?
Beta Was this translation helpful? Give feedback.
All reactions