3

App在前台运行时如何忽略远程通知,但点击通知栏上的通知启动应用时响应?</p>

4

2 回答 2

2

当应用程序处于前台时,通知栏中不会出现通知。通知有效负载被传递给该application:didReceiveRemoteNotification:方法,如果这是您想要的,您可以在其中忽略它。

当app在后台运行时,通知到来时,当你打开app时,application:didReceiveRemoteNotification:也会被调用。您可以使用以下代码区分这两种情况:

-(void)application:(UIApplication *)app didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    if([app applicationState] == UIApplicationStateInactive)
    {
        //application was running in the background
    }
}

当您通过点击通知打开应用程序时,通知有效负载将传递到另一个名为 的方法,application:didFinishLaunchingWithOptions:您可以在其中处理它。

于 2013-08-02T10:25:42.640 回答
0

我更喜欢这个组合。如果没有我在 中添加的功能didFinishLaunchingWithOptions,通知将不会通过didReceiveRemoteNotification应用程序首次从通知点击进入内存时包含的逻辑进行深度链接。

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

    // All your nice startup code

    // ...

    // Hook for notifications
    if (launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]) {
        [self application:application didReceiveRemoteNotification:launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]];
    }
}

顺便说一句,这两个都在您的 AppDelegate 中。

- (void)application:(UIApplication *)application didReceiveRemoteNotification: (NSDictionary *)userInfo {

    if (application.applicationState == UIApplicationStateActive) {
        return;
    }

    // Do anything you want with the notification, such as deep linking

    // ...
}
于 2015-02-24T18:24:08.277 回答