Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class NotificationHubUtil {
private static final String KEY_FOR_PREFS_CHANNELENABLELIGHTS = "AzureNotificationHub_channelEnableLights";
private static final String KEY_FOR_PREFS_CHANNELENABLEVIBRATION = "AzureNotificationHub_channelEnableVibration";

private boolean mIsForeground;

public static NotificationHubUtil getInstance() {
if (sharedNotificationHubUtilInstance == null) {
sharedNotificationHubUtilInstance = new NotificationHubUtil();
Expand Down Expand Up @@ -130,6 +132,14 @@ public boolean hasChannelEnableVibration(Context context) {
return hasKey(context, KEY_FOR_PREFS_CHANNELENABLEVIBRATION);
}

public void setAppIsForeground(boolean isForeground) {
mIsForeground = isForeground;
}

public boolean getAppIsForeground() {
return mIsForeground;
}

public NotificationHub createNotificationHub(String hubName, String connectionString, ReactContext reactContext) {
NotificationHub hub = new NotificationHub(hubName, connectionString, reactContext);
return hub;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,21 @@ public void onNewToken(String token) {

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

if (notificationChannelID == null) {
createNotificationChannel(this);
}

Bundle bundle = remoteMessage.toIntent().getExtras();
ReactNativeNotificationsHandler.sendNotification(this, bundle, notificationChannelID);
if (notificationHubUtil.getAppIsForeground()) {
bundle.putBoolean("foreground", true);
bundle.putBoolean("userInteraction", false);
bundle.putBoolean("coldstart", false);
} else {
ReactNativeNotificationsHandler.sendNotification(this, bundle, notificationChannelID);
}
ReactNativeNotificationsHandler.sendBroadcast(this, bundle, 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public class ReactNativeNotificationHubModule extends ReactContextBaseJavaModule

private ReactApplicationContext mReactContext;
private LocalBroadcastReceiver mLocalBroadcastReceiver;
private boolean mIsForeground;

public ReactNativeNotificationHubModule(ReactApplicationContext reactContext) {
super(reactContext);
Expand All @@ -67,6 +66,16 @@ public String getName() {
return AZURE_NOTIFICATION_HUB_NAME;
}

public void setIsForeground(boolean isForeground) {
NotificationHubUtil notificationHubUtil = NotificationHubUtil.getInstance();
notificationHubUtil.setAppIsForeground(isForeground);
}

public boolean getIsForeground() {
NotificationHubUtil notificationHubUtil = NotificationHubUtil.getInstance();
return notificationHubUtil.getAppIsForeground();
}

@ReactMethod
public void register(ReadableMap config, Promise promise) {
NotificationHubUtil notificationHubUtil = NotificationHubUtil.getInstance();
Expand Down Expand Up @@ -168,7 +177,8 @@ public void unregister(Promise promise) {

@Override
public void onHostResume() {
mIsForeground = true;
setIsForeground(true);

Activity activity = getCurrentActivity();
if (activity != null) {
Intent intent = activity.getIntent();
Expand All @@ -188,7 +198,7 @@ public void onHostResume() {

@Override
public void onHostPause() {
mIsForeground = false;
setIsForeground(false);
}

@Override
Expand All @@ -213,7 +223,7 @@ public void onActivityResult(Activity activity, int requestCode, int resultCode,
public class LocalBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (mIsForeground) {
if (getIsForeground()) {
String event = intent.getStringExtra("event");
String data = intent.getStringExtra("data");
mReactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
Expand Down