问题描述
我正在尝试将Android客户端应用程序从Google Cloud Messaging迁移到Firebase云消息传递。我严格遵循,但未成功 - onMessageReceived()
方法在前台应用程序时不会被调用。
build.gradle(项目级别)
依赖关系{
//其他东西
classpath'com.google.gms:google-services:3.0.0'
}
build.gradle(应用级别)
应用插件:'com.google.gms.google-services'//此行在底部
和
依赖关系{
//其他的东西在这里
编译'com.google .firebase:firebase-messaging:9.0.0'
}
AndroidManifest。 xml
< service android:name = service.FirebaseService >
< intent-filter>
< / intent-filter>
< / service>
和
< service
android:name =。service.MyInstanceIDListenerService
android:exported =false>
< intent-filter>
< / intent-filter>
< / service>
和
< service
android:name =。service.RegistrationIntentService
android:exported =false/>
作为< application>
标签的子元素。
$ b MyInstanceIDListenerService.java
公共类MyInstanceIDListenerService扩展FirebaseInstanceIdService {
final String TAG =Firebase实例ID;
@Override
public void onTokenRefresh(){
String refreshedToken = FirebaseInstanceId.getInstance()。getToken();
Log.d(TAG,刷新标记:+ refreshedToken);
sendRegistrationToServer(refreshedToken);
}
private void sendRegistrationToServer(String token){
Intent intent = new Intent(RegistrationIntentServiceEvent.TOKEN);
intent.putExtra(RegistrationIntentServiceEvent.TOKEN,token);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
}
FirebaseService.java
public class FirebaseService extends FirebaseMessagingService {
// OtherStuff
@Override
public void onMessageReceived(RemoteMessage message){
String = message.getFrom();
Map data = message.getData();
Log.d(TAG,Message:+ from); //不会出现在logcat中
//其他东西
}
}
RegistrationIntentService
public class RegistrationIntentService extends IntentService {
private static final String TAG =RegIntentService;
public RegistrationIntentService(){
super(TAG);
}
@Override
保护无效的onHandleIntent(意图意图){
尝试{
同步(TAG){
字符串标记= FirebaseInstanceId 。.getInstance()为gettoken();
Log.i(TAG,GCM Registration Token:+ token); //这工作正常
//并显示此行 - 12-19 17:59:33.295 3146-5386 / ru。 bpc.mobilebank.bpc I / RegIntentService:GCM注册令牌:* some_token *
sendRegistrationToServer(token);
} catch(Exception e){
Log.d(TAG,无法完成令牌刷新,e);
}
}
private void sendRegistrationToServer(String token){
Intent intent = new Intent(RegistrationIntentServiceEvent.TOKEN);
intent.putExtra(RegistrationIntentServiceEvent.TOKEN,token);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
$ / code>
请告诉我我做错了什么,为什么注册似乎做得很好,但即使应用程序处于前台,也不会调用 onMessageReceived()
方法。提前致谢。
P。顺便说一下,在Firebase控制台中添加SHA-1密钥是否必要?也许这可能会导致问题?但Firebase说这个动作是可选的。
如果您使用FCM,那么您应该替换这个
< service android:name =。service.GcmService>
< intent-filter>
< / intent-filter>
< / service>
至
< service android:name =。service.FirebaseService>
< intent-filter>
< / intent-filter>
< / service>
您的服务未正确注册..
I'm trying to migrate Android client app from Google Cloud Messaging to Firebase Cloud Messaging. I strictly followed an official tutorial, but didn't succeed - onMessageReceived()
method is not called when app in foreground.
So here are code snippets that I touched.
build.gradle (project level)
dependencies {
//other stuff
classpath 'com.google.gms:google-services:3.0.0'
}
build.gradle (app level)
apply plugin: 'com.google.gms.google-services' //this line in the bottom
and
dependencies {
//other stuff here
compile 'com.google.firebase:firebase-messaging:9.0.0'
}
AndroidManifest.xml
<service android:name=".service.FirebaseService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
and
<service
android:name=".service.MyInstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
and
<service
android:name=".service.RegistrationIntentService"
android:exported="false" />
as childs of <application>
tag.
MyInstanceIDListenerService.java
public class MyInstanceIDListenerService extends FirebaseInstanceIdService {
final String TAG = "Firebase instance id";
@Override
public void onTokenRefresh() {
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
sendRegistrationToServer(refreshedToken);
}
private void sendRegistrationToServer(String token) {
Intent intent = new Intent(RegistrationIntentServiceEvent.TOKEN);
intent.putExtra(RegistrationIntentServiceEvent.TOKEN, token);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
}
FirebaseService.java
public class FirebaseService extends FirebaseMessagingService {
//OtherStuff
@Override
public void onMessageReceived(RemoteMessage message){
String from = message.getFrom();
Map data = message.getData();
Log.d(TAG, "Message: " + from); //Never appears in logcat
//other stuff
}
}
RegistrationIntentService
public class RegistrationIntentService extends IntentService {
private static final String TAG = "RegIntentService";
public RegistrationIntentService() {
super(TAG);
}
@Override
protected void onHandleIntent(Intent intent) {
try {
synchronized (TAG) {
String token = FirebaseInstanceId.getInstance().getToken();
Log.i(TAG, "GCM Registration Token: " + token);//This works fine
//and shows this line - 12-19 17:59:33.295 3146-5386/ru.bpc.mobilebank.bpc I/RegIntentService: GCM Registration Token: *some_token*
sendRegistrationToServer(token);
}
} catch (Exception e) {
Log.d(TAG, "Failed to complete token refresh", e);
}
}
private void sendRegistrationToServer(String token) {
Intent intent = new Intent(RegistrationIntentServiceEvent.TOKEN);
intent.putExtra(RegistrationIntentServiceEvent.TOKEN, token);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
}
Please, tell if i did something wrong and why registrations seems to be done properly, but the onMessageReceived()
method is never called even if the app is in foreground. Thanks in advance.
P.S. By the way, is adding SHA-1 keys in Firebase console necessary? maybe this could cause the problem? But Firebase says this action is optional.
if you are using FCM then you should replace this
<service android:name=".service.GcmService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
to
<service android:name=".service.FirebaseService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
your service not registered properly..
这篇关于将GCM迁移到FirebaseCM:onMessageReceived()未在前台调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!