简体   繁体   中英

iOS app shows notifications when in background but not foreground iphone 4s & os version 9.3

App is receiving remote notifications when not running state, background state and also get notification in foreground state using local intercept concept(fire local notification when get remote notification). But When app is in foreground and iOS version is 9.3 it does not receives remote notifications.

my code is here:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
        {

        NSLog(@"Called");
        NSLog(@"Response - > %@",userInfo);
        UILocalNotification* localNotification = [[UILocalNotification alloc] init];
        localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:1];
        localNotification.alertBody = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"];
        localNotification.timeZone = [NSTimeZone defaultTimeZone];
        localNotification.applicationIconBadgeNumber = 0;
    }

    - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification  withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
    {
        NSLog( @"Handle push from foreground" );
        // custom code to handle push while app is in the foreground
        NSLog(@"%@", notification.request.content.userInfo);
        completionHandler(UNNotificationPresentationOptionAlert);
    }

    - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler
    {
        NSLog( @"Handle push from background or closed" );
        // if you set a member variable in didReceiveRemoteNotification, you  will know if this is from closed or background
        NSLog(@"%@", response.notification.request.content.userInfo);
        NSInteger countNoti = [[[response.notification.request.content.userInfo valueForKey:@"aps"] valueForKey:@"badge"] integerValue];


        if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateActive ) {

            UILocalNotification* localNotification = [[UILocalNotification alloc] init];
            localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:1];
            localNotification.alertBody = [[response.notification.request.content.userInfo valueForKey:@"aps"] valueForKey:@"alert"];
            localNotification.timeZone = [NSTimeZone defaultTimeZone];
            localNotification.applicationIconBadgeNumber = countNoti;

            [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
        }
    }

didReceiveRemoteNotification called but notification does not show. Thanks.

I think You haven't implemented iOS9 PushNotification Methods in your AppDelegate file

Objective-C

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

Swift

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any])

You can schedule local notification in above function while app is in the active state. To show notification.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM