如何在不与MainActivity交互的情况下从通知中打开片段页面?

问题描述 投票:0回答:2

我已编写代码来创建通知。

我希望通知直接打开一个名为“StepsFragment”的片段类。

但是,每当我点击通知时,都没有任何反应。这可能是什么问题?

我在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

他们导致我遇到语法错误,因此我无法构建我的项目。

这是我用来从通知中打开片段的代码。

    @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;
    }

如何打开通知,以便在点击通知时将它带到Step Fragment而不是做任何事情?

android android-intent notifications fragment
2个回答
0
投票

你不能直接从Fragment打开Intent - 片段附加到Activity,所以你需要调用一个Activity然后托管你的片段。

有几种方法可以实现这一目标。例如,你可以将StepsFragment移动到它自己的Activity,然后使用你的Intent来调用Activity。或者,您可以向Intent添加额外内容,调用MainActivity,并在Activity中使用该额外内容作为提示,它应该只显示StepsFragment


0
投票

这些是我在点击通知时用来打开fragment类的代码。

注意:您需要调用Activity来调用您的片段类。

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)

    }
}

MainActivtiy中,覆盖onNewIntent函数

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

这里的receiveIntent功能

fun receiveIntent(intent: Intent?) {
      // open your fragment class here
   }
© www.soinside.com 2019 - 2024. All rights reserved.