【问题标题】:How to open specific screen after tapping on notification?点击通知后如何打开特定屏幕?
【发布时间】:2019-04-19 09:10:59
【问题描述】:

当前,当用户在 Flutter 应用程序最小化时收到通知并点击通知时,它每次都会将用户重定向到 screen A。但我希望用户通过手动创建的深度链接登陆让我们说screen C。此外,如果用户从任何屏幕最小化应用程序,点击通知会将用户带到该屏幕。但无论用户从何处最小化应用程序或应用程序何时处于后台,点击通知应始终将用户重定向到screen C。我之前没有实现通知,所以这是我第一次处理这个问题,所以在这方面寻求帮助。通知类代码如下:

companion object {

    const val CHAT_REQUEST_NOTIFICATION_ID = 1775
    const val CHAT_CHANNEL_ID = "com.example.com.CHAT_CHANNEL_ID"
    const val CHAT_CHANNEL_NAME = "Demo Notifications"

    fun showChatNotification(context: Context, userName: String?, body: String?) {

        createChatNotificationChannel(context);

        val chatIntent = Intent();
        val deeplink = generateDeepLink(userName);
        chatIntent.setAction(Intent.ACTION_VIEW);
        chatIntent.setData(Uri.parse(deeplink));
        val chatPendingIntent = PendingIntent.getActivity(context, 100, chatIntent, PendingIntent.FLAG_ONE_SHOT)

        val notification: Notification

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

            notification = Notification.Builder(context, CHAT_CHANNEL_ID)
                .setContentIntent(chatPendingIntent)
                .setSmallIcon(R.drawable.notification_icon)
                .setLargeIcon(BitmapFactory.decodeResource(context.resources, R.drawable.ic_app_icon))
                .setAutoCancel(true)
                .setContentTitle("Message")
                .setContentText(body)
                .setOngoing(false)
                .setVisibility(Notification.VISIBILITY_PUBLIC)
                .build()
        } else {
            notification = Notification.Builder(context)
                .setContentIntent(chatPendingIntent)
                .setSmallIcon(android.R.drawable.btn_star)
                .setLargeIcon(BitmapFactory.decodeResource(context.resources, R.mipmap.app_icon))
                .setAutoCancel(true)
                .setVibrate(chatVibrationPattern)
                .setVisibility(Notification.VISIBILITY_PUBLIC)
                .setContentTitle("Message")
                .setOngoing(false)
                .setContentText(body)
                .build()
        }
        val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        manager.notify(CHAT_REQUEST_NOTIFICATION_ID, notification)
    }

    fun generateDeepLink(userId: String?): String {
        return "https://demo.page.link/?link=https://demo.com/chat?user=$userId&apn=com.example.com&efr=1";
    }

    private fun createChatNotificationChannel(context: Context) {

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

            val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

            notificationManager.deleteNotificationChannel(CHAT_CHANNEL_ID)

            val importance = NotificationManager.IMPORTANCE_HIGH
            val notificationChannel = NotificationChannel(CHAT_CHANNEL_ID, CHAT_CHANNEL_NAME, importance)
            notificationChannel.description = "Message"
            notificationChannel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
            notificationManager.createNotificationChannel(notificationChannel)
        }
    }
}

我正在使用安卓模拟器 (6.0)。

【问题讨论】:

    标签: android kotlin flutter android-notifications


    【解决方案1】:

    @Dk15 您正在做正确的工作,您必须为特定重定向从服务器发送数据中的事件,例如, 在创建通知时发送类似 JOB_ACCEPTED 的事件, 所以你可以检查你的创建
    if (getIntent().getExtras() != null)
    然后重定向到switch语句并检查你收到了哪个事件,然后将用户重定向到你想要的任何地方
    switch (getIntent().getStringExtra("EVENT_TYPE")){ Match your case here and redirect user in your case to SCREEN C case "JOB_ACCEPTED": OPEN SCREEN C; break; }

    【讨论】:

      【解决方案2】:

      试试这个:

           /**Creates an explicit intent for an Activity in your app**/
          Intent resultIntent = new Intent(mContext , SomeOtherActivity.class);
          resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      
          PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext,
                  0 /* Request code */, resultIntent,
                  PendingIntent.FLAG_UPDATE_CURRENT);
      
          mBuilder = new NotificationCompat.Builder(mContext);
          mBuilder.setSmallIcon(R.mipmap.ic_launcher);
          mBuilder.setContentTitle(title)
                  .setContentText(message)
                  .setAutoCancel(false)
                  .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
                  .setContentIntent(resultPendingIntent);
      
          mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
      
          if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
          {
              int importance = NotificationManager.IMPORTANCE_HIGH;
              NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
              notificationChannel.enableLights(true);
              notificationChannel.setLightColor(Color.RED);
              notificationChannel.enableVibration(true);
              notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
              assert mNotificationManager != null;
              mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
              mNotificationManager.createNotificationChannel(notificationChannel);
          }
          assert mNotificationManager != null;
          mNotificationManager.notify(0 /* Request Code */, mBuilder.build());
      }
      

      【讨论】:

        【解决方案3】:

        我没有发现您提到要打开的活动。

        val resultIntent = Intent(this, ResultActivity::class.java)

        一步一步关注this,你会得到答案。

        或者你可以 试试this

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-07-27
          • 2020-10-15
          • 2015-08-18
          • 1970-01-01
          • 1970-01-01
          • 2021-04-25
          • 2018-07-02
          • 2019-12-01
          相关资源
          最近更新 更多