How to show local notification when app is foreground
Asked Answered
C

2

4

How to show local notification always, even in foreground, using new (from iOS 10) notification framework UserNotifications https://developer.apple.com/reference/usernotifications?language=objc ?

Crowned answered 6/10, 2016 at 10:14 Comment(1)
add your language tagPrevailing
H
7
#import <UserNotifications/UserNotifications.h>
#define SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

//interface
 @interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate>

@end

//implementation
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
    [self registerForRemoteNotifications];
    return YES;
}

- (void)registerForRemoteNotifications {
    if(SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(@"10.0")){
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        center.delegate = self;
        [center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){
             if(!error){
                 [[UIApplication sharedApplication] registerForRemoteNotifications];
             }
         }];  
    }
    else {
        // Code for old versions
    }
}

////...........Handling delegate methods for UserNotifications........
//Called when a notification is delivered to a foreground app.

-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
    NSLog(@"User Info : %@",notification.request.content.userInfo);
    completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge);
}

//Called to let your app know which action was selected by the user for a given notification.
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{
    NSLog(@"User Info : %@",response.notification.request.content.userInfo);
    completionHandler();
}

May this will help you. :)

Hautboy answered 6/10, 2016 at 11:36 Comment(4)
Sommm, where did you set parameter for showing notification in foreground? Because it looks like I did the same, but notifications show only in background mode. Shows meens appearing in notifications bar.Crowned
Have you used this solution before?Crowned
It shows notifications when app is in active state?Crowned
The key is in implementing willPresentNotification and calling the completion handler with a value other than "None". See UNUserNotificationCenterDelegate DocsDrape
H
-1
 You can do this way.  

//in Appdelegate
-(void)application:(UIApplication *)application didReceiveLocalNotification(UILocalNotification *)notification
{
    dispatch_async(dispatch_get_main_queue(),^{

    NSLog(@"alertbody ..%@",  notification.alertBody);
    NSLog(@"title ..%@",  notification.alertTitle);
    NSLog(@"action ..%@",  notification.alertAction);
    NSLog(@"userinfo..%@",  notification.userInfo);
    [[NSNotificationCenter defaultCenter]postNotificationName:@"NotificationReceived" object:nil userInfo:notification.userInfo];
          });
}


//in present view controller
-(void)viewDidLoad
{
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(notificationReceivedMethod:) name:@"NotificationReceived" object:nil];
}

-(void) viewWillDisappear:(BOOL)animated {
{
 [[NSNotificationCenter defaultCenter]removeObserver:self name:@"NotificationReceived" object:nil];
}

-(void)notificationReceivedMethod
{
//create you custom popup or view
}
Hautboy answered 6/10, 2016 at 11:14 Comment(1)
No-no-no! I asked about UserNotification framework from iOS 10Crowned

© 2022 - 2024 — McMap. All rights reserved.