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 @@ ...@@ -120,8 +120,8 @@
<orderEntry type="library" exported="" name="firebase-iid-9.4.0" level="project" /> <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="okio-1.9.0" level="project" />
<orderEntry type="library" exported="" name="jsr305-3.0.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="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="firebase-common-9.4.0" level="project" />
<orderEntry type="library" exported="" name="appcompat-v7-23.0.1" 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" /> <orderEntry type="library" exported="" name="firebase-core-9.4.0" level="project" />
......
...@@ -27,6 +27,7 @@ public class FIRLocalMessagingHelper { ...@@ -27,6 +27,7 @@ public class FIRLocalMessagingHelper {
private static final long DEFAULT_VIBRATION = 300L; private static final long DEFAULT_VIBRATION = 300L;
private static final String TAG = FIRLocalMessagingHelper.class.getSimpleName(); private static final String TAG = FIRLocalMessagingHelper.class.getSimpleName();
private final static String PREFERENCES_KEY = "ReactNativeSystemNotification"; private final static String PREFERENCES_KEY = "ReactNativeSystemNotification";
private static boolean mIsForeground = false; //this is a hack
private Context mContext; private Context mContext;
private SharedPreferences sharedPreferences = null; private SharedPreferences sharedPreferences = null;
...@@ -149,31 +150,36 @@ public class FIRLocalMessagingHelper { ...@@ -149,31 +150,36 @@ public class FIRLocalMessagingHelper {
} }
} }
if(mIsForeground){
Intent intent = new Intent(mContext, intentClass); Log.d(TAG, "App is in foreground, broadcast intent instead");
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); Intent i = new Intent("com.evollu.react.fcm.ReceiveLocalNotification");
intent.putExtra("notification", bundle); i.putExtras(bundle);
intent.putExtra("localNotification", true); mContext.sendOrderedBroadcast(i, null);
intent.setAction(bundle.getString("click_action")); }else{
Intent intent = new Intent(mContext, intentClass);
int notificationID = bundle.containsKey("id") ? bundle.getString("id", "").hashCode() : (int) System.currentTimeMillis(); intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(mContext, notificationID, intent, intent.putExtras(bundle);
PendingIntent.FLAG_UPDATE_CURRENT); intent.putExtra("localNotification", true);
intent.setAction(bundle.getString("click_action"));
NotificationManager notificationManager =
(NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); int notificationID = bundle.containsKey("id") ? bundle.getString("id", "").hashCode() : (int) System.currentTimeMillis();
PendingIntent pendingIntent = PendingIntent.getActivity(mContext, notificationID, intent,
notification.setContentIntent(pendingIntent); PendingIntent.FLAG_UPDATE_CURRENT);
Notification info = notification.build(); NotificationManager notificationManager =
(NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
if (bundle.containsKey("tag")) {
String tag = bundle.getString("tag"); notification.setContentIntent(pendingIntent);
notificationManager.notify(tag, notificationID, info);
} else { Notification info = notification.build();
notificationManager.notify(notificationID, info);
if (bundle.containsKey("tag")) {
String tag = bundle.getString("tag");
notificationManager.notify(tag, notificationID, info);
} else {
notificationManager.notify(notificationID, info);
}
} }
//clear out one time scheduled notification once fired //clear out one time scheduled notification once fired
if(!bundle.containsKey("repeat_interval") && bundle.containsKey("fire_date")) { if(!bundle.containsKey("repeat_interval") && bundle.containsKey("fire_date")) {
SharedPreferences.Editor editor = sharedPreferences.edit(); SharedPreferences.Editor editor = sharedPreferences.edit();
...@@ -291,4 +297,8 @@ public class FIRLocalMessagingHelper { ...@@ -291,4 +297,8 @@ public class FIRLocalMessagingHelper {
} }
return array; return array;
} }
public void setApplicationForeground(boolean foreground){
mIsForeground = foreground;
}
} }
...@@ -4,7 +4,6 @@ import android.app.Application; ...@@ -4,7 +4,6 @@ import android.app.Application;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.util.Log;
public class FIRLocalMessagingPublisher extends BroadcastReceiver { public class FIRLocalMessagingPublisher extends BroadcastReceiver {
......
...@@ -41,6 +41,7 @@ public class FIRMessagingModule extends ReactContextBaseJavaModule implements Li ...@@ -41,6 +41,7 @@ public class FIRMessagingModule extends ReactContextBaseJavaModule implements Li
getReactApplicationContext().addActivityEventListener(this); getReactApplicationContext().addActivityEventListener(this);
registerTokenRefreshHandler(); registerTokenRefreshHandler();
registerMessageHandler(); registerMessageHandler();
registerLocalMessageHandler();
} }
@Override @Override
...@@ -149,6 +150,20 @@ public class FIRMessagingModule extends ReactContextBaseJavaModule implements Li ...@@ -149,6 +150,20 @@ public class FIRMessagingModule extends ReactContextBaseJavaModule implements Li
}, intentFilter); }, 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){ private WritableMap parseIntent(Intent intent){
WritableMap params; WritableMap params;
Bundle extras = intent.getExtras(); Bundle extras = intent.getExtras();
...@@ -172,10 +187,12 @@ public class FIRMessagingModule extends ReactContextBaseJavaModule implements Li ...@@ -172,10 +187,12 @@ public class FIRMessagingModule extends ReactContextBaseJavaModule implements Li
@Override @Override
public void onHostResume() { public void onHostResume() {
mFIRLocalMessagingHelper.setApplicationForeground(true);
} }
@Override @Override
public void onHostPause() { public void onHostPause() {
mFIRLocalMessagingHelper.setApplicationForeground(false);
} }
@Override @Override
......
...@@ -27,6 +27,9 @@ FCM.presentLocalNotification = (details) => { ...@@ -27,6 +27,9 @@ FCM.presentLocalNotification = (details) => {
}; };
FCM.scheduleLocalNotification = function(details) { FCM.scheduleLocalNotification = function(details) {
if (!details.id) {
throw new Error("id is required for scheduled notification");
}
RNFIRMessaging.scheduleLocalNotification(details); 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