Home:ALL Converter>iOS 10, Local notification show when app is in foreground?

iOS 10, Local notification show when app is in foreground?

Ask Time:2016-10-14T20:56:54         Author:Nik

Json Formatter

I want to show notification when app is in forground. Below is the code I did for new usernotificationdelegate method.

In app delegate :

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) {

        //iOS 10 handling
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        center.delegate = self;
        [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
          if (!error) {
                 NSLog(@"request authorization succeeded!");
          } }];
     } 
}

#pragma mark - User notification delegate
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {

    NSLog(@"willPresentNotification");
    NSLog(@"%@", notification.request.content.userInfo);
    completionHandler (UNNotificationPresentationOptionAlert);
}

And this is my method to trigger local notification

-(void) fireLocalNotification:(NSString *) message
{

    NSLog(@"fire Local Notification");
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) {

        //Notification Content
        UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
        content.body =  [NSString stringWithFormat:@"%@",message];
        content.sound = [UNNotificationSound defaultSound];

        //Set Badge Number
        content.badge = @([[UIApplication sharedApplication] applicationIconBadgeNumber] + 1);

        // Deliver the notification in five seconds.
        UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger
                                                      triggerWithTimeInterval:1.0f repeats:NO];

        //Notification Request
        UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"LocalNotification" content:content trigger:trigger];

        //schedule localNotification
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        center.delegate = self;
        [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
            if (!error) {
                NSLog(@"add Notification Request succeeded!");
            }
        }];

    }
 }

now after doing this still I am not getting notification in forground.

thanks in advance.

Author:Nik,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/40043769/ios-10-local-notification-show-when-app-is-in-foreground
mital solanki :

Please implement this function :\n\n-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler\n{\n completionHandler(UNNotificationPresentationOptionAlert + UNNotificationPresentationOptionSound);\n}\n",
2016-10-17T04:08:22
neena-systematix :

For getting notification when the app is in foreground you need to implement following delegate method of UNUserNotificationCenterDelegate in iOS10.\n\nfunc userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {\n\n completionHandler([.alert,.badge])\n}\n\n\nFor more information you can refer the apple doc. Here is the link:\n\nhttps://developer.apple.com/documentation/usernotifications/unusernotificationcenterdelegate?language=objc",
2017-06-15T11:16:24
Ajjjjjjjj :

please see this code to handle local notification on foreground.\n/*****************************/\n\n UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];\n\n UNAuthorizationOptions options = UNAuthorizationOptionAlert + UNAuthorizationOptionSound;\n\n [center requestAuthorizationWithOptions:options\n completionHandler:^(BOOL granted, NSError * _Nullable error) {\n if (!granted) {\n //NSLog(@\"Something went wrong\");\n }\n }];\n\n int dayCounter =5;\n\n int minute = 48;\n\n\n {\n NSDateComponents *components = [[NSDateComponents alloc] init];\n components.weekday = dayCounter;\n\n\n dayCounter++;\n\n components.hour = 12;\n components.minute = minute;\n\n UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:YES];\n\n UNMutableNotificationContent *objNotificationContent = [[UNMutableNotificationContent alloc] init];\n objNotificationContent.title = [NSString localizedUserNotificationStringForKey:@\"Notification!\" arguments:nil];\n\n objNotificationContent.body = [NSString localizedUserNotificationStringForKey:@\"We made a surprise Edit for You\" arguments:nil];\n\n objNotificationContent.sound = [UNNotificationSound defaultSound];\n\n objNotificationContent.badge = @([[UIApplication sharedApplication] applicationIconBadgeNumber] + 1);\n\n\n UNNotificationAttachment *attachment = nil;\n\n NSURL* outputURL = [[NSURL alloc] initFileURLWithPath:filePath];\n NSError *attachmentError = nil;\n\n attachment = [UNNotificationAttachment attachmentWithIdentifier:@\"image\"\n URL: outputURL\n options:nil\n error:&attachmentError];\n if (attachmentError) {\n\n return;\n }\n\n objNotificationContent.attachments=@[attachment];\n\n NSString *identifier = @\"UYLLocalNotification\";\n UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier\n content:objNotificationContent trigger: trigger];\n\n [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {\n if (error != nil) {\n NSLog(@\"Something went wrong: %@\",error);\n }\n else\n {\n }\n }];\n\n\n/****************************/\n\n// to handle\n -(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{\n\n UIApplicationState state = [application applicationState];\n if (state == UIApplicationStateActive) {\n\n dispatch_async(dispatch_get_main_queue(), ^{\n UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@\"Reminder\" message:notification.alertBody delegate:self cancelButtonTitle:@\"Cancel\" otherButtonTitles:@\"Yes\", nil];\n alert.tag = 330;\n [alert show];\n });\n }\nelse{\n // handle for background\n}\n",
2017-03-22T10:48:59
Stan :

For displaying banner message while app is in foreground, use the following method.\n\nAdd it to your delegate methods in the AppDelegate and conform to this protocol - UNUserNotificationCenterDelegate\n\niOS 10, Swift 3:\n\n@available(iOS 10.0, *)\nfunc userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {\n completionHandler(UNNotificationPresentationOptions.alert)\n}\n",
2017-07-07T16:25:22
yy