且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如果应用程序已经在后台运行,如何响应推送通知视图

更新时间:2023-02-26 20:57:03

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