我想从firebase获取令牌。但是我得到的消息是firebase初始化是不成功的。
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 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>
我已向manifest添加了服务。还添加了所需的库。我仍然无法获得令牌。我已将项目添加到firebase控制台。
我错过了什么?有人可以帮忙吗?谢谢..
答案 0 :(得分:3)
您必须启动服务,因为它不会自动启动。要启动Firebase服务,请使用以下代码: -
Intent intent=new Intent(ActivityLogin.this, FCMInstanceIDListenerService.class);
startService(intent);
此外,您可能正在使用较旧的类路径进行Google服务。使用最新版本更新它。对于当前版本,它将如下所示。
classpath 'com.google.gms:google-services:3.0.0'
答案 1 :(得分:2)
您使用的是Google Services Gradle Plugin的旧版本。
在项目gradle文件中,替换:
classpath 'com.google.gms:google-services:1.3.0-beta1'
与
classpath 'com.google.gms:google-services:3.0.0'
答案 2 :(得分:-1)
还要检查清单文件中是否提供了Internet许可。解决了我的问题。