【问题标题】:How to open fragment with Firebase push notification?如何使用 Firebase 推送通知打开片段?
【发布时间】:2016-10-03 13:23:30
【问题描述】:

我正在构建一个使用 Firebase 推送通知的应用程序。我在 MainActivity(Fragment1,Fragment2,Fragment3)上有 3 个片段。当用户单击推送通知时,我想将他发送到 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 的什么位置。 请帮忙。

【问题讨论】:

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


    【解决方案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
    
       }
    

    【讨论】:

    • 我可以在 MyActivity 的 OnCreate 中完成所有这些吗??
    • 是的,你可以.. 实际上,刚刚意识到我误读了原来的问题。抱歉,您需要为通知设置待处理的意图。此意图将针对您的主要活动,在其中添加一个额外的内容以告诉您您想要哪个片段。在您的主要活动中检索额外内容,如果它来自通知,则转到正确的片段。
    • 但是我有一个 Pending Intent :) 但它不想提供选项 pendindIntent.putExtra.. 放什么?
    • sendNotification - intent.putExtra("action","gotoFragment1");然后在 setOnClicks Bundle extras = getIntent().getExtras() 之后的 MainActivity; if (extras.getString("action").contentContains("gotoFragement1"){ //切换到fragment1 }
    • 我没有选项 .contentContains("gotoFragment1") 只是 .contains("gotoFragment1")..我希望那是一样的...以及如何切换到 fragment1?抱歉打扰你,这是我的最后一个问题:)
    【解决方案2】:

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

    【讨论】:

    • 我较新的使用本地广播接收器。请你把那几行写给我好吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多