我的设备未收到其他通知(FCM) [英] My device doesn't receive other notification (FCM)
问题描述
我迁移到Firebase Cloud Messaging,当初次尝试发送消息时,我使用Firebase中的控制台收到了一个通知,然后尝试在几分钟后发送另一个通知,但我不再收到另一个通知,但是在我的Firebase控制台中它说是Completed
更新
这是我的代码
MainActivity
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkPlayServices();
Log.i(TAG, "InstanceID token: " + FirebaseInstanceId.getInstance().getToken());
}
....
}
MyFirebaseInstanceIDService
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
sendRegistrationToServer(refreshedToken);
}
}
MyFirebaseMessagingService
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
// [START receive_message]
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
}
// [END receive_message]
似乎其他设备收到了它,但其他设备没有收到它.
由于AdamK的评论询问我是否在前台发送第二条通知,所以我发现仅当我的应用程序在后台时才触发通知.>
在文档中,据说在onMessageReceived()
中,如果应用程序位于前台,则同时处理onMessageReceived()
中的数据和通知消息.因此,如果该应用程序位于前台,因此要执行此操作,请在您的onMessageReceived()
回调中自行触发通知.
我在我的onMessageReceived()
中调用自己的sendNotification()
方法:
sendNotification(remoteMessage.getNotification().getBody());
即使应用程序位于前台,也会显示通知.
I migrated to Firebase Cloud Messaging and when first tried sending message I received a notification using the console in Firebase and then attempted to send another notification after a few minutes but i no longer receives another notification but in my firebase console it said that it was Completed
update
Here is my code
MainActivity
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkPlayServices();
Log.i(TAG, "InstanceID token: " + FirebaseInstanceId.getInstance().getToken());
}
....
}
MyFirebaseInstanceIDService
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
sendRegistrationToServer(refreshedToken);
}
}
MyFirebaseMessagingService
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
// [START receive_message]
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
}
// [END receive_message]
it seems that other devices receives it but the other devices doesn't received it.
Due to AdamK's comment asking if I send the second notification while in foreground, I found out that notification is only triggered when my app is in the background.
In the documentation it was said that in onMessageReceived()
, If the application is in the foreground handle both data and notification messages in the onMessageReceived()
. Therefore a notification will not be triggered automatically if the app is in foreground, so in order to do that, in your onMessageReceived()
callback you need to trigger a notification yourself.
I call my own sendNotification()
method inside my onMessageReceived()
:
sendNotification(remoteMessage.getNotification().getBody());
Which shows the notification even when the app is in the foreground.
这篇关于我的设备未收到其他通知(FCM)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!