Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Know if the app received the notification from server side

I'm building a newspaper-like app and I would like to know how many people received the article's push notification vs how many actually read it.

I was thinking to implement a way in which when the notification is received the app wakes up and send a request to the server saying "Hi I'm _____, I've received the notification of the article ____" and store it in the database. Then afterwards if the user click on the notification and goes to read the article I send another request saying "Hi I'm ____ and I've read the article _____" and I also store it on the database. Afterwards with some queries I'm able to understand the percentage read/received.

I don't understand if it's even possible to wake up the app even if it was not opened by the user in a while and send a request to the server (for background is meant that the application is not launched or that is in the cache ?).

I would like to achieve what they did with Whatsapp:

  • I receive a new message on Whatsapp
  • I don't open the app
  • I go to WhatsApp Web
  • I open the conversation on WhatsApp Web
  • The badge and the notification on the phone goes away because I read it somewhere else

I think that that feature is achieved with silent push notifications that just update the app badge and clear the read notification.

like image 502
Matteo Cardellini Avatar asked Oct 18 '16 22:10

Matteo Cardellini


2 Answers

Thats a very nice question on how to implement such silent notifications. There are few variables here that we need to consider and deal them in a different way.

Push notifications sent to the users - Some of them would have received it, Some may not have received it at all.

Pushing multiple notifications to the same user in a small amount of time - It becomes difficult here to track the exact notification user opened the app. Because user might have read all the news that received notifications in a single attempt.

The actual content displayed to the user in the app - User might have opened the app because of notifications. Some times he might have seen the notifications and then opened the app directly without interacting with the notifications.

So this is how the implementation can be.

  1. Implement push notifications for the app
  2. User receives the push notifications and the notification badge shows Number (1).
  3. Now when the user views the same news story in any other medium (Your own Mac App or PC app). Server is notified of the users action and the news he/she/whoever just read.
  4. Now the server knows it has sent a notification and it is not read. When you receive the read notification, you can send a remote notification that can be handled by the app in background and update the badge.

Check out this link for more details on how to handle notifications in various modes.

Apple documentation also can be referred here for background mode - remote-notification.

So you will be making your app run in background with certain settings to respond to silent notifications and update the badge just like WhatsApp. I hope this helps.

like image 155
Praveen Kumar Avatar answered Sep 20 '22 00:09

Praveen Kumar


I've already implemented such thing in one of my app, and it's actually tricky. You'll have a lot of use cases to handle.

  • First thing (but you seem to already know it): Apple does not provide any callback to say : "this notification was sent"

  • Second thing : when your app is killed (not even in background), nothing at all can be done with your notification, meaning your app won't be able to wake up and read the notification, and therefor do something. The only thing you can do is changing the badge number, even if your app is killed.

  • Third thing : when your app is in background, you can wake up your app during 30sec. During that time you can send a request to the server, but if it takes too long, the process will be killed by the OS.

Saying that, here is a quick explanation of how you could implement the system:

  1. You'll need on the server side to save in your data base any notifications that were sent. As soon as they are sent, save them as "pending"
  2. On the app side: if your app is in background, as soon as the notification is received, you can wake up your app to send a request to the server. Then in your data base, your notification status will change to "receive" or "notified". If your app was killed, when the user launch your app, send a request to the server to ask for all notification in "pending" state, that way your app will be up to date, as well as your badge number.
  3. If the user click on the notification, this will open your app directly on the article, that way you'll be able to send a request and say to your server that the article was received and read.
  4. If the user read your article on the web side, send a notification. Set the notification badge number with the number of actual "pending" notification in your data base.

Hope this will help you in addition of the answer of @Prav :)

like image 27
RomOne Avatar answered Sep 21 '22 00:09

RomOne