android-fragments 点击Firebase通知时打开片段

kwvwclae  于 2022-11-13  发布在  Android
关注(0)|答案(1)|浏览(145)

我尝试在Firebase通知到达时打开特定片段(orderdetails),并保留orderid的值以在整个应用程序中使用。但现在当我单击通知时,它会打开mainFragment。
主要活动:pastebin.com/Lx0hEuq3
我的FCM服务:pastebin.com/jUjZ3kYm
订单详细信息片段:pastebin.com/enMT8Wea

nhjlsmyf

nhjlsmyf1#

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    public static boolean message_received = false;

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        message_received = false;
        send_Notification(remoteMessage);

    }

    private void send_Notification(RemoteMessage remoteMessage) {

        JSONObject response = null;
        String params = remoteMessage.getNotification().getBody();
        try {
            response = new JSONObject(params);
        } catch (Exception e) {
        }
        String title = remoteMessage.getNotification().getTitle();


        if (title.equalsIgnoreCase("order")) {
            message_received = true;
            CustomNotification(title,response.optString("message"));

        } else if (title.equalsIgnoreCase("Discount Code")) {
            message_received = true;
            CustomNotification(title,params);

        }


    }


    public void CustomNotification(String title, String text) {

        RemoteViews remoteViews = new RemoteViews(getPackageName(),
                R.layout.custom_notification_layout);

                getSupportFragmentManager().beginTransaction().replace(R.id.container_body, new HomeFragment()).addToBackStack(null).commit();

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setAutoCancel(false)
                .setContentIntent(pIntent)
                .setContent(remoteViews);

        remoteViews.setTextViewText(R.id.title,getString(R.string.app_name) + " " + title + " " + getString(R.string.str_notification));
        remoteViews.setTextViewText(R.id.text,text);
        NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationmanager.notify(random(), builder.build());

    }

    int random() {
        Random rand = new Random();
        int randomNum = 1 + rand.nextInt((100000 - 1) + 1);
        return randomNum;
    }


}

相关问题