First, notification is received and the function saveNotificationToLocalStorage
is called
// app.component.ts
OneSignal.handleNotificationReceived().subscribe((data) => {
data.payload.rawPayload = JSON.parse(data.payload.rawPayload);
this.user.saveNotificationToLocalStorage(data);
});
// Function saveNotificationToLocalStorage … inside “providers/user-data.ts”
saveNotificationToLocalStorage(notification){
var tmpNotification = notification.payload;
var _isAppInFocus = notification.isAppInFocus;
var tmpArr = JSON.parse(window.localStorage.getItem("arrayOfNotifs"));
tmpArr.unshift(tmpNotification);
window.localStorage.setItem("arrayOfNotifs", JSON.stringify(tmpArr));
// Important part
if(_isAppInFocus){
this.events.publish('notifications:update', tmpArr);
}
}
And then when event is fired “dashboard.ts”
listenToEvents(){
this.events.subscribe('notifications:update', (notifications) => {
this._notifications = notifications[0];
});
}
Dashboard.html
<ion-content no-margin no-padding>
<ion-list no-margin no-padding [virtualScroll]="_notifications" approxItemHeight="40px">
<ion-item-sliding #item *virtualItem="let notification" no-margin no-padding>
<ion-item no-margin no-padding>
{{ notification.title }}
</ion-item>
<ion-item-options side="right" no-margin no-padding>
<button ion-button>
<ion-icon ios="ios-archive" md="md-archive"></ion-icon>
Save
</button>
<button ion-button color="danger">
<ion-icon ios="ios-trash" md="md-trash"></ion-icon>
Delete
</button>
</ion-item-options>
</ion-item-sliding>
</ion-list>
</ion-content>