本文介绍了如何在FirebaseMessagingService中启动提交片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用FireBase可以在我的应用程序中进行消息传递,我希望当用户收到消息时,活动片段会发生变化.我为此做了以下代码,但我不知道为什么它会在getFragmentManager上给我错误,因为我没有活动上下文或类似的东西.

I used FireBase could messaging in my application and I want when the user receive message the activity fragment change. I did the following code for that but i don't know why it give me error on getFragmentManager that it because i don't have activity context or something like that.

public class googleFirebaseMessageService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        switch (remoteMessage.getData().get("message"))
        {
            case "invoices_ready":
                SharedPreferences preferences=getSharedPreferences("invoices_ready",MODE_PRIVATE);
                SharedPreferences.Editor editor=preferences.edit();
                editor.putString("pr_id",remoteMessage.getData().get("pr_id").toString());
                editor.commit();
                FragmentTransaction transaction = getFragmentManager().beginTransaction();


                transaction.replace(R.id.root_menu_fragment, new _step4_FragmentDrugInfo());
                transaction.addToBackStack("mapView");
                transaction.commit();
                showNotification(remoteMessage.getData().get("message"));
                break;
        }


    }

    private void showNotification(String messageBody) {
        Intent intent = new Intent( this , splash.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent resultIntent = PendingIntent.getActivity( this , 0, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri notificationSoundURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder mNotificationBuilder = new NotificationCompat.Builder( this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("Android Tutorial Point FCM Tutorial")
                .setContentText(messageBody)
                .setAutoCancel( true )
                .setSound(notificationSoundURI)
                .setContentIntent(resultIntent);

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

        notificationManager.notify(0, mNotificationBuilder.build());
    }
}

推荐答案

您应该广播任何Firebase消息更新,并在活动中注册处理该更新的广播接收器(您应该在其中进行分段交易) .如果您的活动在前景中,则表示您的接收者已注册.如果您想在Firebase服务上下文中添加片段,则可以实现自己的 android.app.Application.ActivityLifecycleCallbacks 接口,例如

You should broadcast any firebase message updates and register broadcast receiver that handles that updates(where you should do fragment transactions) in your activity. If your activity is in the foreground that means luckily your receiver is registered. If you want to add the fragment in firebase service context, you can implement your own android.app.Application.ActivityLifecycleCallbacks interface, like

public class ActivityLifeCycleHandler implements Application.ActivityLifecycleCallbacks { 
   Activity currentActivity;
   @Override
   public void onActivityResumed(Activity foregroundActivity) {
      this.currentActivity = foregroundActivity;
  }
}

并从您的ActivityLifeCycleHandler类获取您的活动引用.(请注意不要泄漏该活动).我不推荐这种解决方案.

And get your activity's reference from ActivityLifeCycleHandler class of yours.(be careful not to leak the activity) . I wouldn't recommend this solution thought.

此回调已为您的应用程序实例注册,其回调( onResume())由 Activity 生命周期方法( super.onResume())触发).

This callback is registered for your application instance and its callbacks(onResume()) are triggered by Activity life-cycle methods(super.onResume()) for every activity in your application.

这篇关于如何在FirebaseMessagingService中启动提交片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 18:54