0

我正在构建一个带有片段的应用程序,并且我正在使用 Firebase 通知。我希望,当用户单击通知时,将他发送到 MainActivity 上的 Fragment1。而且,我已经做到了,但问题是只有当应用程序处于前台时才有效。当应用程序处于后台通知时,会将我发送到 MainActivity 而不是 Fragment1。这是 MyFirebaseMessagingService:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    Log.d(TAG, "From " + remoteMessage.getFrom());
    Log.d(TAG, "Body " + remoteMessage.getNotification().getBody());
    sendNotification(remoteMessage);
    Log.d("Msg", "Poruka je stigla");
}
private void sendNotification(RemoteMessage remoteMessage){
    Intent intent=new Intent(myFirebaseMessagingService.this, MainActivity.class);
    intent.putExtra("action", "goToFragment1");
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent=PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notification=new NotificationCompat.Builder(this)
            .setSmallIcon(logo)
            .setContentText(remoteMessage.getNotification().getBody())
            .setContentTitle("Naslov")
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);
    NotificationManager notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, notification.build());



}}

这是我的 MainActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    dugme=(Button)findViewById(R.id.dugme1);
    dugme2=(Button)findViewById(R.id.subscribe);
    dugme.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Fragment fragment = null;
            if(view==dugme) {
                fragment = new Fragment1();
            }
            FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.myFragment, fragment);
            transaction.addToBackStack(null);
            transaction.setTransition(FragmentTransaction.TRANSIT_NONE);
            transaction.commit();
        }
    });
    String msg=getIntent().getStringExtra("action");
    FragmentManager fragmentManager=getSupportFragmentManager();
    FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
    if (msg!=null){
        if (msg.equals("goToFragment1")){
            Fragment1 fragment1=new Fragment1();
            fragmentTransaction.replace(R.id.myFragment, fragment1);
            fragmentTransaction.commit();
        }
    }

    dugme2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            FirebaseMessaging.getInstance().subscribeToTopic("android");
            Log.d("Log", "Uspesno ste se pretplatili");
        }
    });

}

我应该怎么办?

4

2 回答 2

1

当您的应用程序处于后台时,onNewIntent将调用而不是onCreate.

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    String msg=intent.getStringExtra("action");
    FragmentManager fragmentManager=getSupportFragmentManager();
    FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
    if (msg!=null){
       if (msg.equals("goToFragment1")){
           Fragment1 fragment1=new Fragment1();
           fragmentTransaction.replace(R.id.myFragment, fragment1);
           fragmentTransaction.commit();
       }
    }
}
于 2016-06-07T12:57:46.273 回答
0

您可以在片段中使用 BroadCastReceiver。 https://developer.android.com/reference/android/content/BroadcastReceiver.html?hl=ru

尝试创建这样的东西:

覆盖 onResume 方法

IntentFilter filter = new IntentFilter();
filter.addAction("SOME_STRING_CONSTANT");
getActivity().registerReceiver(receiver, filter);

并在 onDestroy 方法中取消注册您的接收器。现在您的广播接收器可以在后台工作

编辑

private BroadcastReceiver receiver = new BroadcastReceiver() {

  @Override
  public void onReceive(Context context, Intent intent) {
    if (intent.getAction() == "SOME_STRING_CONSTANT") {
        // do something
    } 
  }
};

@Override
public void onDestroyView() {
   super.onDestroyView();
   getActivity().unregisterReceiver(receiver);
}

您可以从任何地方向广播接收器发送事件

Intent intent = new Intent();
intent.setAction("SOME_STRING_CONSTANT");
startActivity(intent);
于 2016-06-07T13:15:27.107 回答