Firebase onMessageReceived(RemoteMessage remoteMessage)
에서 앱이 Foreground에있을 때이 메소드가 호출됩니다. 알림을 클릭하면 다른 활동 (예 : NotiicationActivity
)을 열 수 있습니다.앱이 배경에있을 때 Firebase onMessageReceived (RemoteMessage remoteMessage)가 호출되지 않습니다.
하지만 앱이 백그라운드 일 경우이 메소드는 호출되지 않으며 알림을 클릭하면 실행기 활동 만 열립니다.
우리의 앱이 백그라운드에 있어도 알림을 클릭하면 어떻게 열리나요?
내 코드는 이것이다 :
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
}
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
sendNotification(remoteMessage.getNotification().getBody());
}
private void sendNotification(String messageBody) {
Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("key", messageBody);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Bitmap icon2 = BitmapFactory.decodeResource(getResources(),
R.mipmap.ic_launcher);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("FCM Sample")
.setContentText(messageBody)
.setAutoCancel(true)
.setLargeIcon(icon2)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(new Random().nextInt() /* ID of notification */, notificationBuilder.build());
}
}
이 답변을 읽기 : 문제 [여기 링크입니다 (http://stackoverflow.com/questions/을 sloved 40626233/firebase-onmessage-received-when-app-is-inactive/40626866 # 40626866) –