2016-11-24 2 views
-1

Мой Firebasemessagingservice код класса для передачи намерения:Как открыть фрагмент на нажатие нажимной уведомления

private void showNotification(String message){ 
    Intent intent=new Intent(this, DrawerActivity.class); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    intent.putExtra("data", message); 
    PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT); 

    NotificationCompat.Builder builder=new NotificationCompat.Builder(this) 
      .setAutoCancel(true) 
      .setContentTitle("Registry") 
      .setContentText(message) 
      .setSmallIcon(R.drawable.com_facebook_button_send_icon_blue) 
      .setContentIntent(pendingIntent); 
    NotificationManager manager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    manager.notify(0,builder.build()); 
} 

Мой фрагмент BaseActivity код в OnCreate() метод:

Intent notifyIntent = getIntent(); 
    Bundle extras = notifyIntent.getExtras(); 
    if (extras != null) { 

     replacefragment code; 

    } 

Это не работает.

+0

PLS сообщение заменить фрагмент кода также. И отредактируйте вопрос, чтобы правильно упомянуть, что вы хотите достичь. –

+0

В чем проблема, задайте вопрос ... см. [Ask] и объясните вам проблему. – AxelH

+0

Переопределите метод 'NewIntent()' в своей деятельности и вычеркните фрагмент из него. – Piyush

ответ

2

Вам необходимо отправить ключ с данными намерений и проверить с помощью этого ключа и выполнить код, как

private void showNotification(String message){ 
    Intent intent=new Intent(this, DrawerActivity.class); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    intent.putExtra("data", message); 
    intent.putExtra("KEY", "YOUR VAL"); 
    PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT); 

    NotificationCompat.Builder builder=new NotificationCompat.Builder(this) 
      .setAutoCancel(true) 
      .setContentTitle("Registry") 
      .setContentText(message) 
      .setSmallIcon(R.drawable.com_facebook_button_send_icon_blue) 
      .setContentIntent(pendingIntent); 
    NotificationManager manager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    manager.notify(0,builder.build()); 
} 

и Chech на активности как

Intent notifyIntent = getIntent(); 
    String extras = getIntent().getExtraString("KEY");; 
    if (extras != null&&extras.equals("YOUR VAL")) {`enter code here` 

     replacefragment code; 

    } 

также проверить onIntent изменить сюда, если деятельность уже открыта

Смежные вопросы