2016-11-22 43 views
1

我正在使用firebase發送通知。當用戶點擊通知時,它將打開ResultActivity。當應用處於前臺時它工作正常。但是,當應用程序處於後臺時,它會打開HomeActivity(這是應用程序的開端活動)而不是ResultActivity。我不明白這是什麼問題?爲什麼PendingIntent會打開應用程序的啓動器活動而不是特定的活動?

public class MyFirebaseMessagingService extends FirebaseMessagingService { 

    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this); 
     notificationBuilder.setContentTitle(getResources().getString(R.string.app_name)); 
     notificationBuilder.setContentText(remoteMessage.getNotification().getBody()); 
     notificationBuilder.setAutoCancel(true); 
     notificationBuilder.setSmallIcon(R.mipmap.ic_launcher); 

     Intent intent = new Intent(getApplicationContext(), ResultActivity.class); 

     PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_ONE_SHOT); 

     notificationBuilder.setContentIntent(pendingIntent); 
     NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     notificationManager.notify(0, notificationBuilder.build()); 

    } 
} 
+3

這個確切的問題,昨天最後問,做一些搜索你問了一個新問題之前 –

+0

http://stackoverflow.com/questions/40718155/open-specific-activity-when-notification-clicked-in-fcm –

+1

看一看[這裏](http://stackoverflow.com/questions/37407366/firebase-fcm-notifications-click-action-payload)。它可能會幫助你。 –

回答

0

這是測試click_action映射的好方法。它需要一個意圖過濾器像在FCM文檔中指定的一個:

<intent-filter> 
    <action android:name="OPEN_ACTIVITY_1" /> 
    <category android:name="android.intent.category.DEFAULT" /> 
</intent-filter> 

此外,還要記住,如果這個應用程序是在後臺纔有效。如果它位於前臺,則需要實施FirebaseMessagingService的擴展。在onMessageReceived方法,您將需要手動瀏覽到您的click_action目標

@Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
    //This will give you the topic string from curl request (/topics/news) 
    Log.d(TAG, "From: " + remoteMessage.getFrom()); 
    //This will give you the Text property in the curl request(Sample  Message): 
    Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody()); 
    //This is where you get your click_action 
    Log.d(TAG, "Notification Click Action: " + remoteMessage.getNotification().getClickAction()); 
    //put code here to navigate based on click_action 
    } 
相關問題