0

I have written code to create a notification.

I would like the notification to directly open a fragment class called "StepsFragment".

However, whenever I click on the notification, nothing happens. What could be the issue here?

I have tried the following solutions on Stack Overflow:

Receive a notification issue and open a fragment

How to open current fragment from notification?

Open a dialog fragment from childfragment from Notification

They resulted in me encountering syntax errors and I'm unable to build my project as a result.

This is the code that I used to open up a fragment from the notification.

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String input = intent.getStringExtra("inputExtra");

        Intent notificationIntent = new Intent(this, StepsFragment.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,0,notificationIntent,0);

        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("Steps Tracker")
                .setContentText("Steps tracked : " + input )
                .setSmallIcon(R.drawable.steps)
                .setContentIntent(pendingIntent)
                .build();
        startForeground(1,notification);

        return START_NOT_STICKY;
    }

How do I open up the notification so that it will take me to StepsFragment instead of not doing anything when I tap on the notification?

Zoe
  • 27,060
  • 21
  • 118
  • 148
Jim Ng
  • 91
  • 1
  • 4
  • 15

2 Answers2

0

You cannot open a Fragment directly from an Intent - Fragments are attached to an Activity, so you will need to invoke an Activity which will then host your Fragment.

There are several ways you can achieve this. As examples, you could move StepsFragment into its own Activity, then use your Intent to invoke that Activity. Or, you can add an extra to the Intent, invoke MainActivity, and use that extra in your Activity as a hint that it should only display StepsFragment.

PPartisan
  • 8,173
  • 4
  • 29
  • 48
0

These are the code I used to open fragment class when tap on the notification.

Noted: You need to invoke Activity to call your fragment class.

class MyFirebaseMessagingService : FirebaseMessagingService() {
    val TAG = "FirebaseService"

    // will run when app is running foreground
    override fun onMessageReceived(remoteMessage: RemoteMessage) {

        if (remoteMessage.data.size > 0) {
            showNotification(remoteMessage.notification?.title, remoteMessage.notification?.body, remoteMessage.data)
        }
    }

    private fun showNotification(title: String?, body: String?, data: Map<String?, String>) {

        val now = Date()
        val id = Integer.parseInt(SimpleDateFormat("ddHHmmss", Locale.US).format(now))

        val intent = Intent(this, MainActivity::class.java)
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_NEW_TASK)
        val pendingIntent = PendingIntent.getActivity(
            this, id, intent,
            PendingIntent.FLAG_ONE_SHOT
        )

        val soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
        val notificationBuilder = NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(title)
            .setContentText(body)
            .setAutoCancel(true)
            .setSound(soundUri)
            .setPriority(Notification.PRIORITY_HIGH)
            .setContentIntent(pendingIntent)
            .setChannelId("xxx")

        .....

        val notification = notificationBuilder.build()
        notificationManager.notify(id, notification)

    }
}

In MainActivtiy, override onNewIntent function

override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        receiveIntent(intent)
    }

Here the receiveIntent function

fun receiveIntent(intent: Intent?) {
      // open your fragment class here
   }
John Joe
  • 12,412
  • 16
  • 70
  • 135