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

发布于 2024-10-19 05:38:24 字数 394 浏览 5 评论 0原文

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

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

我查找 UIApplicationLaunchOptionsRemoteNotificationKey ,嘿,它就在那里。

仅当我的应用程序首次启动时才会调用该方法。如果当通知到来并且用户按下通知上的“查看”按钮时我的应用程序已经在后台运行,我如何读取相同的键?我想将它们发送到一个特定的视图控制器,并在其上打开该数据,就像我第一次从通知启动应用程序时所做的那样。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

关于从前 2024-10-26 05:38:25

要检测应用程序是否是通过远程通知激活的,请尝试以下操作:

- (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);
    }
}

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);
    }
}
泛泛之交 2024-10-26 05:38:24

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


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

因此,真正的问题是如何确定应用程序是否位于前台,或者是否由用户参与推送通知而使其处于活动状态。

它看起来像问题的这个答案​​在后台时的didReceiveRemoteNotification有关键:

您可以判断您的应用程序是否刚刚在应用程序中进入前台: 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
    ...
}

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


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.

It looks like this answer to the question didReceiveRemoteNotification when in background has the key:

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
    ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文