在firebase通知点击打开片段

时间:2017-12-28 16:37:34

标签: android firebase android-fragments android-intent

晚上好,

我正在尝试在firebase通知到达时打开特定片段(orderdetails)并保持在整个应用中使用orderid的值。但现在当我点击通知时,它会打开mainFragment。

MainActivity:pastebin.com/Lx0hEuq3

MyFCMService:pastebin.com/jUjZ3kYm

orderDetailsfragment:pastebin.com/enMT8Wea

1 个答案:

答案 0 :(得分:0)

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;
    }



}