盒子
盒子
文章目录
  1. 系统默认通知
  2. 自定义通知

通知与自定义通知

奥利奥添加了通知渠道

系统默认通知

  • Notification
  • NotificationManager
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    private final String CHANNEL_ID="channelId_1";
    private final String CHANNEL_NAME="channelName_1";
    NotificationCompat.Builder builder=new NotificationCompat.Builder(this,channelId);
    Notification notification=builder
    .setSmallIcon(R.mipmap.ic_launcher)
    .setWhen(System.currentTimeMillis())
    .setContentTitle("title")
    .setContentText("content")
    .setCustomContentView(remoteViews)
    .setChannelId(channelId)
    .build();
    NotificationManager manager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    manager.createNotificationChannel(new NotificationChannel(channelId,channelName,NotificationManager.IMPORTANCE_DEFAULT));
    manager.notify(0,notification);

    在Android Version >= 26时需要加入渠道,否则无法弹出通知:

    1
    manager.createNotificationChannel(new NotificationChannel(channelId,channelName,NotificationManager.IMPORTANCE_DEFAULT));


    NotificationChannel(String id, CharSequence name, int importance)

    id为渠道Id,name为渠道名称,importance为通知优先级,分为以下几个:

    1. IMPORTANCE_UNSPECIFIED
    2. IMPORTANCE_NONE
    3. IMPORTANCE_MIN
    4. IMPORTANCE_LOW
    5. IMPORTANCE_DEFAULT
    6. IMPORTANCE_HIGH

    当创建不同渠道的通知时会将通知如下分类:

    渠道

    public void notify (int id, Notification notification)

    id为通知ID,是该app中该通知的唯一标识,即id相同则代表同一个通知,不弹出新通知

    对于其它丰富通知功能的方法,参照Notification.Builder中的方法

    自定义通知

  • RemoteViews
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    RemoteViews remoteViews=new RemoteViews(getPackageName(),R.layout.view_notification);
    remoteViews.setTextColor(R.id.text_notify_custom,getResources().getColor(R.color.colorAccent,null));
    PendingIntent pendingIntent=PendingIntent.getActivity(this,0,new Intent(this,TestActivity.class),PendingIntent.FLAG_UPDATE_CURRENT);
    remoteViews.setOnClickPendingIntent(R.id.butt_notify_custom,pendingIntent);
    Notification notification=builder
    .setSmallIcon(R.mipmap.ic_launcher)
    .setWhen(System.currentTimeMillis())
    .setContentTitle("title")
    .setContentText("content")
    .setCustomContentView(remoteViews)
    .setChannelId(channelId)
    .build();

    setCustomContentView(remoteViews)

    view_notification.xml:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    <?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:orientation="horizontal">
    <Button
    android:id="@+id/butt_notify_custom"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:text="跳转"/>
    <TextView
    android:id="@+id/text_notify_custom"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="自定义通知"/>
    </LinearLayout>

  • 这里有一个大坑,那就是自定义通知视图的布局不能是约束布局,否则报错!!!
  • Android 8.0及以上的操作系统需要添加通知渠道才能弹出通知否则无效!
  • 支持一下
    扫一扫,支持Grooter
    • 微信扫一扫
    • 支付宝扫一扫