Hi guys,
I’m creating a chat app with Ionic 4, and I’m using OneSignal to send push notifications to users when a new message is received.
The notification is created on a Symfony backend, everything’s fine, notifications are sent and well received by my users.
Problem is, when I click on a notification, I want to go on my chat between me and the user who sent a message, and see the last message he sent, obviously.
When I click on the notification, my app is running on background.
I have 2 cases, one working and one not:
- Working case: When I was using the app and left it, I wasn’t on the Chat page.
- Not working case: When I was using the app and left it, I was on the Chat page. In that case, my app opens but I can’t see the last message sent to me (the one in the notification).
I used Events to manage all of this:
app.component.ts:
this.oneSignal.handleNotificationOpened().subscribe(jsonData => {
if (jsonData.notification.payload.additionalData.notificationType === 'newChatMessage') {
this.events.publish('newMessageChat', jsonData.notification.payload.additionalData.content);
this.router.navigate(['/chat/' + jsonData.notification.payload.additionalData.content.userFrom.id]);
}
});
chat.page.ts:
ionViewDidEnter() {
this.getContactInfos();
this.getUser();
this.getMessages();
this.events.subscribe('newMessageChat', (message) => {
const msgs = [];
msgs.push(message);
this.messages.forEach(msg => {
msgs.push(msg);
});
this.messages = msgs;
setTimeout(() => {
this.updateScroll();
}, 200);
});
}
ionViewWillLeave() {
this.events.unsubscribe('newMessageChat');
}
If I console.log('test');
in my event subscription, I see it on my 2 cases. But in case 2., my page isn’t moving, the last message isn’t showing.
Anyone who had this problem before to help me a bit please ?
Thanks guys !