【问题标题】:how to start commit Fragment in FirebaseMessagingService如何在 FirebaseMessagingService 中开始提交片段
【发布时间】:2017-03-16 11:15:47
【问题描述】:

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

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());
    }
}

【问题讨论】:

    标签: android android-fragments firebase android-notifications firebase-cloud-messaging


    【解决方案1】:

    您应该广播任何 firebase 消息更新并在您的活动中注册处理该更新(您应该在其中进行片段事务)的广播接收器。如果您的活动在前台,这意味着幸运的是您的接收器已注册。如果要在 firebase 服务上下文中添加片段,可以实现自己的 android.app.Application.ActivityLifecycleCallbacks 接口,如

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

    并从您的 ActivityLifeCycleHandler 类中获取您的活动参考。(注意不要泄漏活动)。我不会推荐这个解决方案。

    这个回调是为你的应用实例注册的,它的回调(onResume())由Activity生命周期方法(super.onResume()触发) 用于您应用程序中的每个活动。

    【讨论】:

      【解决方案2】:

      getFragmentManager()Activity 的方法,因此不能在Service 中工作。

      【讨论】:

      【解决方案3】:

      您无法在服务类中获得对 getFragmentManager() 的引用 您可以如何使用广播在应用程序的其他任何地方收听呼叫。

      【讨论】:

        猜你喜欢
        • 2013-07-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-08
        • 1970-01-01
        相关资源
        最近更新 更多