当应用程序在后台运行时,从通知到片段? [英] From notification to fragment when app is in the background?

查看:63
本文介绍了当应用程序在后台运行时,从通知到片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am building an app with fragments and I am using Firebase notifications. I want, when user click on notification, to send him to the Fragment1 on MainActivity. And, I've done it, but the problem is that works only when app is in the foreground. When app is in background notification sends me to the MainActivity not to the Fragment1. This is 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:

And this is my 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");
        }
    });

}

我该怎么办?

推荐答案

当应用在后台运行时,将调用 onNewIntent 而不是 onCreate .

When your app is in background onNewIntent will be called instead of 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();
       }
    }
}

这篇关于当应用程序在后台运行时,从通知到片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