1

How can i show local notifications in iOS 10 and 11 running devices instantly, my app requirement is to show local notifications to user instantly, as we need to request local notification first and then system scheduled the notification.I am running this code for showing local notification.

UNMutableNotificationContent *content = [UNMutableNotificationContent new];
content.title = title;
content.body = body;
content.sound = [UNNotificationSound defaultSound];

UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:1 repeats:NO];

UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];
/// 3. schedule localNotification
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
    if (!error) {
        NSLog(@"Local Notification succeeded");
    }
    else {
        NSLog(@"Local Notification failed");
    }
}];
juanjo
  • 3,737
  • 3
  • 39
  • 44
Pias
  • 218
  • 1
  • 2
  • 13
  • see this https://stackoverflow.com/questions/39941778/how-to-schedule-a-local-notification-in-ios-10-objective-c – Saurabh Jain Oct 02 '17 at 06:07
  • @Saurabh my implementation is also same just they have used calendar trigger and i have used time interval trigger, my question is i want to show notification instantly but my above code is not showing it. – Pias Oct 02 '17 at 06:33
  • If you want to display notifications while your app is active, you're out of luck. These notifications are meant to notify the user about action while the app is in background. – Julian F. Weinert Oct 02 '17 at 21:18
  • @JulianF.Weinert As per the new UNUserNotifications we can see the notifications when the app is active by using the `willPresent` delegate. See this link https://stackoverflow.com/a/39715402/2545465 – Rajan Maheshwari Oct 04 '17 at 06:45
  • refer this useful link with very nice example. https://github.com/arasu01/Local-Notification-Example-iOS1 – Pramod More Mar 29 '18 at 12:37
  • @PramodMore thank you – Pias Mar 29 '18 at 12:50

1 Answers1

1

You can try this ....

        [[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        UNAuthorizationOptions options = UNAuthorizationOptionAlert + UNAuthorizationOptionSound;

        // Request using shared Notification Center
        [center requestAuthorizationWithOptions:options
                              completionHandler:^(BOOL granted, NSError * _Nullable error) {
                                  if (granted) {
                                      NSLog(@"Notification Granted");
                                  }
                              }];

        // Notification authorization settings
        [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
            if (settings.authorizationStatus == UNAuthorizationStatusAuthorized) {
                NSLog(@"Notification allowed");
            }
        }];

        UNMutableNotificationContent *content = [UNMutableNotificationContent new];
        content.title = title;
        content.body = body;
        content.sound = [UNNotificationSound defaultSound];
        NSDate *date = [NSDate date];

        // Trigger with date
        NSDateComponents *triggerDate = [[NSCalendar currentCalendar]
                                         components:NSCalendarUnitYear +
                                         NSCalendarUnitMonth + NSCalendarUnitDay +
                                         NSCalendarUnitHour + NSCalendarUnitMinute +
                                         NSCalendarUnitSecond fromDate:date];
        UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:triggerDate
                                                                                                          repeats:NO];

        // Scheduling the notification
        UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"Identifier" content:content trigger:trigger];

        [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
            if (error != nil) {
                NSLog(@"Something went wrong: %@",error);
            }
        }];
Jana iOS
  • 13
  • 6