Android 之 Notification 必须掌握知识点

本文同步个人 CSDN 博客
转载请注明出处
blog.csdn.net/wufeng55/ar…javascript

建立并发送一个系统通知php

activity_main.xmljava

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.mu_16jj.notificationdemo.MainActivity">
    <TextView android:id="@+id/tv_send_notification" android:layout_width="wrap_content" android:layout_height="35dp" android:layout_centerInParent="true" android:gravity="center" android:background="@color/colorAccent" android:text="send notification" android:textSize="16sp" />
</RelativeLayout>复制代码

很简单的布局,就一个 TextView 利用其点击事件来发送通知。android

建立 Notification并发

private void createNotification() {
        notification = new NotificationCompat.Builder(MainActivity.this)
                .setContentTitle("Notification title")
                .setTicker("Ticker method function...")
                .setContentText("Notification content text")
                .setSubText("subtext...")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher_round)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.large_icon))
                .build();
    }复制代码

经过 NotificationCompat.Build 来构建一个 Notification 对象,并设置一系列属性(每一个属性对应的效果后面展现)。ide

发送按钮监听布局

notificationManager.notify(1, notification);复制代码

这里须要说明的是,Notification 的发送仍是由 NotificationManager 来管理的,第一个参数用来标识一个惟一的 Notification;第二个参数就是须要发送的 Notification 对象。咱们在 onCreate 方法中经过如下代码初始化了通知管理器对象:学习

notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);复制代码

运行效果ui

通知效果

属性介绍

建立通知时的一些基本属性就利用效果图解释完了,其中的 setTicker 须要真机运行才能够看到效果。
但是,这个时候咱们点击这条通知后,该通知一直在系统状态栏,既没有消失,也没有页面跳转,这并非咱们想要的效果,因此下面就来实现点击通知跳转页面,这里以系统提供的登陆 Activity 为目的界面。this

  • 实现点击跳转界面

修改咱们建立通知的方法

private void createNotification() {
        Intent intent = new Intent(this, LoginActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        notification = new NotificationCompat.Builder(MainActivity.this)
                .setContentTitle("Notification title")
                .setContentText("Notification content text")
                .setSubText("subtext...")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher_round)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.large_icon))
                .setContentIntent(pendingIntent)
                .setAutoCancel(true)
                .build();
    }复制代码

能够看到,这里增长了一个 setContentIntent 方法,该方法接收一个 PendingIntent 参数,而获取该实例则能够有三种途径:getActivity(),getBroadcast(),getService();这几个方法所接收的参数都是相同的,第一个是上下文,第二个参数通常不多使用,传 0 便可,第三个参数即是点击通知须要跳转的 Intent 意图对象(LoginActivity直接使用 AS 提供的模板),第四个参数用于肯定 PendingIntent 的行为,有 4 种植可选:FLAG_ONE_SHOT,FLAG_NO_CREATE,CANCEL_CURRENT 和 FLAG_UPDATE_CURRENT。

运行效果

pendingintent

主要常量
FLAG_CANCEL_CURRENT:若是当前系统中已经存在一个相同的 PendingIntent 对象,那么就将先前已有的 PendingIntent 取消,而后从新生成一个 PendingIntent 对象。
FLAG_NO_CREATE:若是当前系统中不存在相同的 PendingIntent 对象,系统将不会建立该 PendingIntent 对象而是直接返回 null。
FLAG_ONE_SHOT:该 PendingIntent 只做用一次。在该 PendingIntent 对象经过send() 方法触发事后,PendingIntent 将自动调用 cancel() 进行销毁,那么若是你再调用 send() 方法的话,系统将会返回一个 SendIntentException。
FLAG_UPDATE_CURRENT:若是系统中有一个和你描述的 PendingIntent 对等的PendingInent,那么系统将使用该 PendingIntent 对象,可是会使用新的 Intent 来更新以前 PendingIntent 中的 Intent 对象数据,例如更新 Intent 中的 Extras。

PendingIntent 和 Intent
PendingIntent 是一个特殊的 Intent,主要区别是 intent 是立马执行,PendingIntent 是待肯定的 Intent。PendingIntent 的操做其实是传入的 intent 的操做。使用 pendingIntent 的目的主要是用于所包含的 intent 执行是否知足某些条件。

  • 实现点击通知后,这条通知从系统状态栏消失

有两种办法,一种是给 Notification 设置

setAutoCancel(true)复制代码

另外一种是经过通知管理器调用 cancel 方法,该方法有一个参数,就是咱们前面发送的时候设置的惟一标识。

通知,设置特效

