2016-11-22 83 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 
    } 
相关问题