我正在开发一个Android应用程序,我已经创建了令牌来发送推送通知,但令牌没有显示。
同时也没有任何错误。
我正在使用firebase云消息服务来生成令牌。
这是我的应用程序等级文件
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.example.visvasrithilakshan.myapplication"
minSdkVersion 24
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.google.firebase:firebase-messaging:11.8.0'
// implementation 'com.google.firebase:firebase-messaging:17.3.4'
//implementation 'com.google.firebase:firebase-auth:11.6.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
implementation 'com.android.support:cardview-v7:26.1.0'
}
apply plugin: 'com.google.gms.google-services'
这是我的构建等级文件
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.4'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
// classpath 'com.google.gms:google-services:3.1.1'
classpath 'com.google.gms:google-services:4.0.2'
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
这是我的MyFirebaseInstanceIdService.java
import android.util.Log;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;
public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d("myfirebaseid", "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);
}
}
逐步:
1( 转到根级别的build.gradle文件,在依赖项下添加此行classpath 'com.google.gms:google-services:4.2.0'
或更低版本的
2( 转到你的应用程序构建渐变文件,并在底部添加此行:apply plugin: 'com.google.gms.google-services'
3( 前往Firebase控制台下载google-services.json
文件并将其放在app
文件夹下
4( 实施您的服务:
public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {
private static String TAG = "FirebaseInstance";
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.e(TAG, "Refreshed token: " + refreshedToken);
}
}
5( 将已实现的服务添加到您的清单:
<service
android:name=".MyFirebaseInstanceIdService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
6( 实现您的FirebaseMessagingService
并将其添加到清单:
<service
android:name=".MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
仅此而已。
附言:我把它从我自己的项目和所有作品中拿走了。
FirebaseMessaging.getInstance().getToken().addOnCompleteListener(new OnCompleteListener<String>() {
@Override
public void onComplete(@NonNull Task<String> task) {
if (task.isComplete()) {
token[0] = task.getResult();
Log.e("AppConstants", "onComplete: new Token got: " + token[0]);
Log.d("tokeb", "checking token");
Log.d("token", token[0]+ "V");
UpdateToken(token[0]); // to Server
}
}
});
在活动的oncreate 中运行此代码
FirebaseInstanceId.getInstance().getInstanceId().addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
@Override
public void onComplete(@NonNull Task<InstanceIdResult> task) {
String token = task.getResult().getToken();
Log.i("Token", token);
}
});