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

时间:2019-03-29 05:43:45

标签: android android-intent notifications fragment

我已经编写了创建通知的代码。

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

但是,每当我单击通知时,都不会发生任何事情。 这里可能是什么问题?

我已经在堆栈溢出中尝试了以下解决方案:

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

如何打开通知,以便在我点击通知时将其带到StepsFragment而不执行任何操作?

2 个答案:

答案 0 :(得分:0)

您不能直接从Fragment打开Intent-片段被附加到Activity上,因此您将需要调用一个Activity来托管您的片段。

有几种方法可以实现此目的。作为示例,您可以将StepsFragment移到其自己的Activity中,然后使用Intent来调用该Activity。或者,您可以在Intent中添加一个附加项,调用MainActivity,并在Activity中使用该附加项以提示它仅应显示StepsFragment

答案 1 :(得分: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
   }