iOS推送通知问题 [英] iOS Push notification issue

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

问题描述

我正在做一个项目,其中推送通知功能是关键功能之一.
当我在应用程序中时它工作正常,我收到通知并处理该通知.

I am doing a project in which push notification feature is one of the key feature.
It is working fine when I am in the app, I receive notification and handle that notification.

但问题是当我在后台并且收到通知时,我在我的应用程序图标上看到了徽章当我点击该图标时,我的应用程序正在启动,但没有调用 didReceiveRemoteNotification 方法,因此我无法处理该通知.

But the issue is when I am in background and notification receives I see badge on my app icon and when I click on the icon my app is launching but the didReceiveRemoteNotification method is not called so I am unable to handle that notification.

另一个问题是有时它会在设备通知列表中显示通知消息,有时则不会.

And another issue is some times it shows the notification message in device notification list and some times it didn't .

当我通过单击通知列表项进入我的应用程序时,didReceiveRemoteNotification 调用并且我能够成功处理通知.我在 didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method

When I am entering in my app through clicking on notification list item the didReceiveRemoteNotification calls and I am successfully able to handle notification. I write following code in didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method

NSDictionary* remoteNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (remoteNotif != nil)
{
    NSLog(@"didFinishLaunchingWithOptions\nNotification recieved:\n%@",remoteNotif);

    notificationData=[[NSDictionary alloc]initWithDictionary:remoteNotif];
    [notif saveNotification:remoteNotif];
}

帮我解决这个问题.提前致谢.

Help me to resolve this . Thanks in advance.

推荐答案

来自 Apple 的 本地和推送通知编程指南

From Apple's Local And Push Notification programming guide

让我们回顾一下系统交付本地时可能出现的场景应用程序的通知或远程通知.

Handling Local and Remote Notifications

Let’s review the possible scenarios when the system delivers a local notification or a remote notification for an application.

  1. 当应用程序未在前台运行时发送通知.

在这种情况下,系统会显示通知,显示警报、标记图标、播放声音.

In this case, the system presents the notification, displaying an alert, badging an icon, perhaps playing a sound.

作为显示通知的结果,用户点击操作警报按钮或点击(或点击)应用程序图标.

As a result of the presented notification, the user taps the action button of the alert or taps (or clicks) the application icon.

如果点击操作按钮(在运行 iOS 的设备上),系统启动应用程序,应用程序调用其委托的application:didFinishLaunchingWithOptions: 方法(如果实施的);它传入通知有效负载(对于远程通知)或本地通知对象(对于本地通知).

If the action button is tapped (on a device running iOS), the system launches the application and the application calls its delegate’s application:didFinishLaunchingWithOptions: method (if implemented); it passes in the notification payload (for remote notifications) or the local-notification object (for local notifications).

如果在运行 iOS 的设备上点击应用程序图标,应用程序调用相同的方法,但不提供任何信息关于通知.如果单击应用程序图标运行 OS X 的计算机,应用程序调用委托的applicationDidFinishLaunching: 委托可以使用的方法获取远程通知负载.

If the application icon is tapped on a device running iOS, the application calls the same method, but furnishes no information about the notification . If the application icon is clicked on a computer running OS X, the application calls the delegate’s applicationDidFinishLaunching: method in which the delegate can obtain the remote-notification payload.

当应用程序在前台运行时发送通知.

应用程序调用其委托的application:didReceiveRemoteNotification: 方法(用于远程通知)或 application:didReceiveLocalNotification: 方法(对于本地通知)并传入通知负载或本地通知对象.

The application calls its delegate’s application:didReceiveRemoteNotification: method (for remote notifications) or application:didReceiveLocalNotification: method (for local notifications) and passes in the notification payload or the local-notification object.

因此,在您的情况下,当应用程序在后台运行时,当您单击通知/警报时,操作系统会将您的应用程序置于前台.所以它属于第二点.

So in your case, when application is running in background, and when you click the notification/alert, operating system brings your app into foreground. So it falls under second point.

您可以实现 application:didReceiveRemoteNotification: 方法来获取通知负载,如果操作按钮被点击.但是当按下应用程序图标而不是操作消息时,通知有效负载不会通过该方法转发.您唯一的选择是联系您的服务器并同步数据.毕竟按照 Apple 的政策,推送通知只会告诉服务器上有数据,无论哪种方式,您都需要连接到服务器并获取数据.

You can implement application:didReceiveRemoteNotification: method to get the notification payload, if action button is tapped. But when the application icon is pressed instead of action message, the notification payload is not forwarded with the method. Your only option is to contact your server and sync the data. After all as per Apple's policy, push notification only tells that there is data on server, and either way you need to connect to server and get the data.

这篇关于iOS推送通知问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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