咱们常见的手机 APP 当有推送消息或者电话之类的通知时,咱们的手机 LED 灯会亮起来,并且颜色也会不同,有时候还会有声音,或者震动之类的,下面就来学习这些方法,这里我经过监听手机锁屏的广播来发送通知(效果会更好),再也不使用上面的按钮来发送(锁屏速度快的话效果仍是能够看到的)。

建立通知代码

private void createNotification() {
        Intent intent = new Intent(this, LoginActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        notification = new NotificationCompat.Builder(MainActivity.this)
                .setContentTitle("Notification title")
                .setContentText("Notification content text")
                .setSubText("subtext...")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher_round)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.large_icon))
                // 跳转界面 系统状态栏通知消失
                .setContentIntent(pendingIntent)
                .setAutoCancel(true)
                // 收到通知时播放声音并震动
                .setDefaults(NotificationCompat.DEFAULT_SOUND) // 默认通知声音
// .setSound(Uri.fromFile(new File("/system/media/audio/ringtones/Luna.ogg")))
                .setVibrate(new long[]{0, 1000, 1000, 1000})
                // LED 灯颜色
                .setLights(Color.RED, 1000, 1000)
                // 根据手机的当前环境使用默认
// .setDefaults(NotificationCompat.DEFAULT_ALL)
                .build();
    }复制代码

这里的注释已经很清楚了,须要注意的是震动是须要权限的:

<uses-permission android:name="android.permission.VIBRATE" />复制代码

广播接收器

/** * 监听手机锁屏 */
    public class MyReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
                notificationManager.notify(1, notification);
            }
        }
    }复制代码

关注注册广播问题,请看《Android 广播介绍以及使用示例

  1. 高级之 setStyle() 和 setPriority() 方法

    • 通知内容支持长文本

      .setStyle(new NotificationCompat.BigTextStyle().bigText("The failures and reverses which await men - and one after another sadden the brow of youth - add a dignity to the prospect of human life, which no Arcadian success would do. -- Henry David Thoreau"))复制代码

      运行效果

bigtext

  • 通知内容支持大图片
    .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(), R.drawable.large_icon)))复制代码
    运行效果

bigpucture

  • 通知优先级
    通知的优先级共有五个常量值可选:PRIORITY_DEFAULT(默认);PRIORITY_MIN 最低优先级,系统可能在特定场景才会显示这条通知,好比用户下拉状态栏;PRIORITY_LOW 较低优先级,系统可能会将这类通知缩小,或改变其显示的顺序,将其排在更重要的通知以后;PRIORITY_HIGH 表示较高优先级,系统可能会将这类通知放大、改变其显示的顺序,将其排在靠前的位置; PRIORITY_MAX 表示最高的重要通知,这类通知必需要让用户马上看到,甚至须要用户作出响应操做。
    .setPriority(NotificationCompat.PRIORITY_MAX)复制代码

运行效果(PRIORITY_MAX)

PRIORITY_MAX


自定义布局通知
咱们在已有的界面基础上再增长一个 TextView 用它的点击事件来发送自定义通知,自定义通知代码以下:

/** * 建立自定义布局通知 */
    private void createCustomNotification() {
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.custom_notification_layout);
        remoteViews.setImageViewResource(R.id.iv_cus_notification, R.mipmap.ic_launcher);
        remoteViews.setTextViewText(R.id.tv_cus_notification, "Notification of Android");
        remoteViews.setTextViewText(R.id.btn_cus_notification, "Button");

        cusNotification = new NotificationCompat.Builder(MainActivity.this)
                // 切记,必定要设置,不然通知显示不出来
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContent(remoteViews)
                .setTicker("Custom Notification")
                .setAutoCancel(true)
                .setDefaults(NotificationCompat.DEFAULT_ALL)
                .build();
    }复制代码

能够看到,相比系统通知的建立,这里多了一个新的 API——RemoteViews. 咱们的自定义布局就是经过它来加载,经过它的一系列 setXXX 方法能够给咱们的布局中控件设置相关属性,而后经过 Builder 的 setContent 方法将其设置上去,这样咱们的自定义通知效果就实现了。

点击事件

notificationManager.notify(2, cusNotification);复制代码

运行效果

自定义布局通知

关于通知的自定义布局从效果就能够看到,很简单,这里不贴出。

注意点
自定义通知布局的可用高度取决于通知视图。普通视图布局限制为 64 dp,扩展视图布局限制为 256 dp。
不管是系统通知仍是自定义布局通知,smallIcon 是必须设置的。


技术公众号.jpg
相关文章
相关标签/搜索