2016-10-07 212 views
2

我想从Firebase获取令牌。但是我得到的消息是firebase初始化不成功。Firebase未初始化

应用摇篮:

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' 

项目摇篮:

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 
} 

火力地堡:

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 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> 

我已经添加了清单的服务。还添加了所需的库。仍然我无法获得令牌。我已经将项目添加到Firebase控制台。

我错过了什么?任何人都可以帮忙吗?谢谢。

+0

你在任何地方开始你的服务? –

+0

你有没有把你的项目中的fire base创建的json文件(google-services.json) –

+0

你是什么意思?我在应用程序上运行这个来获取令牌。 @VivekMishra – Sid

回答

2

你必须要对你的服务,因为它不会自动启动。要启动您的火力地堡服务使用下面的代码: -

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

此外,您可能正在使用旧的类路径的谷歌服务。用最新版本更新它。对于目前的版本,它会看起来像这样。

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

我试过这种方式仍然显示相同的消息。 @Vivek Mishra – Sid

+0

您是否在控制台中正确配置了您的项目? –

+0

是包名称在控制台中是正确的。创建项目。还有什么需要做的?我跟着谷歌开发者教程。 @Vivek Mishra – Sid

1

您正在使用旧版本的Google Services Gradle Plugin

在你的gradle项目文件,替换:

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

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