7

When my app is in the foreground, there is a alert that appears. How do I prevent this from appearing when receiving push notifications?

Tim Nuwin
  • 2,775
  • 2
  • 29
  • 63

4 Answers4

8

In your didFinishLaunchingWithOptions method of AppDelegate you have to add kOSSettingsKeyInAppAlerts = NO

[OneSignal initWithLaunchOptions:launchOptions appId:ONESIGNAL_APPID handleNotificationReceived:nil handleNotificationAction:nil
                            settings:@{kOSSettingsKeyInAppAlerts:@NO}];
Bhavin Ramani
  • 3,221
  • 5
  • 30
  • 41
  • 3
    Note that `kOSSettingsKeyInAppAlerts` is now deprecated and `kOSSettingsKeyInFocusDisplayOption` should be used instead. – jkasten Feb 18 '17 at 00:44
6

For Swift 3.0

// Initialize OngeSignal with Settings for Push Notifications
    OneSignal.initWithLaunchOptions(launchOptions, appId: Constants.OneSignalAppID, handleNotificationReceived: nil, handleNotificationAction: {
        (result) in
        // Do Something with Notification Result
    }, settings: [kOSSettingsKeyInFocusDisplayOption : OSNotificationDisplayType.none.rawValue])
emresancaktar
  • 1,507
  • 17
  • 25
3

By default OneSignal shows notifications as alert dialogs when the app is infocus. To change this pass kOSSettingsKeyInFocusDisplayOption with the value OSNotificationDisplayTypeNotification or OSNotificationDisplayTypeNone to settings on initWithLaunchOptions.

jkasten
  • 3,899
  • 3
  • 32
  • 51
1

I achieved it this way. Add the following code in your AppDelegate didFinishLaunchingWithOptions

OneSignal.inFocusDisplayType = OSNotificationDisplayType.none

on last line in

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {

OneSignal.inFocusDisplayType = OSNotificationDisplayType.none
return true }

we have these 3 options

public enum OSNotificationDisplayType : UInt {


/*Notification is silent, or app is in focus but InAppAlertNotifications are disabled*/
case none


/*Default UIAlertView display*/
case inAppAlert


/*iOS native notification display*/
case notification
}

Here's OneSignal Documentation

Muhammad Nayab
  • 1,612
  • 14
  • 14