Sid
Sid

Reputation: 2862

Firebase not getting initialized

I want to get the token from firebase. But message I get is firebase initialization is unsuccessful.

App Gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.1"

    defaultConfig {
        applicationId "com.example.siddhi.contactsapp"
        minSdkVersion 14
        targetSdkVersion 24
        versionCode 1
        multiDexEnabled true
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false

            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.2.1'
    compile 'com.android.support:recyclerview-v7:24.2.1'
    compile 'de.hdodenhof:circleimageview:2.1.0'
    compile 'com.android.support:design:24.2.1'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'com.google.code.gson:gson:2.2.4'
    compile 'com.google.android.gms:play-services:9.6.0'
    compile 'com.google.firebase:firebase-messaging:9.6.0'
    compile 'com.firebase:firebase-client-android:2.5.2'
    compile 'com.android.support:multidex:1.0.0'
}
apply plugin: 'com.google.gms.google-services'

Project Gradle :

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.0'
        classpath 'com.google.gms:google-services:1.3.0-beta1'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files

    }
}

allprojects {
    repositories {
        maven { url "https://dl.bintray.com/michelelacorte/maven/" }
        jcenter()
        mavenCentral()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

Firebase :

    public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {

    private static final String TAG = "MyFirebaseIIDService";

    /**
     * Called if InstanceID token is updated. This may occur if the security of
     * the previous token had been compromised. Note that this is called when the InstanceID token
     * is initially generated so this is where you would retrieve the token.
     */
    // [START refresh_token]
    @Override
    public void onTokenRefresh() {
        // Get updated InstanceID token.
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG, "Refreshed token: " + refreshedToken);

        // If you want to send messages to this application instance or
        // manage this apps subscriptions on the server side, send the
        // Instance ID token to your app server.
        sendRegistrationToServer(refreshedToken);
    }
    // [END refresh_token]

    /**
     * Persist token to third-party servers.
     *
     * Modify this method to associate the user's FCM InstanceID token with any server-side account
     * maintained by your application.
     *
     * @param token The new token.
     */
    private void sendRegistrationToServer(String token) {
        // TODO: Implement this method to send token to your app server.
    }
}

Manifest :

   <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.siddhi.contactsapp">

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>

    <application
        android:name="android.support.multidex.MultiDexApplication"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".Activities.MainActivity" />
        <service
            android:name="com.example.siddhi.contactsapp.helper.MyFirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
        </service>

        <service
            android:name="com.example.siddhi.contactsapp.helper.MyFirebaseInstanceIDService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
            </intent-filter>
        </service>
        <activity
            android:name=".Activities.RegisterActivity"
            android:label="@string/title_activity_main2"
            android:theme="@style/AppTheme"
            android:windowSoftInputMode="stateHidden" />
        <activity android:name=".Activities.DetailViewActivity" />
        <activity
            android:name=".Activities.ProfileActivity"
            android:windowSoftInputMode="stateHidden" />
        <activity
            android:name=".Activities.LoginActivity"
            android:label="@string/title_activity_login"
            android:theme="@style/AppTheme"
            android:windowSoftInputMode="stateHidden" />
        <activity android:name=".Activities.StartUpActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service  android:enabled="true" android:name="com.example.siddhi.contactsapp.helper.MessageService"></service>
        <activity
            android:name=".Activities.ForgotPasswordActivity"
            android:label="@string/title_activity_forgot_password"
            android:theme="@style/AppTheme"
            android:windowSoftInputMode="stateHidden" />
        <activity android:name=".Activities.InviteContactsActivity" />
        <activity
            android:name=".Activities.PendingInvitesActivity"
            android:label="@string/title_activity_pending_invites"
            android:theme="@style/AppTheme"></activity>
        <receiver android:name="com.example.siddhi.contactsapp.helper.MessageService$SmsSentReceiver">
        </receiver>

        <receiver android:name="com.example.siddhi.contactsapp.helper.MessageService$SmsDeliveredReceiver"/>
    </application>

</manifest>

I have added services to manifest. Also added the libraries which are required. Still I am unable to get the token. I have added project to the firebase console.

What am I missing? Can anyone help please? Thank you..

Upvotes: 2

Views: 551

Answers (3)

Ameer Hamza
Ameer Hamza

Reputation: 1

Also Check that Internet Permission is given in your manifest file. that resolved my issue.

Upvotes: -1

Vivek Mishra
Vivek Mishra

Reputation: 5705

You have to start your service as it will not start automatically. To start your Firebase service use the following code:-

Intent intent=new Intent(ActivityLogin.this, FCMInstanceIDListenerService.class);
startService(intent);

Moreover,You are probably using older class path for google services. Update it with the latest version. For current version it will look like this.

classpath 'com.google.gms:google-services:3.0.0'

Upvotes: 3

Bob Snyder
Bob Snyder

Reputation: 38289

You are using a very old version of Google Services Gradle Plugin.

In your project gradle file, replace:

classpath 'com.google.gms:google-services:1.3.0-beta1'

with

classpath 'com.google.gms:google-services:3.0.0'

Upvotes: 2

Related Questions