控件 -- Notification

一、概念

Notification,是一种具有全局效果的通知,可以在系统的通知栏中显示。当App向系统发出通知时,它将先以图标的形式显示在通知栏中。用户可以下拉通知栏查看通知的详细信息。通知栏和抽屉式通知栏均是由系统控制,用户可以随时查看。

二、使用

Notification的基本操作主要有创建、更新、取消这三种。
一个 Notification的必要属性有三项,如果不设置则在运行时会抛出异常:
小图标,通过 setSmallIcon() 方法设置;
标题,通过 setContentTitle() 方法设置;
内容,通过 setContentText() 方法设置。
除了以上三项,其它均为可选项。可以给 Notification设置一个 Action ,这样就可以直接跳转到App的某个Activity、启动一个 Service或者发送一个Broadcast。当系统接收到通知时,可以通过震动、响铃、呼吸灯等多种方式进行提醒。

1.创建Notification

import android.support.v4.app.NotificationCompat;
private void testNotification() {
    Intent intent = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    //获取NotificationManager实例
    NotificationManager notifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    //实例化NotificationCompat.Builde并设置相关属性
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            //设置小图标
            .setSmallIcon(R.mipmap.ic_launcher)
            //设置通知标题
            .setContentTitle("最简单的Notification")
            //设置通知内容
            .setContentText("只有小图标、标题、内容")
            //设置pendingIntent
            .setContentIntent(pendingIntent)
            //点击通知后自动清除
            .setAutoCancel(true);
    //通过builder.build()方法生成Notification对象,并发送通知,id=1
    notifyManager.notify(1, builder.build());
}

2.更新Notification

更新通知只需要再次发送相同ID的通知即可,如果之前的通知还未被取消,则会直接更新该通知相关的属性,如果之前的通知已经被取消,则会重新创建一个新通知。

3.取消Notification

取消通知有以下几种方式:
1.点击通知栏的清除按钮,会清除所有可清除的通知;
2.设置了setAutoCancel()或FLAG_AUTO_CANCEL的通知,点击该通知时会清除它;
3.通过NotificationManager调用cancel(int id)方法清除指定ID的通知;
4.通过NotificationManager调用cancel(String tag, int id)方法清除指定TAG和ID的通知;
5.通过NotificationManager调用cancelAll()方法清除所有该应用之前发送的通知。

如果是通过NotificationManager.notify(String tag, int id, Notification notify)方法创建的通知,那么只能通过NotificationManager.cancel(String tag, int id)方法才能清除对应的通知,调用NotificationManager.cancel(int id) 无效。

4.设置Notification的通知效果

Notification有震动、响铃、呼吸灯三种通知效果,可以通过setDefaults(int defualts)方法来设置。Default属性有以下四种,一旦设置了Default效果,自定义的效果就会失效。
默认通知效果:

//
Notification.DEFAULT_VIBRATE

//添加系统默认声音效果,设置此值后,调用setSound()设置自定义声音无效
Notification.DEFAULT_SOUND

//添加默认呼吸灯效果,使用时须与Notification.FLAG_SHOW_LIGHTS结合使用,否则无效
Notification.DEFAULT_LIGHTS

//添加上述三种默认提醒效果
Notification.DEFAULT_ALL

常用通知效果:

//三色灯提醒,在使用三色灯提醒时候必须加该标志符
Notification.FLAG_SHOW_LIGHTS

//发起正在运行事件(活动中)
Notification.FLAG_ONGOING_EVENT

//让声音、振动无限循环,直到用户响应(取消或者打开)
Notification.FLAG_INSISTENT

//发起Notification后,铃声和震动均只执行一次
Notification.FLAG_ONLY_ALERT_ONCE

//用户单击通知后自动消失
Notification.FLAG_AUTO_CANCEL

//只有调用NotificationManager.cancel()时才会清除
Notification.FLAG_NO_CLEAR

//表示正在运行的服务
Notification.FLAG_FOREGROUND_SERVICE

设置方法:

NotificationCompat.Builder#setDefaults
NotificationCompat.Builder#setSound
NotificationCompat.Builder#setVibrate
NotificationCompat.Builder#setLights

notification.defaults
notification.sound
notification.vibrate
notification.ledARGB
notification.ledOnMS
notification.ledOffMS
notification.flags

