首页 > 解决方案 > open wrong App via Notification click in same Android device

问题描述

I am develop in Android Notification.

I create the own notification like the following.

const val NotifiTAG = "NotiTag"
val mNotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

        //random request code
        val randomId = List(3) { badge + Random().nextInt(badge+99999) }

        val intentView = Intent(this, Main2Activity::class.java)
        repIntent = PendingIntent.getActivity(this, randomId[0], intentView, PendingIntent.FLAG_UPDATE_CURRENT)


        val builder = NotificationCompat.Builder(applicationContext,NOTIFICATION_CHANNEL)
                .setContentTitle(title)
                .setContentText(description)
                .setAutoCancel(true)
                .setContentIntent(repIntent)
                .setDefaults(Notification.DEFAULT_VIBRATE)
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)


        //Set channel , when SDK >= 26
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            //Set Channel
            val channel = NotificationChannel(NOTIFICATION_CHANNEL, "ubauth",
                    NotificationManager.IMPORTANCE_HIGH)
            channel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
            channel.enableVibration(true)
            channel.setShowBadge(true)
            mNotificationManager.createNotificationChannel(channel)
        }



        val notification = builder.build()
        ShortcutBadger.applyNotification(applicationContext, notification, badge)
        mNotificationManager.notify(NotifiTAG,badge, notification)

The above code work fine for receive Notification, and it can open the App when click notification when only one App in Android device.

When I install two identical App with different build types and different applicationId. The notification open the wrong App when I click the notification .

Did i missing something ? Thanks in advance.

标签: androidandroid-notifications

解决方案


尝试将特定应用的组件名称添加到您的意图中

 Intent intent = new Intent();
 intent.setComponent(new ComponentName("com.your.app", 
 "com.your.app.Main2Activity"));

推荐阅读