【问题标题】:How do I open a fragment page from notification without interacting with MainActivity?如何在不与 MainActivity 交互的情况下从通知中打开片段页面?
【发布时间】:2019-08-20 00:35:34
【问题描述】:

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

我希望通知直接打开一个名为“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;
    }

如何打开通知,以便在我点击通知时将我带到 StepsFragment 而不是什么都不做?

【问题讨论】:

    标签: android android-intent notifications fragment


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      这些是我在点击通知时用来打开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
         }
      

      【讨论】:

        猜你喜欢
        • 2014-12-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-26
        • 2021-06-26
        相关资源
        最近更新 更多