ios 推送通知处理
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10131856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
push notification handling
提问by Joey
I'm reading Apple's docs on
我正在阅读 Apple 的文档
Handling Local and Remote Notifications
and it looks to me to have conflicting statements. Can someone clear up these confusion points? Let's speak strictly of remote notification (versus local) for now.
在我看来,有相互矛盾的陈述。有人可以清除这些混淆点吗?现在让我们严格谈谈远程通知(相对于本地)。
The docs say that if the action button on the notification is pressed, it calls application:didFinishLaunchingWithOptions and passes in the notification payload. Later it says if the app is running in the foreground, it delivers the notification via application:didReceiveRemoteNotification:. This implies to me that when the app is backgrounded or not running, then application:didFinishLaunchingWithOptions is called. Otherwise, application:didReceiveRemoteNotification: is called.
文档说,如果按下通知上的操作按钮,它会调用 application:didFinishLaunchingWithOptions 并传入通知负载。后来它说如果应用程序在前台运行,它会通过 application:didReceiveRemoteNotification: 传递通知。这对我来说意味着当应用程序处于后台或未运行时,将调用 application:didFinishLaunchingWithOptions。否则,将调用 application:didReceiveRemoteNotification:。
Later, there is an iOS Note saying the following:
后来,有一个iOS Note说如下:
"iOS Note: In iOS, you can determine whether an application is launched as a result of the user tapping the action button or whether the notification was delivered to the already-running application by examining the application state. In the delegate's implementation of the application:didReceiveRemoteNotification: or application:didReceiveLocalNotification: method, get the value of the applicationState property and evaluate it. If the value is UIApplicationStateInactive, the user tapped the action button; if the value is UIApplicationStateActive, the application was frontmost when it received the notification."
“iOS 注意:在 iOS 中,您可以通过检查应用程序状态来确定应用程序是否因用户点击操作按钮而启动,或者通知是否已发送到已运行的应用程序。在应用程序的委托实现中:didReceiveRemoteNotification: 或 application:didReceiveLocalNotification: 方法,获取 applicationState 属性的值并对其进行评估。如果值为 UIApplicationStateInactive,则用户点击了操作按钮;如果值为 UIApplicationStateActive,则应用程序在收到通知时位于最前面。 ”
This implies to me that application:didReceiveRemoteNotification: is called both when the app is already foregrounded and if the user presses the action button (or slides the action slider in iOS 5) to foreground/launch the app.
这对我来说意味着 application:didReceiveRemoteNotification: 在应用程序已经处于前台和用户按下操作按钮(或在 iOS 5 中滑动操作滑块)以前台/启动应用程序时调用。
The source of my confusion might be with the first portion where the docs imply the notification payload is sent with the application:didFinishLaunchingWithOptions: method or with a misunderstanding of what a "running" application is (is a backgrounded app considered "running"?). The documentation for application:didReceiveRemoteNotification: states it is called for "running" applications.
我的困惑的根源可能在于文档暗示通知有效负载随应用程序一起发送的第一部分:didFinishLaunchingWithOptions: 方法或对什么是“正在运行”的应用程序的误解(后台应用程序是否被视为“正在运行”?) . application:didReceiveRemoteNotification: 的文档说明它被称为“运行”应用程序。
So, to summarize, could I get clarification on:
所以,总而言之,我可以澄清一下:
1) Is application:didReceiveRemoteNotification: always called when the app is foregrounded or when the user selects to "act" on the notification? If not, how do we make sense of the iOS Note on determining the Application State being active or inactive?
1) application:didReceiveRemoteNotification: 是否总是在应用程序处于前台或用户选择对通知“采取行动”时调用?如果不是,我们如何理解 iOS Note 关于确定应用程序状态是活动的还是非活动的?
2) Is a backgrounded app "running", at least in the sense of the docs claiming application:didReceiveRemoteNotification is called for running apps?
2) 后台应用程序是否“正在运行”,至少在文档声称 application:didReceiveRemoteNotification 被调用以运行应用程序的意义上?
3) For completion, is a backgrounded app UIApplicationStateInactive or Active?
3) 为了完成,后台应用程序 UIApplicationStateInactive 还是 Active?
回答by ch3rryc0ke
The wording here is confusing, especially around the word backgrounding.
这里的措辞令人困惑,尤其是围绕“背景”一词。
When the application is truly not loaded in memory (e,g. when you launch it the splash screen shows up etc), then application:didFinishLaunchingWithOptions is called, and you can get the push notification as follows:
当应用程序确实没有加载到内存中时(例如,当您启动它时,会显示启动画面等),然后 application:didFinishLaunchingWithOptions 被调用,您可以获得如下推送通知:
NSDictionary *remoteNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if(remoteNotif)
{
//Handle remote notification
}
If the app is loaded in memory and is ACTIVE (e.g. the app is currently open on the device) then only application:didReceiveRemoteNotification:
is called.
如果应用程序加载到内存中并且处于活动状态(例如应用程序当前在设备上打开),则 onlyapplication:didReceiveRemoteNotification:
被调用。
If the app is loaded in memory but is not ACTIVE and NOT BACKGROUNDING (e.g., you launched the app, then pressed the home button, and waited 10 seconds), and then you click the action button on a push notification, only didReceiveRemoteNotification is called.
如果应用程序已加载到内存中但未处于活动状态且未在后台运行(例如,您启动应用程序,然后按下主页按钮,并等待 10 秒),然后您单击推送通知上的操作按钮,则只会调用 didReceiveRemoteNotification .
You can capture this case as follows:
您可以按如下方式捕获此案例:
-(void)application:(UIApplication *)app didReceiveRemoteNotification:(NSDictionary *)userInfo
{
if([app applicationState] == UIApplicationStateInactive)
{
//If the application state was inactive, this means the user pressed an action button
// from a notification.
//Handle notification
}
}
回答by Gautam Sareriya
As per iOS 9.1 scenario I have tested push notification in Kill mode where my application is not running in any mode at that time if I tap on push notification than the system will call first,
根据 iOS 9.1 场景,我在 Kill 模式下测试了推送通知,如果我点击推送通知而不是系统将首先调用,我的应用程序当时不会在任何模式下运行,
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
//your code execution will here.
}
And second method call will be,
第二个方法调用将是,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//Your initial code execution.
}
This scenario I have tested in my application.
我在我的应用程序中测试了这个场景。