单击通知时如何打开MainActivity以外的其他活动

问题描述 投票:0回答:1

我可以从console.firebase.google发送推送通知但是,当我点击已发送的通知时,我希望将其重定向到Main2Activity而不是MainActivity,当应用程序位于前台时,我能够实现此目的但不在后台,感谢任何帮助。

public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "FCM Service";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Intent detailIntentForToday = new Intent(this, Main2Activity.class);
    TaskStackBuilder taskStackBuilder = 
    TaskStackBuilder.create(getApplicationContext());
    taskStackBuilder.addNextIntentWithParentStack(detailIntentForToday);
    PendingIntent resultPendingIntent = taskStackBuilder
            .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    Notification notification = new Notification.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(getString(R.string.app_name))
            .setAutoCancel(true)
            .setPriority(Notification.PRIORITY_MAX)
            .setDefaults(Notification.DEFAULT_VIBRATE)
            .setContentIntent(resultPendingIntent)
            .setContentText("Text")
            .build();
    NotificationManager notificationManager = (NotificationManager) 
    getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, notification);
    }
}
java android firebase
1个回答
0
投票

尝试在Notification Builder中设置setContentIntent()您的代码就像

public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "FCM Service";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Intent detailIntentForToday = new Intent(this, Main2Activity.class);
    TaskStackBuilder taskStackBuilder = 
    TaskStackBuilder.create(getApplicationContext());
    taskStackBuilder.addNextIntentWithParentStack(detailIntentForToday);
    PendingIntent resultPendingIntent = taskStackBuilder
            .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    Notification notification = new Notification.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(getString(R.string.app_name))
            .setContentIntent(createContentIntent())
            .setAutoCancel(true)
            .setPriority(Notification.PRIORITY_MAX)
            .setDefaults(Notification.DEFAULT_VIBRATE)
            .setContentIntent(resultPendingIntent)
            .setContentText("Text")
            .build();
    NotificationManager notificationManager = (NotificationManager) 
    getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, notification);
    }
}

private PendingIntent createContentIntent() {
        Intent openUI = new Intent(this,Main2Activity.class);
        openUI.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        return PendingIntent.getActivity(
                this, REQUEST_CODE, openUI, PendingIntent.FLAG_CANCEL_CURRENT);
    }
© www.soinside.com 2019 - 2024. All rights reserved.