我已经使用以下代码来记录日志、显示Toast并从onMessageReceived发送广播给服务。
我能够在日志中看到使用Firebase控制台发送的数据。但是Toast不显示,而且也没有发送广播。
以下是代码:
class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
Toast.makeText(getApplicationContext(),"From: " + remoteMessage.getFrom(),Toast.LENGTH_SHORT).show();
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
Toast.makeText(getApplicationContext(),"Toast shown",Toast.LENGTH_LONG).show();
Intent i = new Intent();
i.setAction("firebase.message.combinedthings");
sendBroadcast(i);
}
}
}