I use OneSignal push notifications. When android app is in foreground and receives a notification, it creates an alert box with the notification. How to prevent this from appearing when receiving notifications?
Asked
Active
Viewed 1.2k times
10
-
Is the receiver implemented inside an activity? If it is that might be the problem. Try making the receiver a separate class. – Quintin Balsdon Dec 16 '16 at 12:03
-
1Here is a similar question about ios [link](http://stackoverflow.com/questions/40165840/how-do-i-prevent-alert-when-app-is-on-foreground-with-incoming-onesignal-push-no?rq=1) So I think it could be solved by disabling some option, because it creates alert by default. – user3671635 Dec 16 '16 at 13:30
5 Answers
16
From The SDK documentation - When you startInit OneSignal, make sure to call inFocusDisplaying with "None" to disable OneSignal's in app AlertBox.
also on NotificationReceivedHandler section -
Important behavior notes - If you will be displaying your own in app message when a notification is received make sure to call inFocusDisplaying with None to disable OneSignal's in app AlertBox.

davidlj95
- 124
- 7

Nati Sholman Oskar
- 1,369
- 11
- 11
-
1As for the latest version of OneSignal SDK, seems that's deprecated right now. Seems https://documentation.onesignal.com/v7.0/docs/sdk-reference#app-in-focus-notification-display is now the way to go – davidlj95 Sep 20 '21 at 09:24
5
Using this code line, I resolved my issue.
OneSignal.inFocusDisplaying(2);

Janaka Pushpakumara
- 4,769
- 5
- 29
- 34
3
It's changed in OneSignal 4.0.
For Kotlin:
OneSignal.setNotificationWillShowInForegroundHandler { notificationReceivedEvent ->
notificationReceivedEvent.complete(null)
}
For Java:
OneSignal.setNotificationWillShowInForegroundHandler(new NotificationWillShowInForegroundHandler() {
@Override
void notificationWillShowInForeground(OSNotificationReceivedEvent notificationReceivedEvent) {
notificationReceivedEvent.complete(null);
}
});

Muz
- 5,866
- 3
- 47
- 65
2
I had similar issues and I resolved it by using inFocusDisplaying
here's how to use this in android.
public class MyApplicationClass extends Application {
private static Context context;
PlayerIdsession session;
public static Context getContext() {
return context;
}
@Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
//MyNotificationOpenedHandler : This will be called when a notification is tapped on.
//MyNotificationReceivedHandler : This will be called when a notification is received while your app is running.
OneSignal.startInit(this)
.setNotificationOpenedHandler(new MyNotiOpenedHandler())
.setNotificationReceivedHandler( new MyNotiReceivedHandler() )
.inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
.init();
OneSignal.idsAvailable(new OneSignal.IdsAvailableHandler() {
@Override
public void idsAvailable(String userId, String registrationId) {
if (userId != null){
session=new PlayerIdsession(context);
session.savePlayerId(userId);
Log.d("debug", "PlayerId:" + userId);
}
/* if (registrationId != null){
Log.d("debug", "registrationId:" + registrationId);
}*/
}
});
}
}

Rajwant Kaur Boughan
- 155
- 3
- 13
2
Just add this line in your windows.plugin.signal
.inFocusDisplaying(window.plugins.OneSignal.OSInFocusDisplayOption.Notification)
for example :-
window.plugins.OneSignal
.startInit("YOUR_APPID")
.inFocusDisplaying(window.plugins.OneSignal.OSInFocusDisplayOption.Notification)
.endInit();

mayur sawant
- 33
- 6