Krutika Sonawala Krutika Sonawala - 2 months ago 16
Objective-C Question

Notification displays in notification bar even when app is open

My app is having local notifications with action buttons : snooze and dismiss. The problem is that, when my app is open I have displayed notification in popup. It displays popup and notification in notification bar also displays.
As a general scenario app does not display notification in notification bar when it is in foreground. But I can see the both.

Can not figure out what is happening here.
I have handled the notification in didReceiveLocalNotification method as below.

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


and below is my didFinishLaunching method

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


see the images below :

enter image description here

enter image description here

Answer

just do this

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

cancelLocalNotification when your app in UIApplicationStateActive.

hope it's help :)

Comments