Skip to content
This repository has been archived by the owner on Jan 14, 2025. It is now read-only.

Option to silence Android foreground notifications #1183

Merged
merged 7 commits into from
Apr 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ PushNotification.localNotification({
visibility: "private", // (optional) set notification visibility, default: private
importance: "high", // (optional) set notification importance, default: high
allowWhileIdle: false, // (optional) set notification to work while on doze, default: false
ignoreInForeground: false, // (optional) if true, the notification will not be visible when the app is in the foreground (useful for parity with how iOS notifications appear)

/* iOS only properties */
alertAction: 'view', // (optional) default: view
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.dieam.reactnativepushnotification.modules;


import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.AlarmManager;
import android.app.Application;
import android.app.Notification;
Expand Down Expand Up @@ -31,6 +33,7 @@
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import static com.dieam.reactnativepushnotification.modules.RNPushNotification.LOG_TAG;
import static com.dieam.reactnativepushnotification.modules.RNPushNotificationAttributes.fromJson;
Expand Down Expand Up @@ -392,14 +395,16 @@ public void sendToNotificationCentre(Bundle bundle) {
commit(editor);
}

Notification info = notification.build();
info.defaults |= Notification.DEFAULT_LIGHTS;

if (bundle.containsKey("tag")) {
String tag = bundle.getString("tag");
notificationManager.notify(tag, notificationID, info);
} else {
notificationManager.notify(notificationID, info);
if (!(this.isApplicationInForeground(context) && bundle.getBoolean("ignoreInForeground"))) {
Notification info = notification.build();
info.defaults |= Notification.DEFAULT_LIGHTS;

if (bundle.containsKey("tag")) {
String tag = bundle.getString("tag");
notificationManager.notify(tag, notificationID, info);
} else {
notificationManager.notify(notificationID, info);
}
}

// Can't use setRepeating for recurring notifications because setRepeating
Expand Down Expand Up @@ -607,4 +612,20 @@ private void checkOrCreateChannel(NotificationManager manager) {
manager.createNotificationChannel(channel);
channelCreated = true;
}

private boolean isApplicationInForeground(Context context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> processInfos = activityManager.getRunningAppProcesses();
if (processInfos != null) {
for (RunningAppProcessInfo processInfo : processInfos) {
if (processInfo.processName.equals(context.getPackageName())
&& processInfo.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND
&& processInfo.pkgList.length > 0) {
return true;
}
}
}
return false;
}

}