次のコードを使用してfirebaseコンソールから送信されたfcmメッセージを受信していますが、この機能は呼び出されません。代わりに、アプリがバックグラウンドにあるときに私のランチャークラスでメッセージを受け取ることができますが、アプリがフォアグラウンドにあるときは取得できません。アプリケーションがフォアグラウンドにあるときに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());
}
}
マニフェスト
<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