Skip to content

Commit a4f82f1

Browse files
authored
Merge pull request #109 from kreativ-software/no-notification-while-foreground
[fix] no notification in tray when app in foreground (like firebase)
2 parents 51a97e4 + 7629b5a commit a4f82f1

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

android/src/main/java/com/azure/reactnative/notificationhub/NotificationHubUtil.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public class NotificationHubUtil {
2525
private static final String KEY_FOR_PREFS_CHANNELENABLELIGHTS = "AzureNotificationHub_channelEnableLights";
2626
private static final String KEY_FOR_PREFS_CHANNELENABLEVIBRATION = "AzureNotificationHub_channelEnableVibration";
2727

28+
private boolean mIsForeground;
29+
2830
public static NotificationHubUtil getInstance() {
2931
if (sharedNotificationHubUtilInstance == null) {
3032
sharedNotificationHubUtilInstance = new NotificationHubUtil();
@@ -130,6 +132,14 @@ public boolean hasChannelEnableVibration(Context context) {
130132
return hasKey(context, KEY_FOR_PREFS_CHANNELENABLEVIBRATION);
131133
}
132134

135+
public void setAppIsForeground(boolean isForeground) {
136+
mIsForeground = isForeground;
137+
}
138+
139+
public boolean getAppIsForeground() {
140+
return mIsForeground;
141+
}
142+
133143
public NotificationHub createNotificationHub(String hubName, String connectionString, ReactContext reactContext) {
134144
NotificationHub hub = new NotificationHub(hubName, connectionString, reactContext);
135145
return hub;

android/src/main/java/com/azure/reactnative/notificationhub/ReactNativeFirebaseMessagingService.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,21 @@ public void onNewToken(String token) {
5858

5959
@Override
6060
public void onMessageReceived(RemoteMessage remoteMessage) {
61+
NotificationHubUtil notificationHubUtil = NotificationHubUtil.getInstance();
6162
Log.d(TAG, "Remote message from: " + remoteMessage.getFrom());
6263

6364
if (notificationChannelID == null) {
6465
createNotificationChannel(this);
6566
}
6667

6768
Bundle bundle = remoteMessage.toIntent().getExtras();
68-
ReactNativeNotificationsHandler.sendNotification(this, bundle, notificationChannelID);
69+
if (notificationHubUtil.getAppIsForeground()) {
70+
bundle.putBoolean("foreground", true);
71+
bundle.putBoolean("userInteraction", false);
72+
bundle.putBoolean("coldstart", false);
73+
} else {
74+
ReactNativeNotificationsHandler.sendNotification(this, bundle, notificationChannelID);
75+
}
6976
ReactNativeNotificationsHandler.sendBroadcast(this, bundle, 0);
7077
}
7178
}

android/src/main/java/com/azure/reactnative/notificationhub/ReactNativeNotificationHubModule.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ public class ReactNativeNotificationHubModule extends ReactContextBaseJavaModule
4949

5050
private ReactApplicationContext mReactContext;
5151
private LocalBroadcastReceiver mLocalBroadcastReceiver;
52-
private boolean mIsForeground;
5352

5453
public ReactNativeNotificationHubModule(ReactApplicationContext reactContext) {
5554
super(reactContext);
@@ -67,6 +66,16 @@ public String getName() {
6766
return AZURE_NOTIFICATION_HUB_NAME;
6867
}
6968

69+
public void setIsForeground(boolean isForeground) {
70+
NotificationHubUtil notificationHubUtil = NotificationHubUtil.getInstance();
71+
notificationHubUtil.setAppIsForeground(isForeground);
72+
}
73+
74+
public boolean getIsForeground() {
75+
NotificationHubUtil notificationHubUtil = NotificationHubUtil.getInstance();
76+
return notificationHubUtil.getAppIsForeground();
77+
}
78+
7079
@ReactMethod
7180
public void register(ReadableMap config, Promise promise) {
7281
NotificationHubUtil notificationHubUtil = NotificationHubUtil.getInstance();
@@ -168,7 +177,8 @@ public void unregister(Promise promise) {
168177

169178
@Override
170179
public void onHostResume() {
171-
mIsForeground = true;
180+
setIsForeground(true);
181+
172182
Activity activity = getCurrentActivity();
173183
if (activity != null) {
174184
Intent intent = activity.getIntent();
@@ -188,7 +198,7 @@ public void onHostResume() {
188198

189199
@Override
190200
public void onHostPause() {
191-
mIsForeground = false;
201+
setIsForeground(false);
192202
}
193203

194204
@Override
@@ -213,7 +223,7 @@ public void onActivityResult(Activity activity, int requestCode, int resultCode,
213223
public class LocalBroadcastReceiver extends BroadcastReceiver {
214224
@Override
215225
public void onReceive(Context context, Intent intent) {
216-
if (mIsForeground) {
226+
if (getIsForeground()) {
217227
String event = intent.getStringExtra("event");
218228
String data = intent.getStringExtra("data");
219229
mReactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)

0 commit comments

Comments
 (0)