美文网首页
Android通知快照

Android通知快照

作者: 番茄tomato | 来源:发表于2021-05-11 10:33 被阅读0次

简单整理一下android通知,快速使用,不深入讲解。
需要完全掌握通知还请仔细学习官方文档。
参考官方文档:https://developer.android.com/training/notify-user/build-notification?hl=zh-cn

发一条简单通知:

记得开发送通知权限!!!

    val CHANNEL_ID_1 = "10001"

    private fun sendNotify() {
        Log.d(TAG, "发送通知")
        createNotificationChannel()
        val intent = Intent(this, AlertActivity::class.java).apply {
            flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
        }
        val pendingIntent: PendingIntent = PendingIntent.getActivity(this, 0, intent, 0)

        val builder = NotificationCompat.Builder(this, CHANNEL_ID_1)
            .setSmallIcon(R.mipmap.logo_splash)//设置通知小图标
            .setContentTitle("我是通知标题")
            .setContentText("我是通知内容")
            .setContentIntent(pendingIntent)//设置通知跳转界面
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)//设置通知重要性
        with(NotificationManagerCompat.from(this)) {
             var notifyId=1//notifyId是标记通知的唯一id,不能重复 用于标记通知,移除指定通知等
            notify(notifyId, builder.build())
        }
    }

//android 8以上需要创建通知频道才能发出通知
    private fun createNotificationChannel() {
        // Create the NotificationChannel, but only on API 26+ because
        // the NotificationChannel class is new and not in the support library
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val name = getString(R.string.channel_name)
            val descriptionText = getString(R.string.channel_description)
            val importance = NotificationManager.IMPORTANCE_DEFAULT
            val channel = NotificationChannel(CHANNEL_ID_1, name, importance).apply {
                description = descriptionText
            }
            // Register the channel with the system
            val notificationManager: NotificationManager =
                getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(channel)
        }
    }

发一条自定义常驻通知:

记得开发送通知权限!!!
自定义通知就是可以让我们通知布局xml文件自定义通知的样式,监听通知中各个控件


image.png

    private fun sendRemoteNotify() {
//还是要创建通知频道
        createNotificationChannel()

        val intent = Intent(this, AlertActivity::class.java).apply {
            flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
        }
        val pendingIntent1: PendingIntent = PendingIntent.getActivity(this, 0, intent, 0)
        val pendingIntent2: PendingIntent = PendingIntent.getActivity(this, 0, intent, 0)
//设置通知布局
        val notificationLayout =
            RemoteViews(packageName, R.layout.notify_my_first)
//设置布局中的控件1监听
        notificationLayout.setOnClickPendingIntent(
            R.id.img,
            pendingIntent1
        )
//设置布局中的控件2监听
        notificationLayout.setOnClickPendingIntent(
            R.id.tv,
            pendingIntent2
        )
//创建通知
        val customNotifyBuilder = NotificationCompat.Builder(context, CHANNEL_ID_1)
            .setSmallIcon(R.mipmap.logo_splash)
            .setStyle(NotificationCompat.DecoratedCustomViewStyle())
            .setCustomContentView(notificationLayout)
            .setOngoing(true)//设置为常驻通知
//发出通知
        with(NotificationManagerCompat.from(this)) {
            // notificationId is a unique int for each notification that you must define
            notify(2, customNotifyBuilder.build())
        }

    }

通知布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:gravity="center_vertical"
    android:orientation="horizontal">


    <ImageView
        android:id="@+id/img"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@mipmap/logo_splash" />

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:text="我是通知" />


</LinearLayout>

相关文章

  • Android通知快照

    简单整理一下android通知,快速使用,不深入讲解。需要完全掌握通知还请仔细学习官方文档。参考官方文档:http...

  • local android studio project to

    本地 android studio 项目 如图屏幕快照 2019-04-20 23.26.12.png Comma...

  • Android直接回复通知

    Android直接回复通知 通知直接回复 Android N/7.0 前言 通知(Notification)可为是...

  • 《Android第一行代码》first reading 十

    Android多媒体运用 一 通知 使用Android通知功能步骤: 通过Context的getSystemSer...

  • 2019-03-22

    iOS 屏幕快照(view 截图/快照) layer 快照 view 快照

  • Android的通知

    通知是什么? 通知存在的意义是什么? Android O(Android SDK Api Level 26,And...

  • android通知

    1.这里写了一个常用的通知的代码

  • Android通知

    通知在实际开发中还是比较常见的,例如新闻,音乐播放器,等。 1,基本通知 2,基础扩展通知自定义布局 xml >>...

  • android 通知

    notification需要一个NotificationManager来管理,如何获取呢?Notification...

  • Android通知

    在这里写一个Android的通知,相应的图标以及位置大概也有介绍,需要的时候可以试试;

网友评论

      本文标题:Android通知快照

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