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
7 changes: 6 additions & 1 deletion app/src/org/commcare/activities/connect/ConnectActivity.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.commcare.activities.connect;

import static org.commcare.connect.ConnectConstants.GO_TO_JOB_STATUS;
import static org.commcare.connect.ConnectConstants.NOTIFICATION_ID;
import static org.commcare.connect.ConnectConstants.REDIRECT_ACTION;
import static org.commcare.connect.ConnectConstants.SHOW_LAUNCH_BUTTON;
import static org.commcare.personalId.PersonalIdFeatureFlagChecker.FeatureFlag.NOTIFICATIONS;
Expand Down Expand Up @@ -37,6 +38,7 @@
import org.commcare.connect.PersonalIdManager;
import org.commcare.connect.database.ConnectJobUtils;
import org.commcare.connect.database.ConnectMessagingDatabaseHelper;
import org.commcare.connect.database.NotificationRecordDatabaseHelper;
import org.commcare.dalvik.R;
import org.commcare.fragments.RefreshableFragment;
import org.commcare.personalId.PersonalIdFeatureFlagChecker;
Expand Down Expand Up @@ -136,7 +138,10 @@ private int handleInfoRedirect(Bundle startArgs) {
private int handleSecureRedirect(Bundle startArgs) {
//Entering from a notification, so we may need to initialize
PersonalIdManager.getInstance().init(this);

String notificationId = getIntent().getStringExtra(NOTIFICATION_ID);
if (!TextUtils.isEmpty(notificationId)) {
NotificationRecordDatabaseHelper.INSTANCE.updateReadStatus(this, notificationId, true);
}
startArgs.putString(REDIRECT_ACTION, redirectionAction);
startArgs.putBoolean(SHOW_LAUNCH_BUTTON, getIntent().getBooleanExtra(SHOW_LAUNCH_BUTTON, true));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
import org.commcare.connect.MessageManager;
import org.commcare.connect.PersonalIdManager;
import org.commcare.connect.database.ConnectMessagingDatabaseHelper;
import org.commcare.connect.database.NotificationRecordDatabaseHelper;
import org.commcare.dalvik.R;

import static org.commcare.connect.ConnectConstants.CCC_MESSAGE;
import static org.commcare.connect.ConnectConstants.NOTIFICATION_ID;
import static org.commcare.connect.ConnectConstants.REDIRECT_ACTION;

public class ConnectMessagingActivity extends NavigationHostCommCareActivity<ConnectMessagingActivity> {
Expand Down Expand Up @@ -74,6 +76,10 @@ private void handleRedirectIfAny() {
if (success) {
String channelId = getIntent().getStringExtra(
ConnectMessagingMessageRecord.META_MESSAGE_CHANNEL_ID);
String notificationId = getIntent().getStringExtra(NOTIFICATION_ID);
if (!TextUtils.isEmpty(notificationId)) {
NotificationRecordDatabaseHelper.INSTANCE.updateReadStatus(this, notificationId, true);
}
if (TextUtils.isEmpty(channelId)) {
showFailureMessage(getString(R.string.connect_messaging_pn_wrong_channel));
} else {
Expand Down Expand Up @@ -151,5 +157,4 @@ private NavOptions getNavOptions() {
.setPopUpTo(navController.getGraph().getStartDestinationId(), true)
.build();
}

}
16 changes: 15 additions & 1 deletion app/src/org/commcare/pn/workers/NotificationsSyncWorker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import kotlinx.coroutines.withContext
import org.commcare.android.database.connect.models.ConnectJobRecord
import org.commcare.connect.ConnectActivityCompleteListener
import org.commcare.connect.ConnectConstants.NOTIFICATION_BODY
import org.commcare.connect.ConnectConstants.NOTIFICATION_ID
import org.commcare.connect.ConnectConstants.OPPORTUNITY_ID
import org.commcare.connect.ConnectJobHelper
import org.commcare.connect.database.ConnectJobUtils
import org.commcare.connect.database.NotificationRecordDatabaseHelper.getNotificationById
import org.commcare.dalvik.R
import org.commcare.utils.FirebaseMessagingUtil
import org.commcare.utils.FirebaseMessagingUtil.cccCheckPassed
Expand Down Expand Up @@ -194,8 +196,20 @@ class NotificationsSyncWorker(val appContext: Context, workerParams: WorkerParam
}

private fun raiseFCMPushNotificationIfApplicable() {
if (showNotification) {
if (showNotification && !isNotificationRead()) {
FirebaseMessagingUtil.handleNotification(appContext, notificationPayload, null, true)
}
}

private fun isNotificationRead(): Boolean {
return if (notificationPayload?.containsKey(NOTIFICATION_ID)!! &&
notificationPayload?.get(NOTIFICATION_ID) != null
) {
val notification =
getNotificationById(appContext, notificationPayload?.get(NOTIFICATION_ID)!!)
return notification != null && notification.readStatus
} else {
false
}
}
}
15 changes: 13 additions & 2 deletions app/src/org/commcare/utils/FirebaseMessagingUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Base64;

import androidx.core.app.NotificationCompat;
Expand Down Expand Up @@ -54,6 +55,7 @@
import static org.commcare.connect.ConnectConstants.CCC_MESSAGE;
import static org.commcare.connect.ConnectConstants.CCC_PAYMENT_INFO_CONFIRMATION;
import static org.commcare.connect.ConnectConstants.NOTIFICATION_BODY;
import static org.commcare.connect.ConnectConstants.NOTIFICATION_ID;
import static org.commcare.connect.ConnectConstants.NOTIFICATION_TITLE;
import static org.commcare.connect.ConnectConstants.OPPORTUNITY_ID;
import static org.commcare.connect.ConnectConstants.PAYMENT_ID;
Expand Down Expand Up @@ -437,6 +439,10 @@ private static NotificationCompat.Builder buildNotification(Context context, Int
? PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
: PendingIntent.FLAG_UPDATE_CURRENT;

Bundle bundleExtras = new Bundle();
bundleExtras.putString(NOTIFICATION_ID, fcmMessageData.getPayloadData().get(NOTIFICATION_ID));
intent.putExtra(NOTIFICATION_ID, fcmMessageData.getPayloadData().get(NOTIFICATION_ID));

PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, flags);

if (Strings.isEmptyOrWhitespace(fcmMessageData.getNotificationTitle()) && Strings.isEmptyOrWhitespace(fcmMessageData.getNotificationText())) {
Expand All @@ -452,7 +458,8 @@ private static NotificationCompat.Builder buildNotification(Context context, Int
.setAutoCancel(true)
.setSmallIcon(R.drawable.commcare_actionbar_logo)
.setPriority(fcmMessageData.getPriority())
.setWhen(System.currentTimeMillis());
.setWhen(System.currentTimeMillis())
.setExtras(bundleExtras);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when is this extras used ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To generate the unique notification id.

Copy link
Contributor

@shubham1g5 shubham1g5 Nov 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can't directly pass that as an argument to showNotification ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its being used across many functions so better to have extras as can be utilized directly without need to pass as function parameters everytime.

Copy link
Contributor

@shubham1g5 shubham1g5 Nov 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think we want to use exttas to pass arguments between methods as a design pattern. Bundles or extras are only really meant be used for cross-process comms and data passing as it has a extra overhead of serializing and unserializing data from a byte stream

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shubham1g5 Agree, it's mainly used for media type notification for maintaining the interaction for sessions in notification. But thought that single string will not cause overhead and it was giving nit code at that expense.


if (fcmMessageData.getLargeIcon() != null) {
fcmNotification.setLargeIcon(fcmMessageData.getLargeIcon());
Expand All @@ -469,7 +476,11 @@ private static NotificationCompat.Builder buildNotification(Context context, Int
*/
private static void showNotification(Context context, NotificationCompat.Builder notificationBuilder) {
NotificationManager mNM = (NotificationManager)context.getSystemService(NOTIFICATION_SERVICE);
mNM.notify(FCM_NOTIFICATION, notificationBuilder.build());
String notificationId = notificationBuilder.getExtras().getString(NOTIFICATION_ID);
int notifId = !TextUtils.isEmpty(notificationId)
? notificationId.hashCode()
: FCM_NOTIFICATION; // fallback to constant
mNM.notify(notifId, notificationBuilder.build());
}


Expand Down