即使应用程序处于打开状态,通知栏中也会显示通知

时间:2016-09-13 09:33:43

标签: ios objective-c uilocalnotification

我的应用通过操作按钮进行本地通知:暂停和解除。问题是,当我的应用程序打开时,我在弹出窗口中显示了通知。它会显示弹出窗口并显示通知栏中的通知。 作为一般情况,应用程序在前台时不会在通知栏中显示通知。但我可以看到两者。

无法弄清楚这里发生了什么。 我已经在didReceiveLocalNotification方法中处理了通知,如下所示。

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

 UIApplicationState state = [application applicationState];
 if (state == UIApplicationStateActive) {

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"MyApp"
                                                    message:notification.alertBody
                                                   delegate:self cancelButtonTitle:@"OK"
                                          otherButtonTitles:@"SNOOZE",@"DISMISS",nil];
    alert.delegate = self;
    [alert show];
 }
}

以下是我的didFinishLaunching方法

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

 if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
    UIMutableUserNotificationAction *action1;
    action1 = [[UIMutableUserNotificationAction alloc] init];
    [action1 setActivationMode:UIUserNotificationActivationModeForeground];
    [action1 setTitle:@"SNOOZE"];
    [action1 setIdentifier:kNotificationActionSnooze];
    [action1 setDestructive:NO];
    [action1 setAuthenticationRequired:NO];

    UIMutableUserNotificationAction *action2;
    action2 = [[UIMutableUserNotificationAction alloc] init];
    [action2 setActivationMode:UIUserNotificationActivationModeBackground];
    [action2 setTitle:@"DISMISS"];
    [action2 setIdentifier:kNotificationActionDismiss];
    [action2 setDestructive:NO];
    [action2 setAuthenticationRequired:NO];

    UIMutableUserNotificationCategory *actionCategory;
    actionCategory = [[UIMutableUserNotificationCategory alloc] init];
    [actionCategory setIdentifier:kNotificationCategoryIdent];
    [actionCategory setActions:@[action1, action2] forContext:UIUserNotificationActionContextDefault];

    NSSet *categories = [NSSet setWithObject:actionCategory];
    UIUserNotificationType types = (UIUserNotificationTypeAlert| UIUserNotificationTypeSound| UIUserNotificationTypeBadge);

    UIUserNotificationSettings *settings;
    settings = [UIUserNotificationSettings settingsForTypes:types categories:categories];

    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];

   }

 return YES;
}

见下图:

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:2)

就这样做

(arr[:,:,0] / 255).astype(int)
当您的应用程序处于UIApplicationStateActive时,

cancelLocalNotification。

希望它有所帮助:)