I have local notifications in my chat app but they only fire if user is in same group page as notification is fired!
Example
chat group 1:
- user 1 send message
- user 2 receive notification if he is in
chat group 1
at the same time. If user 2 app is closed or he is in another page won’t receive notification
Code
app.components.ts
import {
Plugins,
LocalNotificationScheduleResult, //here
NetworkStatus,
PluginListenerHandle,
PushNotification,
PushNotificationToken,
PushNotificationActionPerformed } from '@capacitor/core';
export class AppComponent implements OnInit, OnDestroy {
notifs: LocalNotificationScheduleResult; //here
pendingNotifs: LocalNotificationScheduleResult; //here
constructor(...){...}
async ngOnInit() {
// local notifications
await Plugins.LocalNotifications.requestPermissions();
try {
Plugins.LocalNotifications.addListener('localNotificationReceived', (notification) => {
console.log('Notification: ', notification);
});
Plugins.LocalNotifications.addListener('localNotificationActionPerformed', (notification) => {
console.log('Notification action performed', notification);
});
} catch (e) {
console.log(e);
}
}
myTab.page.ts that pushes notifications
import { Plugins } from '@capacitor/core';
const { Toast } = Plugins;
ngOnInit() {
this.socket.fromEvent('message').subscribe(async (message: any) => {
// notif (should push notifications)
const notifs = await Plugins.LocalNotifications.schedule({
notifications: [
{
title: 'Local Notifications',
body: message.msg.message,
id: Math.floor(Math.random() * 10),
schedule: { at: new Date(Date.now() + 1000 * 2) },
sound: 'beep.aiff',
attachments: null,
actionTypeId: 'OPEN_CHAT',
extra: null
}
]
});
console.log('scheduled notifications', notifs);
});
}
Any idea?
1 post - 1 participant