taivo / parse-push-plugin

Push notification plugin for Cordova/Phonegap/ionic on Parse platform
118 stars 102 forks source link

Push notification displayed into the system tray on iOS, even if the application is in foreground #116

Closed Kira2 closed 6 years ago

Kira2 commented 6 years ago

Hi,

On iOS, when the application is in foreground, the notifications are still shown into the system tray about it. I did not test it on Android. How to hide them in this case ?

Thank you.

Kira2 commented 6 years ago

Hi,

I could find an answer on StackOverflow.

Indeed, according to Apple documentation, willPresentNotification is called when a notification is delivered to a foreground app.

And when we look into the file ParsePushPlugin.m, we see that this line is always executed to show an alert:

completionHandler(UNNotificationPresentationOptionAlert);

So, I reused the solution given on StackOverflow for willPresentNotification, to prevent notifications to be shown into the system tray when the application is in foreground:

-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
    NSLog(@"User info %@", notification.request.content.userInfo);

    UIApplication *application = [UIApplication sharedApplication];
    [self jsCallback:notification.request.content.userInfo withAction:(application.applicationState == UIApplicationStateActive) ? @"RECEIVE" : @"OPEN"];

    // do not show notifications into the system tray if the application is in foreground
    if (application.applicationState == UIApplicationStateActive) {
        completionHandler(UNNotificationPresentationOptionNone);
    }
    else {
        completionHandler(UNNotificationPresentationOptionAlert);
    }
}

As I did this for my specific use case, I do not know if this is a common behavior to always hide the notifications when the application is in foreground. Maybe this could be an option of the plugin that could be configured into the config.xml file, like ParseMultiNotifications etc.

I still submitted a pull-request if you think that this code could be useful.

If anybody have a better idea to handle this case, let me know ;-)

Thanks.