diff --git a/android/react-native-fcm.iml b/android/react-native-fcm.iml index 17bc181efa4dd31c7f4e2a87eb5f0ad299054898..fa71af8096a2e0e9aa6cc118bb881f468a6f40eb 100644 --- a/android/react-native-fcm.iml +++ b/android/react-native-fcm.iml @@ -120,8 +120,8 @@ - + diff --git a/android/src/main/java/com/evollu/react/fcm/FIRLocalMessagingHelper.java b/android/src/main/java/com/evollu/react/fcm/FIRLocalMessagingHelper.java index 5a93828641695494f36812a6d3c5713b2dd518f5..cbc7bc678b70fb8af91dcbbd4e11a4531f3d1f89 100644 --- a/android/src/main/java/com/evollu/react/fcm/FIRLocalMessagingHelper.java +++ b/android/src/main/java/com/evollu/react/fcm/FIRLocalMessagingHelper.java @@ -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,31 +150,36 @@ public class FIRLocalMessagingHelper { } } - - Intent intent = new Intent(mContext, intentClass); - intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); - intent.putExtra("notification", bundle); - intent.putExtra("localNotification", true); - intent.setAction(bundle.getString("click_action")); - - int notificationID = bundle.containsKey("id") ? bundle.getString("id", "").hashCode() : (int) System.currentTimeMillis(); - PendingIntent pendingIntent = PendingIntent.getActivity(mContext, notificationID, intent, - PendingIntent.FLAG_UPDATE_CURRENT); - - NotificationManager notificationManager = - (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); - - notification.setContentIntent(pendingIntent); - - Notification info = notification.build(); - - if (bundle.containsKey("tag")) { - String tag = bundle.getString("tag"); - notificationManager.notify(tag, notificationID, info); - } else { - notificationManager.notify(notificationID, info); + 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.putExtras(bundle); + intent.putExtra("localNotification", true); + intent.setAction(bundle.getString("click_action")); + + int notificationID = bundle.containsKey("id") ? bundle.getString("id", "").hashCode() : (int) System.currentTimeMillis(); + PendingIntent pendingIntent = PendingIntent.getActivity(mContext, notificationID, intent, + PendingIntent.FLAG_UPDATE_CURRENT); + + NotificationManager notificationManager = + (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); + + notification.setContentIntent(pendingIntent); + + Notification info = notification.build(); + + 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 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; + } } diff --git a/android/src/main/java/com/evollu/react/fcm/FIRLocalMessagingPublisher.java b/android/src/main/java/com/evollu/react/fcm/FIRLocalMessagingPublisher.java index 2a44fb5feea722f5e000db4685de2f9ffc505602..1f0e0e8c353afe5f0a92e4414fb5e14ea380992c 100644 --- a/android/src/main/java/com/evollu/react/fcm/FIRLocalMessagingPublisher.java +++ b/android/src/main/java/com/evollu/react/fcm/FIRLocalMessagingPublisher.java @@ -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 { diff --git a/android/src/main/java/com/evollu/react/fcm/FIRMessagingModule.java b/android/src/main/java/com/evollu/react/fcm/FIRMessagingModule.java index d77554499e79c6c0f6b09472be8dcc8b271711ff..a8567b36a0bebae435265a0b567488319e00a494 100644 --- a/android/src/main/java/com/evollu/react/fcm/FIRMessagingModule.java +++ b/android/src/main/java/com/evollu/react/fcm/FIRMessagingModule.java @@ -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 diff --git a/index.js b/index.js index 593992abcbe6b0fcfdab60b324aab9df850fe084..726995a6b514ec4bc961d68540fd7231df2a1988 100644 --- a/index.js +++ b/index.js @@ -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); };