Commit 7daac877 authored by Libin Lu's avatar Libin Lu

do not show scheduled notification if app is foreground

parent 6a790a4a
......@@ -120,8 +120,8 @@
<orderEntry type="library" exported="" name="firebase-iid-9.4.0" level="project" />
<orderEntry type="library" exported="" name="okio-1.9.0" level="project" />
<orderEntry type="library" exported="" name="jsr305-3.0.0" level="project" />
<orderEntry type="library" exported="" name="okhttp-3.4.1" level="project" />
<orderEntry type="library" exported="" name="bolts-tasks-1.4.0" level="project" />
<orderEntry type="library" exported="" name="okhttp-3.4.1" level="project" />
<orderEntry type="library" exported="" name="firebase-common-9.4.0" level="project" />
<orderEntry type="library" exported="" name="appcompat-v7-23.0.1" level="project" />
<orderEntry type="library" exported="" name="firebase-core-9.4.0" level="project" />
......
......@@ -27,6 +27,7 @@ public class FIRLocalMessagingHelper {
private static final long DEFAULT_VIBRATION = 300L;
private static final String TAG = FIRLocalMessagingHelper.class.getSimpleName();
private final static String PREFERENCES_KEY = "ReactNativeSystemNotification";
private static boolean mIsForeground = false; //this is a hack
private Context mContext;
private SharedPreferences sharedPreferences = null;
......@@ -149,10 +150,15 @@ public class FIRLocalMessagingHelper {
}
}
if(mIsForeground){
Log.d(TAG, "App is in foreground, broadcast intent instead");
Intent i = new Intent("com.evollu.react.fcm.ReceiveLocalNotification");
i.putExtras(bundle);
mContext.sendOrderedBroadcast(i, null);
}else{
Intent intent = new Intent(mContext, intentClass);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("notification", bundle);
intent.putExtras(bundle);
intent.putExtra("localNotification", true);
intent.setAction(bundle.getString("click_action"));
......@@ -173,7 +179,7 @@ public class FIRLocalMessagingHelper {
} else {
notificationManager.notify(notificationID, info);
}
}
//clear out one time scheduled notification once fired
if(!bundle.containsKey("repeat_interval") && bundle.containsKey("fire_date")) {
SharedPreferences.Editor editor = sharedPreferences.edit();
......@@ -291,4 +297,8 @@ public class FIRLocalMessagingHelper {
}
return array;
}
public void setApplicationForeground(boolean foreground){
mIsForeground = foreground;
}
}
......@@ -4,7 +4,6 @@ import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class FIRLocalMessagingPublisher extends BroadcastReceiver {
......
......@@ -41,6 +41,7 @@ public class FIRMessagingModule extends ReactContextBaseJavaModule implements Li
getReactApplicationContext().addActivityEventListener(this);
registerTokenRefreshHandler();
registerMessageHandler();
registerLocalMessageHandler();
}
@Override
......@@ -149,6 +150,20 @@ public class FIRMessagingModule extends ReactContextBaseJavaModule implements Li
}, intentFilter);
}
private void registerLocalMessageHandler() {
IntentFilter intentFilter = new IntentFilter("com.evollu.react.fcm.ReceiveLocalNotification");
getReactApplicationContext().registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (getReactApplicationContext().hasActiveCatalystInstance()) {
sendEvent("FCMLocalNotificationReceived", Arguments.fromBundle(intent.getExtras()));
abortBroadcast();
}
}
}, intentFilter);
}
private WritableMap parseIntent(Intent intent){
WritableMap params;
Bundle extras = intent.getExtras();
......@@ -172,10 +187,12 @@ public class FIRMessagingModule extends ReactContextBaseJavaModule implements Li
@Override
public void onHostResume() {
mFIRLocalMessagingHelper.setApplicationForeground(true);
}
@Override
public void onHostPause() {
mFIRLocalMessagingHelper.setApplicationForeground(false);
}
@Override
......
......@@ -27,6 +27,9 @@ FCM.presentLocalNotification = (details) => {
};
FCM.scheduleLocalNotification = function(details) {
if (!details.id) {
throw new Error("id is required for scheduled notification");
}
RNFIRMessaging.scheduleLocalNotification(details);
};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment