PendingIntent.getActivities & Intent 数组

前言

点击通知栏直接打开App某个页面,点击返回回到桌面;而需求是点击返回回到首页,也就是要求一次打开多个页面(Activity),实现如下:

解决方案:

PendingIntent.getActivities 方法支持包含多个Intent元素的数组,因为用的Kotlin,所以费了点功夫,记录如下:

// 直接要打开的Intent放到最后,其余的依次倒序放置,你懂的。先打开先被覆盖。
 val intents:Array = arrayOf(
                    Intent(context, MainActivity::class.java),
                    Intent(context, NoticeActivity::class.java)
            )

val pendingIntent = PendingIntent.getActivities(context, 1, intents, PendingIntent.FLAG_CANCEL_CURRENT)

仅按照上述设置还是不行的,因为MainActivity有可能会被重复创建,这时候你想到了什么?

        

需要设置会被重复创建的 Activity 的 launchMode="singleTask"

附通知栏代码:

class CustomNotificationReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {
        val action = context.getPackageName() + NimIntent.ACTION_RECEIVE_CUSTOM_NOTIFICATION
        if (action == intent.action) {
            // 从 intent 中取出自定义通知, intent 中只包含了一个 CustomNotification 对象
            val data = intent.getSerializableExtra(NimIntent.EXTRA_BROADCAST_MSG) as CustomNotification
            showNotification(context, data, setPendingIntent(context, data))
        }
    }

    //  PendingIntent 匹配策略
    fun setPendingIntent(context: Context, data: CustomNotification): PendingIntent {

        val intents: Array = when (data.sessionType) {
            SessionTypeEnum.P2P -> arrayOf(Intent(context, MainActivity::class.java), Intent(context, P2pActivity::class.java))
            SessionTypeEnum.Team -> arrayOf(Intent(context, MainActivity::class.java), Intent(context, TeamActivity::class.java))
            SessionTypeEnum.System -> arrayOf(Intent(context, MainActivity::class.java), Intent(context, NoticeActivity::class.java))
            else -> arrayOf(Intent(context, MainActivity::class.java))  // 其余情况目前 只打开首页
        }

        return PendingIntent.getActivities(context, 1, intents, PendingIntent.FLAG_CANCEL_CURRENT)
    }

    //  显示通知栏
    fun showNotification(context: Context, data: CustomNotification, pendingIntent: PendingIntent) {

        val notificationManager = (context as Context).getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager?
        val notificationId = 0x1234
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            var mChannel = NotificationChannel("1", "my_channel_01" as CharSequence, NotificationManager.IMPORTANCE_DEFAULT)
            mChannel.enableLights(true)
            mChannel.setLightColor(Color.RED);
            mChannel.enableVibration(true);
            notificationManager!!.createNotificationChannel(mChannel)

            val builder = Notification.Builder(context, "1")
            builder.setSmallIcon(android.R.drawable.stat_notify_chat)
                    .setContentTitle(data.fromAccount)
                    .setContentText(data.content[0].toString())
                    .setContentIntent(pendingIntent)
                    .setAutoCancel(true)
                    .setNumber(3) //久按桌面图标时允许的此条通知的数量
            notificationManager!!.notify(notificationId, builder.build())

        } else {
            TODO("VERSION.SDK_INT < O")
        }
    }
}

你可能感兴趣的:(PendingIntent.getActivities & Intent 数组)