Skip to content

Commit

Permalink
πŸ„πŸΌβ€β™‚οΈ [Feature] Add push notifications (litecoin-foundation#162)
Browse files Browse the repository at this point in the history
* Finally fixed the PN dep error caused by Firebase

- Added more analytics for the push notifications
- Simple clean up refactoring

Signed-off-by: kcw-grunt <[email protected]>

* Added the AdvertID into the BreadApp singleton

Signed-off-by: kcw-grunt <[email protected]>

* Added access and location of the key file

- addess access object, JSON object, method to read and operate
- updated gitignore
Signed-off-by: kcw-grunt <[email protected]>

---------

Signed-off-by: kcw-grunt <[email protected]>
  • Loading branch information
kcw-grunt authored Jan 29, 2024
1 parent 4ac8339 commit d34bb4f
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 35 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ crash.txt
.idea/copyright/
/app/google-services.json
/app/partner-keys.json
/app/src/main/assets/partner-keys.json
/.idea/markdown-navigator/
/app/src/main/java/com/breadwallet/presenter/fragments/FragmentBreadSignal.java
/fastlane/
Expand Down
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ repositories {

dependencies {

implementation 'org.json:json:20231013'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'junit:junit:4.12'
androidTestImplementation('androidx.test.espresso:espresso-core:3.5.1')
Expand Down Expand Up @@ -310,6 +311,7 @@ dependencies {
implementation 'com.google.firebase:firebase-analytics'
implementation 'com.google.firebase:firebase-crashlytics'
implementation 'com.google.firebase:firebase-crashlytics-ndk'
implementation('com.google.firebase:firebase-iid:21.1.0')

// push notifications
implementation 'com.google.firebase:firebase-messaging:23.4.0'
Expand Down
85 changes: 56 additions & 29 deletions app/src/main/java/com/breadwallet/BreadApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,31 @@
import android.content.Context;
import android.graphics.Point;
import android.hardware.fingerprint.FingerprintManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;

import com.breadwallet.di.component.DaggerAppComponent;
import com.breadwallet.presenter.activities.util.BRActivity;
import com.breadwallet.presenter.entities.PartnerNames;
import com.breadwallet.tools.listeners.SyncReceiver;
import com.breadwallet.tools.manager.AnalyticsManager;
import com.breadwallet.tools.util.BRConstants;
import com.breadwallet.tools.util.LocaleHelper;
import com.breadwallet.tools.util.Utils;
import com.google.android.gms.ads.identifier.AdvertisingIdClient;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.common.GooglePlayServicesRepairableException;
import com.google.firebase.crashlytics.FirebaseCrashlytics;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
Expand All @@ -39,7 +50,6 @@ public class BreadApp extends Application {
private static Timer isBackgroundChecker;
public static AtomicInteger activityCounter = new AtomicInteger();
public static long backgroundedTime;

private static Activity currentActivity;

@Override
Expand All @@ -52,32 +62,6 @@ public void onCreate() {
if (Utils.isEmulatorOrDebug(this)) {
enableCrashlytics = false;
}
// setup Push Notifications
//PushNotifications.start(getApplicationContext(), "06a438d5-27ba-4cc2-94df-554dc932a796");
//PushNotifications.addDeviceInterest("");

// // Pusher
// pushNotifications.start(instanceId: Partner.partnerKeyPath(name: .pusherStaging))
// // pushNotifications.registerForRemoteNotifications()
// let generaliOSInterest = "general-ios"
// let debugGeneraliOSInterest = "debug-general-ios"
//
// try? pushNotifications
// .addDeviceInterest(interest: generaliOSInterest)
// try? pushNotifications
// .addDeviceInterest(interest: debugGeneraliOSInterest)
//
// let interests = pushNotifications.getDeviceInterests()?.joined(separator: "|") ?? ""
// let device = UIDevice.current.identifierForVendor?.uuidString ?? "ID"
// let interestesDict: [String: String] = ["device_id": device,
// "pusher_interests": interests]
//
// LWAnalytics.logEventWithParameters(itemName: ._20231202_RIGI, properties: interestesDict)

// delay(4.0) {
// self.appDelegate.pushNotifications.registerForRemoteNotifications()
// }


// setup Timber
if(BuildConfig.DEBUG){
Expand All @@ -86,8 +70,8 @@ public void onCreate() {

FirebaseCrashlytics.getInstance().setCrashlyticsCollectionEnabled(enableCrashlytics);
AnalyticsManager.init(this);

AnalyticsManager.logCustomEvent(BRConstants._20191105_AL);
loadAdvertisingAndPush(this);

WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Expand Down Expand Up @@ -134,7 +118,6 @@ public void run() {
isBackgroundChecker.cancel();
fireListeners();
}
// APP in foreground, do something else
}
};

Expand All @@ -150,6 +133,50 @@ protected void attachBaseContext(Context base) {
public interface OnAppBackgrounded {
void onBackgrounded();
}
private void loadAdvertisingAndPush(Context app) {
Thread thr = new Thread(new Runnable() {
@Override
public void run() {
try {
AdvertisingIdClient.Info adInfo = AdvertisingIdClient.getAdvertisingIdInfo(app);
finished(adInfo);
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
finished(null);
}
});
thr.start();
}

private void finished(final AdvertisingIdClient.Info adInfo) {
if(adInfo!=null) {

String adInfoString = adInfo.getId();
String generalAndroidInterest = "general-android";
String debugGeneralAndroidInterest = "debug-general-android";
String pusherInstanceID = Utils.fetchPartnerKey(this, PartnerNames.PUSHERSTAGING);

// setup Push Notifications
//This worked had to add the iid dep https://github.com/mixpanel/mixpanel-android/issues/744
PushNotifications.start(getApplicationContext(), pusherInstanceID);
PushNotifications.addDeviceInterest(generalAndroidInterest);
PushNotifications.addDeviceInterest(debugGeneralAndroidInterest);

//Send params for pusher setup
Bundle params = new Bundle();
params.putString("general-interest",generalAndroidInterest);
params.putString("debug-general-interest",debugGeneralAndroidInterest);
params.putString("device-ad-info",adInfoString);
AnalyticsManager.logCustomEventWithParams(BRConstants._20240123_RAGI, params);
}
}

private static class CrashReportingTree extends Timber.Tree {
private static final String CRASHLYTICS_KEY_PRIORITY = "priority";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Created by sadia on 2020-January-27
* email: [email protected]
*/

public class Partner {
private int logo;
private int title;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.breadwallet.presenter.entities

import com.breadwallet.tools.security.BRKeyStore
import org.json.JSONObject
import java.util.regex.Pattern
import java.io.File

enum class PartnerNames(val key: String) {
MOONPAY("moonpay"),
BITREFILL("bitrefill"),
INFURA("infura-api"),
LITEWALLETOPS("litewallet-ops"),
LITEWALLETSTART("litewallet-start"),
PUSHER("pusher-instance-id"),
PUSHERSTAGING("pusher-staging-instance-id")
}
4 changes: 2 additions & 2 deletions app/src/main/java/com/breadwallet/tools/util/BRConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ private BRConstants() {
public static final String _20210427_HCIEEH = "heartbeat_check_if_event_even_happens";
public static final String _20220822_UTOU = "user_tapped_on_ud";
public static final String _20230131_NENR = "no_error_nominal_response";
public static final String _20231202_RIGI = "registered_ios_general_interest";
public static final String _20240123_RAGI = "registered_android_general_interest";
public static final String _20231225_UAP = "user_accepted_push";
public static final String _20240101_US = "user_signup";

Expand Down Expand Up @@ -189,7 +189,7 @@ private BRConstants() {
_20220822_UTOU,
_20230131_NENR,
_20230407_DCS,
_20231202_RIGI,
_20240123_RAGI,
_20231225_UAP,
_20240101_US
})
Expand Down
44 changes: 40 additions & 4 deletions app/src/main/java/com/breadwallet/tools/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.hardware.fingerprint.FingerprintManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.view.inputmethod.InputMethodInfo;
Expand All @@ -18,18 +19,29 @@
import androidx.core.app.ActivityCompat;

import com.breadwallet.presenter.activities.intro.IntroActivity;

import com.breadwallet.presenter.entities.PartnerNames;
import com.breadwallet.tools.manager.AnalyticsManager;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collection;
import java.util.List;
import java.util.Locale;

import timber.log.Timber;
import org.json.*;
import java.io.FileReader;
import java.io.InputStream;

import static android.content.Context.FINGERPRINT_SERVICE;

import java.io.FileNotFoundException;
import android.content.res.AssetManager;
public class Utils {

public static boolean isUsingCustomInputMethod(Activity context) {
Expand Down Expand Up @@ -211,5 +223,29 @@ public static String join(String[] array, CharSequence separator) {
stringBuilder.append(array[array.length - 1]);
return stringBuilder.toString();
}

public static String fetchPartnerKey(Context app, PartnerNames name) {
JSONObject keyObject;
AssetManager assetManager = app.getAssets();
try (InputStream inputStream = assetManager.open("partner-keys.json")) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
keyObject = new JSONObject(sb.toString()).getJSONObject("keys");
return keyObject.get(name.getKey()).toString();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
throw new RuntimeException(e);
}
} catch (IOException e) {
e.printStackTrace();
}
Bundle params = new Bundle();
params.putString("error_message: %s Key not found", name.toString());
AnalyticsManager.logCustomEventWithParams(BRConstants._20200112_ERR,params);
return "";
}
}

0 comments on commit d34bb4f

Please sign in to comment.