DrumPower3004
DrumPower3004

Reputation: 75

Send UILocalNotification to main Notifications area instead of Alert

I'm developing an iOS App which makes a local notification as an Alert on screen inside the app. I want it to send the notification to the main Notifications area and not alert anything on screen instead. How can I do this?

At the moment the notification area just says "No Notifications".

Here's my code:

AppDelegate

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    UIApplicationState state = [application applicationState];
    if (state == UIApplicationStateActive) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Reminder"
                                                        message:notification.alertBody
                                                       delegate:self cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
    }
        // Set icon badge number to zero
    application.applicationIconBadgeNumber = 0;
}

ViewController

self.itemText.text = @"Notification";
NSDate *pickerDate = [[NSDate alloc] initWithTimeIntervalSinceNow:5];
NSLog(@"Picked date is %@", pickerDate);

NSDate *todaysDate;
todaysDate = [NSDate date];
NSLog(@"Todays date is %@", todaysDate);


// Schedule the notification
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = pickerDate;
// TO DO : Assign proper text to self.itemText.text based on real data
localNotification.alertBody = self.itemText.text;
localNotification.alertAction = @"Another";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

Upvotes: 0

Views: 238

Answers (1)

Sander
Sander

Reputation: 1375

UILocalNotification will be added to the main Notifications area only if application is in background or terminated. In the foreground it will call your method

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

The only way you can get what you want, is to use UserNotifications.framework (since iOS 10).

Introduction to User Notifications Framework in iOS 10 is just the first tutorial

Here is the Apple Documentation on Local Notifications

And using it, don't forget to implement method of UNUserNotificationCenterDelegate to get Native notification even if your application is in foreground.

- (void)userNotificationCenter:(UNUserNotificationCenter *)center 
   willPresentNotification:(UNNotification *)notification 
     withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler;

Hope it helps!

Upvotes: 0

Related Questions