lucgian841
lucgian841

Reputation: 2002

Multiple notification on iOS app

In my app I need to send a first notification (when app is started) and a second notification when user press a button. To send the first notification I implemented the following code:

- (void)viewDidAppear:(BOOL)animated {

    UILocalNotification *localNotification = [[UILocalNotification alloc]init];
    localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
    localNotification.alertBody = @"My notification text";
    localNotification.alertAction = @"text";
    localNotification.soundName = UILocalNotificationDefaultSoundName;
    localNotification.timeZone = [NSTimeZone localTimeZone];
    localNotification.userInfo = @{@"ID": @"Geo"};
    localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}

For the second notification I implemented the following code:

- (IBAction)buttonDone:(UIButton *)sender {
    [self.buttonDone setEnabled:NO];
    UILocalNotification *localNotification = [[UILocalNotification alloc]init];
    localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
    localNotification.alertBody = @"My notification text";
    localNotification.alertAction = @"text";
    localNotification.soundName = UILocalNotificationDefaultSoundName;
    localNotification.timeZone = [NSTimeZone localTimeZone];
    localNotification.userInfo = @{@"ID": @"Pay"};
    localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}

Now when I run the app on the simulator I've the following issue: the app is started and it send me (after 5 second) the first notification, but I don't see banner on the status bar and when I go to the Notification Center I can't see the notification that I received, but the app open the view controller that should be opened after pressing on notification. When I press the button for the second notification all works fine. In my AppDelegate.m I inserted the following code:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    [[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]];

    UILocalNotification *localNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
    if (localNotification) {
        application.applicationIconBadgeNumber = 0;
    }
    application.applicationIconBadgeNumber = 0;

    return YES;
}

and I inserted the following method to respond at the notification:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    UIApplicationState state = [application applicationState];

     NSString *ident = [notification.userInfo objectForKey:@"ID"];

    if (state == UIApplicationStateActive) {
        [[UIApplication sharedApplication] cancelAllLocalNotifications];
        if ([ident isEqualToString:@"Geo"]) {
            ViewController *notificationVC = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"Local"];
            [self.window setRootViewController:notificationVC];
        } else if ([ident isEqualToString:@"Pay"]) {
            ViewController *notificationVC = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"Notification"];
            [self.window setRootViewController:notificationVC];
        }
    }

    [[UIApplication sharedApplication] cancelAllLocalNotifications];

    if ([ident isEqualToString:@"Geo"]) {
        ViewController *notificationVC = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"Local"];
        [self.window setRootViewController:notificationVC];
    } else if ([ident isEqualToString:@"Pay"]) {
        ViewController *notificationVC = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"Notification"];
        [self.window setRootViewController:notificationVC];
    }

    application.applicationIconBadgeNumber = 0;

}

I set the Storyboard ID like the name I put in the code. What's wrong with my code? Why it doesn't shows the notification banner and it doesn't shows the notification in Notification Center? I hope you can help me to fix this issue.

Upvotes: 0

Views: 389

Answers (1)

E-Riddie
E-Riddie

Reputation: 14780

Details

If you are on Foreground you have to handle the message on your own code. If you are on Background then iOS SDK manages it for you by showing the notification banner.

Solution

You can handle your notifications on didReceiveLocalNotification under appDelegate on your own.

If you are interested on implementing something similar to notification banner of iOS SDK here you have it on GitHub AGPushNote:

enter image description here

Upvotes: 1

Related Questions