我有一段时间没有编程android了,我发现Firebase消息传递服务已被弃用。
我实现了新的FirebaseMessagingService
类,并且重写了onNewToken以获得令牌,但是该方法甚至没有被调用,因为文档中说该方法应自动运行。但是现在我很困惑,为什么不生成令牌?这是我实现的
public class FireBsaeTest extends FirebaseMessagingService {
public FireBsaeTest(){
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getData().size() > 0) {
} else {
}
}
if (remoteMessage.getNotification() != null) {
}
}
@Override
public void onNewToken(String token) {
System.out.println(token);
}}
这是清单
<service android:name=".FireBsaeTest">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
我想念什么吗?
更新
我尝试从我的活动中手动调用它,以阻止新的非法异常firebaseNotInilized
FirebaseApp.initializeApp(this);
FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(new OnSuccessListener<InstanceIdResult>() {
@Override
public void onSuccess(InstanceIdResult instanceIdResult) {
System.out.println(instanceIdResult.getToken());
}
});
答案 0 :(得分:0)
我只知道问题出在哪里,问题在于使用类路径实施根build.gradle
文件,android studio像下面这样配置依赖项
dependencies {
classpath 'com.android.tools.build:gradle:3.3.0'
classpath 'com.google.gms:google-services:4.1.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
应将Google-services配置为classpath 'com.google.gms:google-services:4.0.1'
一旦我更改了班级路径,它就可以工作了,现在无法获取令牌
答案 1 :(得分:-1)
MyFirebaseMessagingService
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = MyFirebaseMessagingService.class.getSimpleName();
@Override
public void onNewToken(String s) {
super.onNewToken(s);
Log.i(TAG, "onNewToken: >>> " + s);
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
}
}
MyFirebaseInstanceIDService.java
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
@Override
public void onTokenRefresh() {
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d("FirebaseInstanceID", "Refreshed token: " + refreshedToken);
// Notify UI that registration has completed, so the progress indicator can be hidden.
}
}
Manifest.xml
<service android:name=".services.MyFirebaseMessagingService"
android:exported="false"
android:stopWithTask="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service
android:name=".services.MyFirebaseInstanceIDService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
成绩
implementation 'com.google.firebase:firebase-core:16.0.6'
implementation 'com.google.firebase:firebase-messaging:17.3.4'