Search code examples
iosobjective-cunusernotificationcenter

avoid showing alert for notification while app is in foreground


hi I used this UNUserNotificationCenterDelegate for avoiding alert notification while app is in foreground with this function

-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler  API_AVAILABLE(ios(10.0)){
    completionHandler(UNNotificationPresentationOptionAlert);
}

but I get notification with both option Alert & banner how can I remove alert while app is in foreground?


Solution

  • I found how to fix there is a option for one signal init to avoid showing alert

    [OneSignal initWithLaunchOptions:launchOptions appId:@"YOURE_KEY" handleNotificationReceived:nil handleNotificationAction:nil
                                settings:@{kOSSettingsKeyInAppAlerts:@NO}];
    

    and for showing as banner you have to add this code

    -(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler  API_AVAILABLE(ios(10.0)){
        completionHandler(UNNotificationPresentationOptionAlert);
    }