如果应用程序已经在后台运行,如何响应推送通知视图 [英] How to respond to push notification view if app is already running in the background

查看:26
本文介绍了如果应用程序已经在后台运行,如何响应推送通知视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做一些相当简单的事情.我将自定义数据附加到我处理的一些推送通知

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

我寻找 UIApplicationLaunchOptionsRemoteNotificationKey,嘿,它就在那里.

I look for the UIApplicationLaunchOptionsRemoteNotificationKey and hey presto there it is.

只有在我的应用第一次启动时才会调用该方法.如果我的应用程序在收到通知并且用户按下通知上的查看"按钮时已经在后台运行,我该如何读取相同的密钥?我想将它们发送到一个特定的视图控制器,并在其上打开数据,就像我第一次从通知启动应用程序一样.

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.

推荐答案

在 iOS 7 及更高版本中查看 application:didReceiveRemoteNotification:fetchCompletionHandler:.

Check out application:didReceiveRemoteNotification:fetchCompletionHandler: in iOS 7 and later.

如果您的应用程序在前台运行,则调用方法 application:didReceiveRemoteNotification:.如果您的应用在后台运行并且用户与您的推送通知互动(从而使您的应用处于活动状态),它也会被调用.

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

因此,真正的问题是如何确定应用是在前台还是通过用户与您的推送通知互动而使其处于活动状态.

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.

对于问题​​didReceiveRemoteNotification when in background 有钥匙:

您可以使用以下代码在 application:didReceiveRemoteNotification: 中判断您的应用是否刚刚进入前台:

You can tell whether your app was just brought to the foreground or not in application:didReceiveRemoteNotification: using this bit of code:

- (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
    ...
}

这篇关于如果应用程序已经在后台运行,如何响应推送通知视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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