Android基础之Notification

2018-08-16  本文已影响41人  城南一霸賈十七

Android Notification 详解
具体代码实现

    val channel_id = "channel_id"
    val channel_name = "channel_name"
    val notificationID = 1
    internal lateinit var notificationManager: NotificationManager
// 发送通知
    fun sendNotifacation(view: View) {
        /**
         * 新建通知,必须的属性
         * smallIcon(小图标) contentTitle(通知标题) contentText(通知内容)
         */
        val builder = NotificationCompat.Builder(this, channel_id)
                .setSmallIcon(R.drawable.notification_icon)
                .setContentTitle("This is Notification title")
                .setContentText("This is Notification content,This is Notification content,This is Notification content")
                .setVisibility(Notification.VISIBILITY_PRIVATE)
        val notification = builder.build()
        notificationManager = applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        //适配Android8.0
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(channel_id, channel_name, NotificationManager.IMPORTANCE_HIGH)
            notificationManager.createNotificationChannel(channel)
        }
        notificationManager.notify(notificationID, notification)
    }

    // 取消通知
    fun cancleNotifacation(view: View) {
        notificationManager!!.cancel(notificationID)
    }

实现点击通知,跳转到相应的页面

class ForegroundService : Service() {

    companion object {
        private val TAG = ForegroundService::class.java.simpleName
    }

    val channel_id = "channel_id"
    val channel_name = "channel_name"
    val notificationID = 1

    @RequiresApi(Build.VERSION_CODES.JELLY_BEAN)
    override fun onCreate() {
        super.onCreate()
        Log.d(TAG,"onCreate()")
        val intent = Intent(this, MyAidlActivity::class.java)
        val pendingIntent = PendingIntent.getActivity(this, 0,
                intent,
                PendingIntent.FLAG_UPDATE_CURRENT)
        val builder = NotificationCompat.Builder(this, channel_id)
                .setContentText("content-text")
                .setContentTitle("content-title")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setWhen(System.currentTimeMillis())
                .setContentIntent(pendingIntent)
                .setFullScreenIntent(pendingIntent, true)
        val notificationManager = applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            notificationManager.createNotificationChannel(NotificationChannel(channel_id,
                    channel_name, NotificationManager.IMPORTANCE_HIGH))
        }
        val  notification = builder!!.build()
        notificationManager.notify(notificationID,notification)
    }

    override fun onBind(intent: Intent?): IBinder? {
        return null
    }

}

保留 Activity 返回栈

默认情况下,从通知启动一个Activity,按返回键会回到主屏幕。

但某些时候有按返回键仍然留在当前应用的需求,这就要用到TaskStackBuilder了。
1、在manifest中定义Activity的关系

<activity android:name=".service.MyAidlActivity"
            android:parentActivityName=".MainActivity" />

2、构建带返回栈的PendingIntent并发送通知

       // 构建返回栈
        val tsb = TaskStackBuilder.create(this)
        tsb.addParentStack(MyAidlActivity::class.java)
        tsb.addNextIntent(Intent(this, MyAidlActivity::class.java))
        // 构建包含返回栈的 PendingIntent
        val pendingIntent = tsb.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT)
        val builder = NotificationCompat.Builder(this, channel_id)
                .setContentText("content-text")
                .setContentTitle("content-title")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setWhen(System.currentTimeMillis())
                .setContentIntent(pendingIntent)
        val notificationManager = applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            notificationManager.createNotificationChannel(NotificationChannel(channel_id,
                    channel_name, NotificationManager.IMPORTANCE_HIGH))
        }
        val  notification = builder!!.build()
        notificationManager.notify(notificationID,notification)
上一篇 下一篇

猜你喜欢

热点阅读