2016-12-07 34 views
3

在Firebase onMessageReceived(RemoteMessage remoteMessage)中,當您的應用程序位於Foreground時,將調用此方法。因此,點擊通知後,您可以打開另一個活動,例如NotiicationActivityFirebase onMessageReceived(RemoteMessage remoteMessage)不在應用程序處於後臺時調用

但是如果你的應用程序在後臺,那麼這個方法將不會被調用,並且在點擊通知時只會打開一個啓動器活動。

那麼,即使我們的應用程序在後臺,如何在點擊通知時打開NotificationActivity

我的代碼是這樣的:

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

閱讀這樣的回答:sloved問題[這裏是鏈接](http://stackoverflow.com/questions/ 40626233/firebase-onmessagereceived-not-called-when-app-is-inactive/40626866#40626866) –

回答

3

onMessageReceived時纔會觸發。 如果該應用程序是後臺,您仍然可以收到通知,但不會觸發onMessageReceived

所以我的建議, 在接收活動,你仍然可以通過使用得到通知的數據:

getIntent().getExtras(); 

這應該相應工作。希望這有助於:)

+0

感謝這項工作,我只是這樣做。 'if(getIntent()。getExtras()!= null){ //我在這裏寫了。 Intent intent = new Intent(MainActivity.this,NotificationActivity.class); startActivity(intent); }' – Shekhar

+0

不用擔心@Shekhar,請將其投票,以便其他人可能知道此解決方案的工作原理。謝謝。 – kenix

0

嘗試,並遵循以下鏈接:當應用程序在前臺

Firebase Cloud Messaging for Android

+0

本教程僅在您的應用處於前景時纔有效。我想打開NotificationActivity,即使應用程序在後臺,甚至沒有啓動。 – Shekhar

0

在你sendNotification的()方法:

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()); 

} 
相關問題