Notification 通知

使用步骤

Manifest.xml文件中声明权限

1
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>

请求权限

创建通知的通道 NotificationChannel

使用 NotificationManager 的 createNotificationChannel() 方法创建通知的通道 NotificationChannel

1
2
3
4
5
6
//获取系统管理通知的对象 NotificationManager 在程序和系统的通知服务之间建立连接
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

//创建通道
val channel = NotificationChannel(channel_id, channel_name,NotificationManager.IMPORTANCE_HIGH)
notificationManager.createNotificationChannel(channel)

创建通知 Notification

1
2
3
4
5
6
7
8
9
val mNotification = NotificationCompat
.Builder( context: this,channel_id)
.setSmallIcon(R.drawable.logo) //小图标
.setContentTitle("标题")
.setContentText("这是文本内容")
.build()

//将通知发出去
notificationManager.notify( id: 1, mNotification)

自定义 Notification 布局

自定义 Notification 的布局,使用 RemoteViews
需要给Notification设置以下内容:

  • setContent(views : RemoteViews?)
  • setCustomBigContentView(views : RemoteViews?)

绑定通知栏中的事件(点击事件)PendingIntent
PendingIntent.getBroadcast()

1
2
3
4
5
6
7
mNotification = NotificationCompat
.Builder(AudioHelper.instance.context, CHANNEL_ID)
.setSmallIcon(R.drawable.small_icon)
.setContent(mSmallRemoteView)
.setCustomBigContentView(mBigRemoteView)
.build()
mNotificationManager.notify(1,mNotification)

配置 RemoteViews

1
2
3
4
5
6
7
mSmallRemoteView = RemoteViews(packageName,R.layout.layout_notification_collpase).apply {
setTextViewText(R.id.titleView,mMusic.title)//歌名
setTextViewText(R.id.authorView,mMusic.author)//歌手
setImageViewResource(R.id.previous,R.drawable.notification_previous)
setImageViewResource(R.id.playorpause,R.drawable.notification_play)
setImageViewResource(R.id.next,R.drawable.notification_next)
}

PendingIntent

endingIntent 对象封装了 Intent 对象的功能,同时以您应用的名义指定其他应用允许哪些操作的执行,来响应用户未来会进行的操作。
比如,所封装的 Intent 可能会在闹铃关闭后或者用户点击通知时被触发。

使用场景

PendingIntent 的应用场景关键在于间接的 Intent 跳转需求, 即先通过一级 Intent 跳转到某个组件,在该组件完成任务后再间接地跳转到二级的 Intent。PendingIntent 中的单词 “pending” 指延迟或挂起,就是指它是延迟的或挂起的。例如,你在以下场景中就可以使用 PendingIntent:

场景 1 - 系统通知消息的点击操作
场景 2 - 桌面微件的点击操作
场景 3 - 系统闹钟操作
场景 4 - 第三方应用回调操作

创建 PendingIntent

PendingIntent 支持在启动 Activity 、 Service 或 BroadcastReceiver
不同类型的组件必须使用特定的静态方法:

1
2
3
4
5
6
//启动 Activity
PendingIntent.getActivity(Context context, int requestCode, Intent intent, int flags)
//启动 Service
PendingIntent.Service(Context context, int requestCode, Intent intent, int flags)
//启动 BroadcastReceiver(发送广播)
PendingIntent.getBroadcast(Context context, int requestCode, Intent intent, int flags)

创建 PendingIntent 的4个参数:

  1. context: 当前应用的上下文,PendingIntent 将从中抽取授权信息;
  2. requestCode: PendingIntent 的请求码,与 Intent 的请求码类似;
  3. intent: 最终的意图操作;
  4. flag: 控制标记位,我们暂且放到一边。
  • FLAG_ONE_SHOT:PendingIntent对象仅使用一次;
  • FLAG_NO_CREATE:如果PendingIntent对象不存在则返回null
  • FLAG_CANCEL_CURRENT:如果PendingIntent对象已存在,则取消原有的对象,创建新的PendingIntent对象
  • FLAG_UPDATE_CURRENT:如果PendingIntent对象已存在,则保留原有的对象,修改原有对象的属性数据
  • FLAG_IMMUTABLE:PendingIntent对象是不可变的
  • FLAG_MUTABLE:PendingIntent对象是可变的

NotificationManager

常用方法:

  1. notify(int id, Notification notification) - 向用户发送通知消息
  2. cancel(int id) - 取消指定id的通知
  3. cancelAll() - 取消所有之前发送的通知

常用属性:

  1. static final String ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED - 通知策略权限已更改的广播动作
  2. static final String EXTRA_NOTIFICATION_POLICY_ACCESS_GRANTED_STREAMS - 通知策略权限已更改的流
  3. static final int IMPORTANCE_DEFAULT - 默认通知重要性等级
  4. static final int IMPORTANCE_HIGH - 高通知重要性等级
  5. static final int IMPORTANCE_LOW - 低通知重要性等级
    这些方法和属性可以帮助开发者对通知进行管理和控制。

NotificationChannel

属性:

  1. id:通道的唯一标识符。
  2. name:通道的名称。
  3. description:通道的描述。
  4. importance:通道的重要性级别。
  5. groupId:通道所属的组的Id。
    方法:
  6. getImportance():获取通道的重要性级别。
  7. getName():获取通道的名称。
  8. setDescription():设置通道的描述。
  9. setShowBadge():设置是否在应用图标上显示通知提示。
  10. enableVibration():启用通道的振动功能。
  11. enableLights():启用通道的指示灯功能。
  12. setVibrationPattern():设置通知的振动模式。
  13. setSound():设置通知的声音。
  14. setBypassDnd():设置通知是否绕过“勿扰模式”。
  15. setGroup():设置通道所属的组。
Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.

扫一扫,分享到微信

微信分享二维码
  • Copyrights © 2023-2025 Annie
  • Visitors: | Views:

嘿嘿 请我吃小蛋糕吧~

支付宝
微信