点击通知后如何打开特定屏幕?

时间:2019-04-19 09:10:59

标签: android kotlin flutter android-notifications

当前,当在最小化Flutter应用程序的情况下,当用户收到通知并点击通知时,它将每次将用户重定向到screen A。但是我希望用户通过手动创建的Deeplink进入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)
        }
    }
}

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

3 个答案:

答案 0 :(得分:0)

@ Dk15您正在做正确的工作,对于特定的重定向,您必须从服务器发送数据中的事件,例如, 在创建通知时,发送一个类似于JOB_ACCEPTED的事件, 因此,您可以在on create上检查
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; }

答案 1 :(得分:0)

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

  

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

逐步遵循this,您将得到答案。

或者您可以 尝试this

答案 2 :(得分:0)

尝试一下:

where not exists