Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Referral Notifications - FCM Push Notifications #2667

Merged
merged 19 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 2 additions & 15 deletions app/src/org/commcare/CommCareApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,8 @@
import androidx.work.PeriodicWorkRequest;
import androidx.work.WorkManager;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import com.google.firebase.analytics.FirebaseAnalytics;
import com.google.firebase.messaging.FirebaseMessaging;

import net.sqlcipher.database.SQLiteDatabase;
import net.sqlcipher.database.SQLiteException;
Expand Down Expand Up @@ -109,6 +106,7 @@
import org.commcare.utils.CrashUtil;
import org.commcare.utils.DeviceIdentifier;
import org.commcare.utils.FileUtil;
import org.commcare.utils.FirebaseMessagingUtil;
import org.commcare.utils.GlobalConstants;
import org.commcare.utils.MarkupUtil;
import org.commcare.utils.MultipleAppsUtil;
Expand All @@ -117,7 +115,6 @@
import org.commcare.utils.SessionStateUninitException;
import org.commcare.utils.SessionUnavailableException;
import org.commcare.views.widgets.CleanRawMedia;
import org.conscrypt.Conscrypt;
import org.javarosa.core.model.User;
import org.javarosa.core.reference.ReferenceManager;
import org.javarosa.core.reference.RootTranslator;
Expand All @@ -128,12 +125,10 @@
import org.javarosa.core.util.externalizable.PrototypeFactory;

import java.io.File;
import java.security.Security;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import javax.annotation.Nullable;
Expand Down Expand Up @@ -253,8 +248,7 @@ public void onCreate() {
LocalePreferences.saveDeviceLocale(Locale.getDefault());
GraphUtil.setLabelCharacterLimit(getResources().getInteger(R.integer.graph_label_char_limit));

// Retrieve the current Firebase Cloud Messaging (FCM) registration token^M
FirebaseMessaging.getInstance().getToken().addOnCompleteListener(handleFCMTokenRetrieval());
FirebaseMessagingUtil.verifyToken();
}

protected void attachISRGCert() {
Expand Down Expand Up @@ -1198,11 +1192,4 @@ public boolean isNsdServicesEnabled() {
return true;
}

private OnCompleteListener handleFCMTokenRetrieval(){
return (OnCompleteListener<String>) task -> {
if (!task.isSuccessful()) {
Logger.exception("Fetching FCM registration token failed", task.getException());
}
};
}
}
20 changes: 20 additions & 0 deletions app/src/org/commcare/utils/FirebaseMessagingUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@

import androidx.preference.PreferenceManager;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.firebase.messaging.FirebaseMessaging;

import org.commcare.CommCareApplication;
import org.commcare.dalvik.BuildConfig;
import org.javarosa.core.services.Logger;

public class FirebaseMessagingUtil {
public static final String FCM_TOKEN = "fcm_token";
Expand Down Expand Up @@ -46,4 +51,19 @@ public static String removeServerUrlFromUserDomain(String userDomain) {
}
return userDomain;
}

public static void verifyToken() {
if(!BuildConfig.DEBUG) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not init token in debug ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nothing in particular, just trying to avoid noise but I'm happy to revert that.

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah we should revert to be able to have receive push notifications with debug apk as well.

// Retrieve the current Firebase Cloud Messaging (FCM) registration token
FirebaseMessaging.getInstance().getToken().addOnCompleteListener(handleFCMTokenRetrieval());
}
}

private static OnCompleteListener handleFCMTokenRetrieval(){
return (OnCompleteListener<String>) task -> {
if (!task.isSuccessful()) {
Logger.exception("Fetching FCM registration token failed", task.getException());
}
};
}
}