ios 如果应用程序已经在后台运行,如何响应推送通知视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5099483/
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
How to respond to push notification view if app is already running in the background
提问by rustyshelf
I have something fairly simple I want to do. I attach a custom piece of data to some push notifications that I handle in
我有一些我想做的相当简单的事情。我将自定义数据附加到我处理的一些推送通知
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
I look for the UIApplicationLaunchOptionsRemoteNotificationKeyand hey presto there it is.
我寻找UIApplicationLaunchOptionsRemoteNotificationKey,嘿,它就在那里。
That method only gets called if my app is being launched for the first time. How do I read that same key if my application is running in the background already when the notification comes in and the user pressed the 'View' button on the notification? I want to send them to a particular view controller with that data open on it, the same as I do if the app is being launched for the first time from the notification.
只有在我的应用程序第一次启动时才会调用该方法。如果我的应用程序在收到通知并且用户按下通知上的“查看”按钮时已经在后台运行,我该如何读取相同的密钥?我想将它们发送到一个特定的视图控制器,并在其上打开数据,就像我第一次从通知启动应用程序一样。
回答by gerry3
Check out application:didReceiveRemoteNotification:fetchCompletionHandler:
in iOS 7 and later.
application:didReceiveRemoteNotification:fetchCompletionHandler:
在 iOS 7 及更高版本中查看。
The method application:didReceiveRemoteNotification:
is called if your app is running in the foreground. It also is called if your app is running in the background and the user engages with your push notification (thus making your app active).
application:didReceiveRemoteNotification:
如果您的应用程序在前台运行,则会调用该方法。如果您的应用程序在后台运行并且用户与您的推送通知互动(从而使您的应用程序处于活动状态),它也会被调用。
So, the real question is how to determine if the app was in the foreground or if it was made active by the user engaging with your push notification.
因此,真正的问题是如何确定应用程序是在前台还是由用户与您的推送通知互动而使其处于活动状态。
It looks like this answerto the question didReceiveRemoteNotification when in backgroundhas the key:
它看起来像这样的回答这个问题didReceiveRemoteNotification当后台有钥匙:
You can tell whether your app was just brought to the foreground or not in application:didReceiveRemoteNotification:
using this bit of code:
您可以application:didReceiveRemoteNotification:
使用以下代码判断您的应用程序是否刚刚被带到前台:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
if ( application.applicationState == UIApplicationStateActive )
// app was already in the foreground
else
// app was just brought from background to foreground
...
}
回答by user523234
To detect if the app was activated by remote notication, try this:
要检测应用程序是否被远程通知激活,请尝试以下操作:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (userInfo == NULL)
{
NSLog(@"didFinishLaunchingWithOptions user startup userinfo: %@", userInfo);
}
else
{
NSLog(@"didFinishLaunchingWithOptions notification startup userinfo: %@", userInfo);
}
}