Skip to content

Commit

Permalink
Fixes crash with FirebaseInstanceId
Browse files Browse the repository at this point in the history
* This crash happens due Google not handling the case where there isn't a default FireBase app.
* Resolves OneSignal#552
  • Loading branch information
jkasten2 committed Jul 3, 2018
1 parent fa78bb8 commit f614e8b
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 0 deletions.
1 change: 1 addition & 0 deletions OneSignalSDK/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 42 additions & 0 deletions OneSignalSDK/app/google-services.json
Original file line number Diff line number Diff line change
@@ -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"
}
2 changes: 2 additions & 0 deletions OneSignalSDK/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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]'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,48 @@

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 <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
// Note: Starting with Firebase Messaging 17.1.0 onNewToken in FirebaseMessagingService should be
// used instead.

class PushRegistratorFCM extends PushRegistratorAbstractGoogle {

private static final String FCM_APP_NAME = "ONESIGNAL_SDK_FCM_APP_NAME";

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";
Expand Down

0 comments on commit f614e8b

Please sign in to comment.