0

this following step i do in my app

in my appdelegate

#import <UserNotifications/UserNotifications.h>


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.


    UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
      center.delegate = (id)self;

    [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound)
                          completionHandler:^(BOOL granted, NSError * _Nullable error) {


                              // Enable or disable features based on authorization.
                          }];







    return YES;
}

and in my viewcontroller the code for generate local notification

-(IBAction)btnclicked:(id)sender 
{
    UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
    content.title = @"gg";
     content.subtitle = @"ggg";
    content.body = @"hhh";
    content.sound = [UNNotificationSound defaultSound];

    // Deliver the notification in five seconds.
    UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger
                                                  triggerWithTimeInterval:5 repeats:NO];
    UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:@"2"
                                                                          content:content trigger:trigger
                                      ];

    // Schedule the notification.
    UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
    [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
        NSLog(@"hii");
    }];





}

Now When my app in foreground Notification not generating.

in ios 9 i use

 UILocalNotification* localNotification = [[UILocalNotification alloc] init];
    localNotification.soundName = @"Default";
    localNotification.alertBody = @"my notification;)";
    localNotification.fireDate = [NSDate date];
    localNotification.category = @"Email";
    localNotification.repeatInterval = kCFCalendarUnitDay;
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

this code work in ios 9 but in ios 10 UILocalNotification is deprecate.

so how to generate local notification in ios 10 when your app in foreground ?

balkaran singh
  • 2,754
  • 1
  • 17
  • 32

2 Answers2

0

Try to use it with UNUserNotificationCenterDelegate in iOS10

iOS 10 adds the UNUserNotificationCenterDelegate protocol for handling notifications while your app is in the foreground.

Hope this will help

Community
  • 1
  • 1
Arnab
  • 4,216
  • 2
  • 28
  • 50
  • i am already using User Notifications framework . my question is how to generate local notification in ios 10 when your app in foreground ? – balkaran singh Dec 14 '16 at 09:05
-1

For displaying banner message while app is in foreground, use the following method.

iOS 10, Swift 3 :

// This method will be called when app received push notifications in foreground

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) 
{
    completionHandler(
       [UNNotificationPresentationOptions.alert,
        UNNotificationPresentationOptions.sound,
        UNNotificationPresentationOptions.badge])
}
Mingebag
  • 748
  • 2
  • 20
  • 35