检测用户点击本地通知

时间:2015-10-06 09:14:42

标签: ios uilocalnotification

我会定期显示我的本地通知。

UILocalNotification *notification = [[UILocalNotification alloc]init];
[notification setAlertBody:@"Test test"];
[notification setUserInfo:@{@"test": @"test"}];
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];

我需要检测到该通知,我打算在这里写一下。

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification

无论用户点击通知还是自动在前台呼叫,它总是调用该功能。

所以,我将其分开使用。

if (application.applicationState == UIApplicationStateActive)

当我显示通知中心时,它变为InActive。但是,它仍然调用didReceiveLocalNotification。我无法区分用户是否点击通知中心的通知,还是因为我的定期发布通知。

我怎么能真正知道我在didReceiveLocalNotification中点击通知(来自InActive State或Background State)?

4 个答案:

答案 0 :(得分:8)

Assuming that I understood your issue correctly, I stumbled on the same obstacle and couldn't find a super clean solution.

So the situation where the following method

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification

is called and applicationState is equal to UIApplicationStateInactive happens in two cases:

  1. the app is in the foreground and the notification has just been fired
  2. the notification has been fired some time ago, notification center is pulled down and user tapped on the notification

One way to distinguish these two cases is to check the notification's fireDate:

notification.fireDate.timeIntervalSinceNow < 0.5

If this expression is true, it's very likely that the first case happened. If the expression is false, it's very likely that the second case happened.

This solution depends on the system delivering the notification without delay and/or the user not being fast enough to click the notification in the notification center under 500ms since the notification's firing. I'm not sure how likely is it for a firing delay to happen. I guess it's possible if the device is under some kind of processing load.

I hope there is a cleaner solution, hopefully someone will share it.

答案 1 :(得分:5)

首先,请阅读Apple Documentation

  

用户点按iOS 8通知中的自定义操作按钮。在这   例如,iOS调用   application:handleActionWithIdentifier:forRemoteNotification:completionHandler:   要么   application:handleActionWithIdentifier:forLocalNotification:completionHandler:。   在这两种方法中,您都可以获得操作的标识符   确定用户点击的按钮。你也可以得到遥控器   或本地通知对象,以便您可以检索任何信息   你需要处理这个动作。

     

用户点按提醒中的默认按钮或点击(或点击)   应用程序图标如果轻触默认操作按钮(在运行的设备上)   iOS),系统启动应用程序,应用程序调用其委托   application:didFinishLaunchingWithOptions:方法,传入   通知有效载荷(用于远程通知)或   本地通知对象(用于本地通知)。虽然   application:didFinishLaunchingWithOptions:不是最适合的地方   处理通知,此时获取有效负载给你   在处理程序方法之前启动更新过程的机会   被称为。

第二,您可以通过以下方式区分是从活动状态还是非活动状态调用didReceiveLocalNotification:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    UIApplicationState appState = UIApplicationStateActive;
    if ([application respondsToSelector:@selector(applicationState)])
        appState = application.applicationState;

    if (appState == UIApplicationStateActive)
    {
    }
    else
    {
    }
}

答案 2 :(得分:1)

  • 应用:didReceiveLocalNotification:

当正在运行的应用收到本地通知时发送给代理。

检查一下:

iOS UILocalNotification - No delegate methods triggered when app is running in background and the icon is clicked upon notification

答案 3 :(得分:-4)

点击按钮时,使用KVO键值观察来了解并执行某些操作。