三、适配Android 8.0通知

  1. AndroidManifest.xml

    
        
            
        
    
    
  2. strings.xml

    OneNotification
    TwoNotification
    ThreeNotification
    
  3. NotificationChannelController

    public class NotificationChannelController {
        public static final String CHANNEL_ID_ONE_NOTIFICATION = "CHANNEL_ID_ONE_NOTIFICATION";
        public static final String CHANNEL_ID_TWO_NOTIFICATION = "CHANNEL_ID_TWO_NOTIFICATION";
        public static final String CHANNEL_ID_THREE_NOTIFICATION = "CHANNEL_ID_THREE_NOTIFICATION";
    
        @RequiresApi(api = Build.VERSION_CODES.O)
        private enum Channel {
            ONE_NOTIFICATION(CHANNEL_ID_ONE_NOTIFICATION, R.string.one_notification, NotificationManager.IMPORTANCE_LOW, true),
            TWO_NOTIFICATION(CHANNEL_ID_TWO_NOTIFICATION, R.string.two_notification, NotificationManager.IMPORTANCE_DEFAULT, false),
            THREE_NOTIFICATION(CHANNEL_ID_THREE_NOTIFICATION, R.string.three_notification, NotificationManager.IMPORTANCE_HIGH, true);
    
            private final String id;
            private final int nameResId;
            private final int importance;
            private final boolean showBadge;
    
            Channel(String id, @StringRes int nameResId, int importance, boolean showBadge) {
                this.id = id;
                this.nameResId = nameResId;
                this.importance = importance;
                this.showBadge = showBadge;
            }
    
            public String getId() {
                return id;
            }
    
            public int getNameResId() {
                return nameResId;
            }
    
            public int getImportance() {
                return importance;
            }
    
            public boolean canShowBadge() {
                return showBadge;
            }
        }
    
        public static void createChannels(Context context) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                final NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                List channelList = new ArrayList<>();
                for (Channel channel : Channel.values()) {
                    NotificationChannel notificationChannel = new NotificationChannel(channel.getId(), context.getString(channel.getNameResId()), channel.getImportance());
                    notificationChannel.setShowBadge(channel.canShowBadge());
                    channelList.add(notificationChannel);
                }
                if (notificationManager != null) {
                    notificationManager.createNotificationChannels(channelList);
                }
            }
        }
    }
    
  4. MainActivity

    import android.app.PendingIntent;
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.os.Bundle;
    import android.support.v4.app.NotificationCompat;
    import android.support.v4.app.NotificationManagerCompat;
    import android.support.v7.app.AppCompatActivity;
    import android.util.Log;
    
    public class MainActivity extends AppCompatActivity {
        private static final String TAG = MainActivity.class.getSimpleName();
        private TestBroadcastReceiver mReceiver;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Log.d(TAG, "zwm, onCreate");
    
            //适配Android 8.0, 创建通知渠道
            NotificationChannelController.createChannels(getApplicationContext());
            Intent intent = new Intent("com.tomorrow.intent.action.test");
            intent.setPackage(getPackageName());
            PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
            NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NotificationChannelController.CHANNEL_ID_THREE_NOTIFICATION);
            builder.setShowWhen(true)
                    .setContentTitle("notification title")
                    .setContentText("notification content")
                    .setLargeIcon(bitmap)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setStyle(new NotificationCompat.BigTextStyle().bigText("BigTextStyle content"))
                    .setContentIntent(pendingIntent)
                    .setAutoCancel(true);
            NotificationManagerCompat.from(this).notify(99, builder.build());
        }
    }
    
  5. TestBroadcastReceiver

    public class TestBroadcastReceiver extends BroadcastReceiver {
        private static final String TAG = "TestBroadcastReceiver";
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d(TAG, "zwm, onReceive, action: " + intent.getAction());
        }
    }
    
  6. 打印日志

    2020-03-26 13:26:30.397 20445-20445/com.tomorrow.architetest D/MainActivity: zwm, onCreate
    2020-03-26 13:26:50.909 20445-20445/com.tomorrow.architetest D/TestBroadcastReceiver: zwm, onReceive, action: com.tomorrow.intent.action.test
    

你可能感兴趣的:(控件 -- Notification)