如何使用Firebase推送通知打开片段?

时间:2016-06-03 12:57:11

标签: android android-fragments android-studio push-notification firebase

我正在构建一个应用程序,我正在使用Firebase推送通知。我在MainActivity上有3个片段(Fragment1,Fragment2,Fragment3)。当用户点击推送通知时,我想将他发送到Fragment1。你可以帮帮我吗? 这是我的MainActivity(不要注意注释行,这是我解决这个问题的不成功的努力)

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn1=(Button)findViewById(R.id.dugme1);
    btn2=(Button)findViewById(R.id.dugme2);
    btn3=(Button)findViewById(R.id.dugme3);
    dugme=(Button)findViewById(R.id.subscribe);
    dugme.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            FirebaseMessaging.getInstance().subscribeToTopic("developeri");
            Log.d(TAG, "Subscribed to developeri topic");
        }
    });

    Fragment newFra;
    String menuFragment=getIntent().getStringExtra("menuFragment");
    FragmentManager fm=getSupportFragmentManager();
    FragmentTransaction ft=fm.beginTransaction();

    StartFragment startFragment=new StartFragment();
    ft.add(R.id.myFragment, startFragment);
    ft.commit();
    btn1.setOnClickListener(btnOnClickListener);
    btn2.setOnClickListener(btnOnClickListener);
    btn3.setOnClickListener(btnOnClickListener);

}
 Button.OnClickListener  btnOnClickListener= new Button.OnClickListener() {
     @Override
     public void onClick(View view) {
         Fragment newFragment;
         if (view==btn1){
             newFragment =new Fragment1();

         }else if(view==btn2){
              newFragment =new Fragment2();
         }else if (view==btn3){
             newFragment =new Fragment3();
         }else{
             newFragment=new StartFragment();
         }
         FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
         transaction.replace(R.id.myFragment, newFragment);
         transaction.addToBackStack(null);
         transaction.setTransition(FragmentTransaction.TRANSIT_NONE);
         transaction.commit();


     }
 };

这是我的FCMessagingService

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Log.d("OnMessage", "Received");
    super.onMessageReceived(remoteMessage);
    Log.d(TAG, "From " + remoteMessage.getFrom());
    Log.d(TAG, "Body " + remoteMessage.getNotification().getBody());
    sendNotification(remoteMessage);
    Intent intent=new Intent(MyFireBaseMessagingService.this, MainActivity.class);
    intent.putExtra("menuFragment", "Fragment1");




    // remoteMessage.getNotification().getBody();

}




private void sendNotification(RemoteMessage remoteMessage) {

    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            //.setSmallIcon(R.mipmap.ic_launcher)
            .setSmallIcon(R.drawable.logo)
            .setContentText(remoteMessage.getNotification().getBody())
            .setContentTitle("ASP")
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

我知道我必须携带来自FCMessagingService的Intent,但我不知道将它放在MainActivity中的哪个位置。 请帮忙。

2 个答案:

答案 0 :(得分:1)

在发送通知中...

    Intent msgIntent = new Intent(context, mainActivity.class);
    Intent broadcast = new Intent(“broadcaster”);
    LocalBroadcastManager.getInstance(context).sendBroadcast(broadcast);

在MainActivity中......

    private BroadcastReceiver mReciever;

    LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver,
    new IntentFilter(“broadcaster”));


   mReceiver = new BroadcastReceiver() {
   @Override
   public void onReceive(Context context, Intent intent) {


   //code to switch to correct fragment

   }

答案 1 :(得分:0)

使用本地广播接收器。当您收到通知时,将触发本地广播。在您的活动中,当您收到本地广播时,请更改为正确的片段。