One Signal Object Push to localStorage Array

Hi

I am using one signal notifications and I want to push the notifications received object to local storage, I want it to save as array so user can browse through all received notification.

Be able to delete the read notification from locaStorage as well.

Great. So what code have you written to this end, and how is it behaving differently from your goal?

HI

This is the code I have so far, its showing only latest notification only.

// One Signal Notification Received
    private onPushReceived(notification) {
        this.notification = [];
        this.notification.push(notification);

        this.storage.set('notifications', JSON.stringify(this.notification));
        console.log('Notification Array', notification);

        // this.saveNotifications();
        console.log('Push received:' + notification);
    }

I get the data back via


    getNotifications() {
        this.storage.get('notifications').then((data) => {
        this.notifications = JSON.parse(data);
        console.log('Notifications from localStorage', this.notifications);
        });

    }

Its not adding up the new notifications to the old ones in the local storage.

Seems to me that if you combine what you’re doing in getNotifications to get the current lot, and use that instead of initializing your array to [] every time, you’d have what you’re looking for.

1 Like

Thanks, seems to be working now …

image