如何在 iOS 中处理未读推送通知? [英] How can I handle unread push notifications in iOS?

查看:36
本文介绍了如何在 iOS 中处理未读推送通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 iOS 5.1 应用程序,它注册到 APNS 服务以接收通知.注册成功,我正确收到通知.当我尝试处理通知时,问题就出现了.

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.

应用程序运行后,AppDelegate 中的方法 didReceiveRemoteNotification 被正确调用,因此通知按预期处理.但是,这仅在应用程序在前台运行时发生.

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.

但是,当应用程序在后台运行或只是停止时,不会调用该方法.我读到您应该在方法 didFinishLaunchingWithOptions 方法中添加一些行以从 userInfo 字典中获取通知并处理它.这工作得很好,但仅当通过单击通知中心的通知打开应用程序时.这意味着,如果您通过单击其徽章打开应用程序,或者如果您在后台运行它,只需更改上下文,该应用程序永远不会意识到收到了通知.此外,如果收到了多个通知,我们可以单击通知中心一次只能处理其中一个,这很痛苦:-)

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 :-)

有没有办法在通知中心读取待处理的通知?我知道有一种方法可以使用 cancelAllLocalNotifications 方法来刷新它们,但我还没有找到一种方法来读取它们.我真的需要处理所有这些.我想过与第三方通知服务器实现通信协议,以便在应用程序进入前台时再次检索信息,但由于信息已经在操作系统中,如果无法以某种方式访问​​它,我会觉得很奇怪.

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.

推荐答案

当推送通知到达并且用户单击取消"时,您的应用程序无法再次读取该推送通知.您必须实现一个单独的功能(很可能在服务器端)来获取发送到此设备的通知列表.

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.

例如,如果您的应用提供了聊天功能,并且您通过推送通知发送聊天消息,那么您还应该在服务器上保留聊天消息.如果用户在任何推送通知上单击取消",则该聊天消息将不会显示在 iOS 设备上.在这种情况下,当应用稍后进入前台时,您可以调用服务器并获取所有过去的聊天消息(通过推送通知发送).

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).

这篇关于如何在 iOS 中处理未读推送通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