美文网首页
Android基础之Notification

Android基础之Notification

作者: 城南一霸賈十七 | 来源:发表于2018-08-16 12:29 被阅读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)

相关文章

  • Android基础之Notification

    Android Notification 详解具体代码实现 实现点击通知,跳转到相应的页面 保留 Activity...

  • Android基础-Notification

    在Android手机上,基本上每天都能看到各种各样的推送,如网易云的推荐,新闻的推荐,QQ,WeChat的消息推送...

  • 通知

    Android中Notification 提示对话框,notification 概述 notification,俗...

  • Android 之 Notification

    Notification在手机的运用中是很常见的,当我们收到一个短信时,就会在我们的通知栏显示一个消息的图标和简单...

  • Android之Notification

    Notification位于标题栏之下,用于提醒用户,内容为一些应用程序的推送消息和电池信息等。通常使用Notif...

  • Android8.0 NotificationChannel配

    .... ..问题: android8.0的技术革新中,需要在以往notification的基础上添加notifi...

  • Android学习笔记总结(未更新完,持续更新中)

    Android基础 Android之启动activity Android之数据存储 Android之ListVie...

  • Android 8.0 Notification

    Android 8.0 通知适配: Android Api 26 Notification Builder 构建...

  • Android笔记之Notification

    通知可以给予用户一些必要的信息,合理的利用通知能给带来意想不到的效果。Notification在不同版本上有不同的...

  • Android UI - Notification

    Notification Notification,通知。Android 里的通知设置无非下面几步:创建一个 No...

网友评论

      本文标题:Android基础之Notification

      本文链接:https://www.haomeiwen.com/subject/kjsdbftx.html