2016-12-07 1 views
3

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()); 
    } 
} 
+0

이 답변을 읽기 : 문제 [여기 링크입니다 (http://stackoverflow.com/questions/을 sloved 40626233/firebase-onmessage-received-when-app-is-inactive/40626866 # 40626866) –

답변

3

onMessageReceived 만 트리거됩니다. 앱이 백그라운드로 설정되어있는 경우에도 알림을받을 수 있지만 onMessageReceived은 실행되지 않습니다.

그래서 내 제안, 수신 활동에 대한 , 당신은 여전히 ​​사용하여 통지의 데이터를 얻을 수 있습니다 :

getIntent().getExtras(); 

이 적절하게 작동합니다. 희망이 도움이됩니다 : 당신의 sendNotification에에서

+0

고마워요, 그냥 이랬어. 'if (getIntent(). getExtras()! = null) { // 여기에 썼습니다. 인 텐트 intent = 새 인 텐트 (MainActivity.this, NotificationActivity.class); startActivity (intent); }' – Shekhar

+0

@Shekhar, 아무도 걱정하지 마시고 다른 사람들이이 해결책이 효과가 있다는 것을 알 수 있도록 투표하십시오. 고맙습니다. – kenix

0

시도하고 아래 링크를 따르십시오 : 응용 프로그램이 포 그라운드에있을 때

Firebase Cloud Messaging for Android

+0

이 자습서는 앱이 포 그라운드에있는 경우에만 작동합니다. 응용 프로그램이 백그라운드에 있거나 시작되지 않은 경우에도 NotificationActivity를 열고 싶습니다. – Shekhar

0

() 메소드 :

private void showNotification(String message){ 
    Intent intent=new Intent(this, NotificationActivity.class); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    intent.putExtra("key", messageBody); 
    PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT); 

    NotificationCompat.Builder builder=new NotificationCompat.Builder(this) 
      .setAutoCancel(true) 
      .setContentTitle("FCM Sample") 
      .setContentText(message) 
      .setLargeIcon(icon2) 
      .setContentIntent(pendingIntent); 
    NotificationManager manager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    manager.notify(0,builder.build()); 

} 
관련 문제