user717452
user717452

Reputation: 111

iOS App Not Receiving Notifications if App is in foreground

I'm having issues with my app and push notifications. I'm using the Parse server and back4app.com to handle push notifications. The notifications arrive just fine if the app is running in the background, or not even active, but if the app is being used, nothing ever pops up. What am I missing here? The app also allows for local notifications, reminders of certain things as well, so I don't know for sure if that is causing issues with it.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{ 
    window.rootViewController = navController;
    [window makeKeyAndVisible];
    [UNUserNotificationCenter currentNotificationCenter].delegate = self;
    [Parse initializeWithConfiguration:[ParseClientConfiguration configurationWithBlock:^(id<ParseMutableClientConfiguration> configuration) {
           configuration.applicationId = @"APPID";
           configuration.clientKey = @"CLIENTKEY";
           configuration.server = @"https://parseapi.back4app.com";
       }]];
   
    
    UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
                                                    UIUserNotificationTypeBadge |
                                                    UIUserNotificationTypeSound);
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes
                                                                             categories:nil];
    [application registerUserNotificationSettings:settings];
    [application registerForRemoteNotifications];
    
  
 
   
    NSDictionary *userInfo = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];

    if (userInfo) {
        [self handleRemoteNotificationWithPayload:userInfo];
        
    }
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
        willPresentNotification:(UNNotification *)notification
        withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {

    // Play a sound.
   completionHandler(UNNotificationPresentationOptionSound);
    NSLog( @"Here handle push notification in foreground" );
    //For notification Banner - when app in foreground
    
    completionHandler(UNNotificationPresentationOptionAlert);
    
    // Print Notification info
    NSLog(@"Userinfo %@",notification.request.content.userInfo);
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
           didReceiveNotificationResponse:(UNNotificationResponse *)response
           withCompletionHandler:(void (^)(void))completionHandler {
    NSLog(@"RECEIVED HIS WORD %@", response.notification.request.content.categoryIdentifier);

    if ([response.notification.request.content.categoryIdentifier isEqualToString:@"OPEN_HIS_WORD"]) {
        NSLog(@"RECEIVED HIS WORD");
        HisWord *hiSword= [[HisWord alloc] initWithNibName:@"HisWord" bundle:[NSBundle mainBundle]];

        
        [navController pushViewController:hiSword animated:YES];

 
    }
 
    // Else handle actions for other notification types. . .
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    // Store the deviceToken in the current installation and save it to Parse.
    PFInstallation *currentInstallation = [PFInstallation currentInstallation];
    [currentInstallation setDeviceTokenFromData:deviceToken];
    [currentInstallation saveInBackground];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    
    
   
    
    [PFPush handlePush:userInfo];
    UINavigationController *nav = (UINavigationController *) self.navController;
    DirectoryViewController *dvController8 = [[DirectoryViewController alloc] initWithNibName:@"DirectoryViewController" bundle:[NSBundle mainBundle]];
    [nav pushViewController:dvController8 animated:YES];
    
    if (application.applicationState == UIApplicationStateInactive) {
        
        [self handleRemoteNotificationWithPayload:userInfo];
    }
}
-(void)handleRemoteNotificationWithPayload:(NSDictionary *)payload {
    UINavigationController *nav = (UINavigationController *) self.navController;
    DirectoryViewController *dvController8 = [[DirectoryViewController alloc] initWithNibName:@"DirectoryViewController" bundle:[NSBundle mainBundle]];
    [nav pushViewController:dvController8 animated:YES];
    
    
    
}

Upvotes: 0

Views: 978

Answers (1)

Phil Dukhov
Phil Dukhov

Reputation: 88182

You call completionHandler of userNotificationCenter:willPresentNotification:withCompletionHandler: two times, and the second one is ignored. Instead you need to pass both options like this:

completionHandler(UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert);

Upvotes: 1

Related Questions