Android 之 Notification 必须掌握的知识

来源:互联网 发布:社交网络营销策划方案 编辑:程序博客网 时间:2024/05/22 04:33

创建并发送一个系统通知

activity_main.xml

<?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 利用其点击事件来发送通知。

创建 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 对象,并设置一系列属性(每个属性对应的效果后面展示)。

发送按钮监听

notificationManager.notify(1, notification);

这里需要说明的是,Notification 的发送还是由 NotificationManager 来管理的,第一个参数用来标识一个唯一的 Notification;第二个参数就是需要发送的 Notification 对象。我们在 onCreate 方法中通过以下代码初始化了通知管理器对象:

notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

运行效果

通知效果

属性介绍

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

  • 实现点击跳转界面

修改我们创建通知的方法

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 是必须设置的。


技术公众号

1 0
原创粉丝点击