From f614e8b28bc1cb3b8d1dcf3427db06728464f757 Mon Sep 17 00:00:00 2001 From: Josh Kasten Date: Mon, 2 Jul 2018 16:39:13 -0700 Subject: [PATCH] Fixes crash with FirebaseInstanceId * This crash happens due Google not handling the case where there isn't a default FireBase app. * Resolves #552 --- OneSignalSDK/app/build.gradle | 1 + OneSignalSDK/app/google-services.json | 42 +++++++++++++++++++ OneSignalSDK/build.gradle | 2 + .../main/java/com/onesignal/OneSignal.java | 2 + .../com/onesignal/PushRegistratorFCM.java | 29 +++++++++++++ 5 files changed, 76 insertions(+) create mode 100644 OneSignalSDK/app/google-services.json diff --git a/OneSignalSDK/app/build.gradle b/OneSignalSDK/app/build.gradle index 6d592f390e..127996360c 100644 --- a/OneSignalSDK/app/build.gradle +++ b/OneSignalSDK/app/build.gradle @@ -48,6 +48,7 @@ dependencies { // exclude group: 'com.google.android.gms', module: 'play-services-gcm' // exclude group: 'com.google.android.gms', module: 'play-services-analytics' // exclude group: 'com.google.android.gms', module: 'play-services-location' +// exclude group: 'com.google.firebase', module: 'firebase-messaging' } // Use snapshot diff --git a/OneSignalSDK/app/google-services.json b/OneSignalSDK/app/google-services.json new file mode 100644 index 0000000000..aa39f9b48c --- /dev/null +++ b/OneSignalSDK/app/google-services.json @@ -0,0 +1,42 @@ +{ + "project_info": { + "project_number": "86233933250", + "firebase_url": "https://onesignalexample-8d956.firebaseio.com", + "project_id": "onesignalexample-8d956", + "storage_bucket": "onesignalexample-8d956.appspot.com" + }, + "client": [ + { + "client_info": { + "mobilesdk_app_id": "1:86233933250:android:37fb51944ca88b0f", + "android_client_info": { + "package_name": "com.onesignal.example" + } + }, + "oauth_client": [ + { + "client_id": "86233933250-qcj1t4q0lpon6q1nidojhrdh6gmgg3b6.apps.googleusercontent.com", + "client_type": 3 + } + ], + "api_key": [ + { + "current_key": "AIzaSyCvCs5vm7k3mo_2K0oB00QL7Z2zQKqSYY0" + } + ], + "services": { + "analytics_service": { + "status": 1 + }, + "appinvite_service": { + "status": 1, + "other_platform_oauth_client": [] + }, + "ads_service": { + "status": 2 + } + } + } + ], + "configuration_version": "1" +} \ No newline at end of file diff --git a/OneSignalSDK/build.gradle b/OneSignalSDK/build.gradle index a52b4d1d97..477876555e 100644 --- a/OneSignalSDK/build.gradle +++ b/OneSignalSDK/build.gradle @@ -13,6 +13,8 @@ buildscript { dependencies { classpath 'com.android.tools.build:gradle:3.1.0' + classpath 'com.google.gms:google-services:4.0.1' + // OneSignal-Gradle-Plugin - Local testing // classpath 'com.onesignal:onesignal-gradle-plugin:[0.8.1, 0.99.99]' diff --git a/OneSignalSDK/onesignal/src/main/java/com/onesignal/OneSignal.java b/OneSignalSDK/onesignal/src/main/java/com/onesignal/OneSignal.java index 9bac22455c..1458a2cd1b 100644 --- a/OneSignalSDK/onesignal/src/main/java/com/onesignal/OneSignal.java +++ b/OneSignalSDK/onesignal/src/main/java/com/onesignal/OneSignal.java @@ -640,6 +640,8 @@ public static void init(Context context, String googleProjectNumber, String oneS if (TrackFirebaseAnalytics.CanTrack()) trackFirebaseAnalytics = new TrackFirebaseAnalytics(appContext); + + PushRegistratorFCM.disableFirebaseInstanceIdService(appContext); initDone = true; diff --git a/OneSignalSDK/onesignal/src/main/java/com/onesignal/PushRegistratorFCM.java b/OneSignalSDK/onesignal/src/main/java/com/onesignal/PushRegistratorFCM.java index 7376824684..624928748d 100644 --- a/OneSignalSDK/onesignal/src/main/java/com/onesignal/PushRegistratorFCM.java +++ b/OneSignalSDK/onesignal/src/main/java/com/onesignal/PushRegistratorFCM.java @@ -27,12 +27,19 @@ package com.onesignal; +import android.content.ComponentName; +import android.content.Context; +import android.content.pm.PackageManager; + import com.google.firebase.FirebaseApp; import com.google.firebase.FirebaseOptions; import com.google.firebase.iid.FirebaseInstanceId; +import com.google.firebase.iid.FirebaseInstanceIdService; import com.google.firebase.messaging.FirebaseMessaging; // TODO: 4.0.0 - Switch to using +// Note: Starting with Firebase Messaging 17.1.0 onNewToken in FirebaseMessagingService should be +// used instead. class PushRegistratorFCM extends PushRegistratorAbstractGoogle { @@ -40,6 +47,28 @@ class PushRegistratorFCM extends PushRegistratorAbstractGoogle { private FirebaseApp firebaseApp; + // Disable in the case where there isn't a default Firebase app as this will crash the app. + // The crash will happen where the Google Play services app fires an intent that the token + // needs to be refreshed. + // This checks for gcm_defaultSenderId in values.xml (normally added from google-services.json) + // https://github.com/OneSignal/OneSignal-Android-SDK/issues/552 + static void disableFirebaseInstanceIdService(Context context) { + String senderId = OSUtils.getResourceString(context, "gcm_defaultSenderId", null); + int componentState = + senderId == null ? + PackageManager.COMPONENT_ENABLED_STATE_DISABLED : + PackageManager.COMPONENT_ENABLED_STATE_ENABLED; + + PackageManager pm = context.getPackageManager(); + try { + ComponentName componentName = new ComponentName(context, FirebaseInstanceIdService.class); + pm.setComponentEnabledSetting(componentName, componentState, PackageManager.DONT_KILL_APP); + } catch (NoClassDefFoundError ignored) { + // Will throw if missing FirebaseInstanceIdService class, ignore in this case. + // We already print a logcat error in another spot + } + } + @Override String getProviderName() { return "FCM";