Example: Create Activity Feed
How to build an activity feed of notifications using OneSignal
What is an Activity Feed?
An activity feed lets users see the history of notifications they've received within your app.
OneSignal primarily focuses on delivery of messages, so at this current time OneSignal does not store notifications sent history of each individual user. However, you can implement this yourself by storing the data on your own server or on the device.
Option 1: Saving to Server
Instead of using background processing in your app, you may choose to create all your notifications with our Create notification REST API and additionally save a copy of the contents on your server. Then each time your app is started check your server for updates.
Once the data is stored by your app or on your server, you can display it to the user when you choose.
Option 2: Save the notification when received
This option may be less reliable for cases when the device is turned off for an extended period of time (see Notification Behavior & Payload Information for more details). Also, if the device or user removes the app from memory like app being force quit on Android it will not get the notification (see Notifications Not Shown for more details). For iOS 9 and older, this option will not work unless the app is running in the background or foreground when receiving the push.
First, you must setup the required code to cause your application to be woken up in the background whenever a notification is received (even if it's not clicked).
iOS - This requires a Notification Service Extension which should already be setup when following our Mobile SDK Setup Guide to add one to your Xcode project.
For iOS 9 and older device support you will need to use the
content-available flag. Read Apple's content-available guide for details on receiving and processing the event.
Within the Notification Service Extension, you can capture the Notification Payload and process it within your Activity Feed. For details on how to capture the OneSignal Notification Payload see our iOS Notification Service Extensions Feature Guide.
Android - Read our Android Background Data guide for details on receiving and processing the event.
When you process the event, you can save a copy of the notification to your app.
Option 3: OneSignal View Notifications API
The View notifications endpoint will allow you to pull the last 50 notifications in a paginated fashion. It will pull notifications that are still scheduled, so keep track of the
completed_at parameter to know if the message finished sending.
Note that the view notifications endpoint will return all messages (paginated in groups of 50), so this will not provide a list of messages sent specifically to a particular user. In order to know if a user was sent a particular message, you can use the notification ID returned by the view notifications endpoint to call the notification history endpoint. This will return a CSV file containing the player_id of all the users that this specific message was sent to, which you can then check for your current user's id.
Reducing the amount of API calls needed
This final option (3) will require a large amount of API calls to be made in order to find all of the messages sent to your user, therefore this may not be a viable option for OneSignal apps that send a large volume of messages.
In order to reduce the need for searching through all messages, it is recommended that you perform a nightly update to your database which includes all of the data from the past 24 hours that is returned by utilising the view notifications endpoint in conjunction with the notification history endpoint. This way, you can simply search your database to retrieve the notification history for a user from before the current date.
Combining this with Option 2 for messages that were sent within the last 24 hours will provide you with a complete and up-to-date list of all messages sent to this particular user.
Updated 3 months ago