簡體   English   中英

如何在片段中打開活動頁面,當按下 android 中的通知時

[英]How to open activity page in fragment, when pressed a notification in android

我有 PushNotificationService 和我的方法 onMessageReceived。

如果我在程序中,則會從上方發出通知並將我定向到我想要的頁面。

但是,當程序關閉時,當我在后台點擊通知時,它並沒有到我想要的頁面,它只到主頁。

當我點擊通知時,我想打開活動。 我能怎么做?

public class PushNotificationService extends FirebaseMessagingService {

    private NotificationCompat.Builder builder;

    @SuppressLint("NewApi")
    @Override
    public void onMessageReceived(@NonNull RemoteMessage message) {

        String baslik = message.getNotification().getTitle();
        String icerik = message.getNotification().getBody();
        durumaBagli(baslik,icerik);
        
    }
    private void durumaBagli(String baslik, String icerik) {

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Intent intent = new Intent(this,BildirimGonder.class);

         PendingIntent pendingIntent = PendingIntent
                .getActivity(this,1,intent,
                        PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){

            String kanalid = "kanalId";
            String kanalAd = "kanalAd";
            String kanalTanim = "kanalTanim";

            int kanalOnceligi = NotificationManager.IMPORTANCE_HIGH;

            NotificationChannel kanal = notificationManager.getNotificationChannel(kanalid);

            if (kanal == null){
                kanal = new NotificationChannel(kanalid,kanalAd,kanalOnceligi);
                kanal.setDescription(kanalTanim);
                notificationManager.createNotificationChannel(kanal);
            }

            builder = new NotificationCompat.Builder(this,kanalid);
            builder.setContentTitle(baslik);
            builder.setContentText(icerik);
            builder.setSmallIcon(R.drawable.gurpinarlogom);
            builder.setAutoCancel(true);
            builder.setContentIntent(pendingIntent);
        }else {
            builder = new NotificationCompat.Builder(getApplicationContext());
            builder.setContentTitle(baslik);
            builder.setContentText(icerik);
            builder.setSmallIcon(R.drawable.gurpinarlogom);
            builder.setAutoCancel(true);
            builder.setContentIntent(pendingIntent);
            builder.setPriority(Notification.PRIORITY_HIGH);
        }

        notificationManager.notify(1,builder.build());
    }
}

還有我的 MainActivity

public class MainActivity extends AppCompatActivity {

    private BottomNavigationView bottomNav;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
                     
        bottomNav = findViewById(R.id.bottomNav);
        NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager()
                .findFragmentById(R.id.nav_host_fragment);
        NavigationUI.setupWithNavController(bottomNav, navHostFragment.getNavController());
        getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#01A2D6")));
    }
    public void setActionBarTitle(String title) {
        getSupportActionBar().setTitle(title);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.toolbar_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_iletisim:
                Intent mesajGonder = new Intent(getApplicationContext(), MesajGonder.class);
                startActivity(mesajGonder);
                Toast.makeText(this, "İletişim sayfası", Toast.LENGTH_SHORT).show();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

這是我的代碼

像這樣把這個標志放到你的意圖中:intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

根據這篇文章,當應用程序處於foreground/background/killed時,您的 FCM 類型的消息可能不會觸發onMessageReceived() function 。

確保 FCM 消息類型為Data Messages

查看帖子,因為它包含有關如何正確設置 FCM 以便通知在應用程序處於foreground/background/killed已終止時觸發onMessageReceived()的詳細信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM