diff options
Diffstat (limited to 'android/app/src/fcm/java')
| -rw-r--r-- | android/app/src/fcm/java/io/coinos/app/CoinosApp.java | 12 | ||||
| -rw-r--r-- | android/app/src/fcm/java/io/coinos/app/Fcm.java | 64 | ||||
| -rw-r--r-- | android/app/src/fcm/java/io/coinos/app/FcmService.java | 19 |
3 files changed, 95 insertions, 0 deletions
diff --git a/android/app/src/fcm/java/io/coinos/app/CoinosApp.java b/android/app/src/fcm/java/io/coinos/app/CoinosApp.java new file mode 100644 index 0000000..855db91 --- /dev/null +++ b/android/app/src/fcm/java/io/coinos/app/CoinosApp.java @@ -0,0 +1,12 @@ +package io.coinos.app; + +import android.app.Application; + +/** Sets Firebase up before anything in the process (a push included) needs it. */ +public class CoinosApp extends Application { + @Override + public void onCreate() { + super.onCreate(); + Fcm.init(this); + } +} diff --git a/android/app/src/fcm/java/io/coinos/app/Fcm.java b/android/app/src/fcm/java/io/coinos/app/Fcm.java new file mode 100644 index 0000000..02ce12c --- /dev/null +++ b/android/app/src/fcm/java/io/coinos/app/Fcm.java @@ -0,0 +1,64 @@ +package io.coinos.app; + +import android.content.Context; + +import com.google.firebase.FirebaseApp; +import com.google.firebase.FirebaseOptions; +import com.google.firebase.messaging.FirebaseMessaging; + +/** + * Firebase Cloud Messaging, for a phone with Google Play Services: the same + * channel web push rides in Chrome, so the wallet is reached with no + * distributor app to install. Its token goes to the web app as the push + * endpoint "fcm:<token>", down the road a UnifiedPush endpoint takes (?up=, + * or straight into the page's storage), and the notifier sends an fcm: + * endpoint through Firebase. A phone without Play Services, or a build made + * without android/app/google-services.json, falls back to UnifiedPush. + * + * <p>Firebase is set up here from the values build.gradle read out of that + * file, rather than by Google's Gradle plugin. + */ +final class Fcm { + private Fcm() {} + static final String PREFIX = "fcm:"; + + static boolean configured() { return !BuildConfig.FCM_APP_ID.isEmpty(); } + + static boolean available(Context c) { + if (!configured()) return false; + return Push.playServices(c); + } + + /** Called from CoinosApp, before anything (FcmService included) asks for Firebase. */ + static void init(Context c) { + if (!configured()) return; + try { + if (!FirebaseApp.getApps(c).isEmpty()) return; + FirebaseApp.initializeApp(c, new FirebaseOptions.Builder() + .setApplicationId(BuildConfig.FCM_APP_ID) + .setApiKey(BuildConfig.FCM_API_KEY) + .setProjectId(BuildConfig.FCM_PROJECT_ID) + .setGcmSenderId(BuildConfig.FCM_SENDER_ID) + .build()); + } catch (Throwable ignored) {} + } + + /** + * Ask for the token. It arrives later; onEndpoint runs (on any thread) once + * it's stored as this app's push endpoint. + * @return whether Firebase will deliver this app's pushes + */ + static boolean register(Context c, Runnable onEndpoint) { + if (!available(c)) return false; + init(c); + try { + FirebaseMessaging.getInstance().getToken().addOnSuccessListener((token) -> { + if (token == null || token.isEmpty()) return; + String ep = PREFIX + token; + if (!ep.equals(Push.endpoint(c))) Push.setEndpoint(c, ep); + if (onEndpoint != null) onEndpoint.run(); + }); + return true; + } catch (Throwable t) { return false; } + } +} diff --git a/android/app/src/fcm/java/io/coinos/app/FcmService.java b/android/app/src/fcm/java/io/coinos/app/FcmService.java new file mode 100644 index 0000000..24d4aca --- /dev/null +++ b/android/app/src/fcm/java/io/coinos/app/FcmService.java @@ -0,0 +1,19 @@ +package io.coinos.app; + +import com.google.firebase.messaging.FirebaseMessagingService; +import com.google.firebase.messaging.RemoteMessage; + +/** Firebase's half of PushReceiver: the notifier's JSON arrives as data "body". */ +public class FcmService extends FirebaseMessagingService { + @Override + public void onNewToken(String token) { + // a rotated token is a new endpoint; the page gets it on its next load + if (token != null && !token.isEmpty()) Push.setEndpoint(this, Fcm.PREFIX + token); + } + + @Override + public void onMessageReceived(RemoteMessage msg) { + String body = msg.getData() == null ? null : msg.getData().get("body"); + if (body != null) PushReceiver.onPush(this, body); + } +} |
