即使应用程序打开,通知也会显示在通知栏中
Notification displays in notification bar even when app is open
我的应用有带有操作按钮的本地通知:暂停和关闭。问题是,当我的应用程序打开时,我在弹出窗口中显示了通知。它显示弹出窗口,通知栏中的通知也显示。
作为一般场景,应用程序在前台时不会在通知栏中显示通知。但我可以看到两者。
无法弄清楚这里发生了什么。
我已经在 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;
}
见下图:
就这样做
- (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];
[[UIApplication sharedApplication] cancelLocalNotification:notification];
}
}
当您的应用处于 UIApplicationStateActive 时取消 LocalNotification。
希望对您有所帮助:)
我的应用有带有操作按钮的本地通知:暂停和关闭。问题是,当我的应用程序打开时,我在弹出窗口中显示了通知。它显示弹出窗口,通知栏中的通知也显示。 作为一般场景,应用程序在前台时不会在通知栏中显示通知。但我可以看到两者。
无法弄清楚这里发生了什么。 我已经在 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;
}
见下图:
就这样做
- (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];
[[UIApplication sharedApplication] cancelLocalNotification:notification];
}
}
当您的应用处于 UIApplicationStateActive 时取消 LocalNotification。
希望对您有所帮助:)