Я использую следующий код для получения сообщения fcm, отправленного через консоль firebase, но эта функция никогда не вызывается. Вместо этого я получаю сообщение в своем классе запуска, когда приложение находится в фоновом режиме, но когда приложение находится на переднем плане, я не могу его получить.onMessageReceived() не вызывается, когда приложение находится на переднем плане
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
String message="";
Map<String, String> m1;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//Displaying data in log
//It is optional
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
System.out.println("---------------------");
message = remoteMessage.getNotification().getTitle();
m1= remoteMessage.getData();
System.out.println("=============="+m1);
System.out.println("=============="+m1.get("url"));
//Calling method to generate notification
//sendNotification(remoteMessage.getNotification().getBody());
//sendNotification_test(message);
createNotification(m1,remoteMessage.getNotification().getBody());
}
private void createNotification(Map<String,String> payload, String title){
Intent intent = new Intent(this, LoginActivity.class);
intent.putExtra("url",payload.get("url"));
intent.putExtra("package",payload.get("package"));
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("FCM Message")
.setContentText(title)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
Manifest
<service
android:name="com.example.pranshusrivastav.fcmtest.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service
android:name="com.example.pranshusrivastav.fcmtest.MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
Какова ваша полезная нагрузка в сообщении fcm – tyczj