¿Cómo puedo manejar las notificaciones push no leídas en iOS?

I have a iOS 5.1 application that registers to the APNS service to receive notifications. The register is successful and I receive the notifications correctly. The problem comes when I try to handle the notifications.

Once the application is running, the method didReceiveRemoteNotification in the AppDelegate is called correctly and so the notification is handled as intended. This, however, only happens when the application is running on the foreground.

However, when the application is running on the background or is simply stopped, that method is not called. I've read that you should add some lines to the method didFinishLaunchingWithOptions method to obtain the notification from the userInfo dictionary, and handle it. This works just fine, but ONLY when the application is opened by clicking on the notification at the Notification Center. This means that if you open the application by clicking on its badge, or simply by changing context if you were running it on the background, the app never realises that a notification came in. Additionally, if more than one notification was received, we can only handle one of them at once by clicking on the Notification Center, which is a pain :-)

Is there any way to read the pending notifications in the Notification Center? I know there is a way to flush them using the method cancelAllLocalNotifications but I haven't found a way to just read them. And I really need to handle all of them. I thought of implementing a communication protocol with the third-party notification server to retrieve the information again when the application comes to the foreground, but since the information is already in the operating system I would find it strange if it's impossible to access it somehow.

So, does anybody know a way to do it? Thanks in advance.

preguntado el 05 de septiembre de 12 a las 10:09

2 Respuestas

When a push notification arrives and the user clicks 'cancel', your app has no way to read that push notification again. You have to implement a separate functionality (most probably on server-side) to fetch a list of notifications sent to this device.

For example, if a chat functionality is provided in your app and you send chat messages via push notifications then you should also keep chat messages on the server. If a user clicks 'Cancel' on any push notification then that chat message will not be displayed on the iOS device. In that case when a app comes in foreground later, you make a call to the server and fetch all the past chat messages (sent via push notification).

Respondido el 05 de Septiembre de 12 a las 10:09

What's certain is that push notifications are not guaranteed to arrive. Perhaps i'm wasting my time trying to read them from the notifiaction service, because I can't also be sure that it contains all the information I want. Anyway I need to know if there's a way to do so, before discarding any solution... - Bartserk

Your are correct. As far as I know, there is no solution other than fetching from server-side. - Aziz jeque

Ok, So a possible solution would be to have another database table with the messages in with a 'read' flag and a messageID field? Which by default the read flag is NO, then when the app successfully reads this and displays, it updates the flag to YES?

And with only 256 bytes to play with, what sort of ID field length would be necessary?

Editar,

Executed this plan and its working successfully.

Respondido 22 Abr '15, 16:04

Yup, that's a good solution :) You can also relate that message to a "user" table, and this way you can build an aggregate value to check how many unread messages that user has. And, using that value, you're able to update a bullet safely which was my main concern back then when I wrote the question. As for the id, a good olde 32 bit numeric value should be enough unless you're handling huge amounts of information :) - Bartserk

Ive gone for using the apps token and a message id as a way to compare messages, this means that I can just use a short id, and the token should be enough to stop collisions. - Simón Unsworth

I've got to say that using the app's token as id is strongly discouraged in the documentation. Even though it works, more or less, the app token is more volatile than it seems in iOS. It might change, for example, with a OS update or a factory reset. You should consider generating some other kind of device-related id. - Bartserk

Understood, i will have a rethink, maybe my counter ID is fine on its own. - Simón Unsworth

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.