【问题标题】:Open fragment on firebase notification click在firebase通知点击打开片段
【发布时间】:2017-12-28 16:37:33
【问题描述】:

晚上好,

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

主活动:pastebin.com/Lx0hEuq3

MyFCMService : pastebin.com/jUjZ3kYm

orderDetailsfragment : pastebin.com/enMT8Wea

【问题讨论】:

    标签: android firebase android-fragments android-intent


    【解决方案1】:
    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;
        }
    
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-15
      • 1970-01-01
      • 2016-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多