Meng Han ink & feather --Android Notification notice

table of Contents

Here Insert Picture Description

First, the basic use notifications

(1), the effect of FIG.

(2) using a step

(3), details of the use of

Second, the use of advanced notice

(1) to set the sound

(2), a vibration

(3), disposed LED flashes

(4), the effect of default settings for

Third, the use of advanced notice

(1), set rich text information

(2), provided with image information

(3), the importance of setting notification

First, the basic use of the notification
(1), the effect of FIG.
Here Insert Picture Description

(2), using steps
1, you first need a NotificationManager to manage, you can call getSystemService method of obtaining Context, here passed a Context. NOTIFICAATION_SERVICE can be.

NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

2, requires the use of a Notification Builder constructor to create an object, due to the different API will cause instability problems appear different versions of the notice, so here NotificationCompat class to use compatible versions.

Notification notification = new NotificationCompat.Builder(MainActivity.this).build();

3, the basic settings

.setContentTitle("这是测试通知标题")  //设置标题
.setContentText("这是测试通知内容") //设置内容
.setWhen(System.currentTimeMillis())  //设置时间
.setSmallIcon(R.mipmap.ic_launcher)  //设置小图标
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))   //设置大图标

4, call notify () to make notification is displayed (first parameter is the ID, to ensure that each notification specified id is different, and the second parameter is the notification objects)

manager.notify(1,notification);

(3)、细节使用
  <一>、跳转功能:
使用PendingIntent进行通知点击跳转功能。
PendingIntent的用法:
(1)、通过getActivity()、getBroadcast()、getService()方法获取实例
(2)、参数(Context context, int requestCode, Intent intent, int flags)
第一个参数:Context
第二个参数:requestCode 一般用不到 ,通常设置为0
第三个参数:intent
第四个参数:flags 用于确定PendingIntent的行为。这里传0就行
(3)、使用方法

 Intent intent = new Intent(MainActivity.this,NotificationActivity.class);
                PendingIntent pi = PendingIntent.getActivity(MainActivity.this,0,intent,0);
                NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                Notification notification = new NotificationCompat.Builder(MainActivity.this)
                        .setContentTitle("这是测试通知标题")  //设置标题
                        .setContentText("这是测试通知内容") //设置内容
                        .setWhen(System.currentTimeMillis())  //设置时间
                        .setSmallIcon(R.mipmap.ic_launcher)  //设置小图标  只能使用alpha图层的图片进行设置
                        .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))   //设置大图标
                        .setContentIntent(pi)
 //                       .setAutoCancel(true)
                        .build();
                manager.notify(1,notification);

<二>、通知取消:
我们发现当点击查看通知后,通知栏中还保留着通知图标,我们怎么取消呢
(1)、使用setAutoCancel(true)

Notification notification = new NotificationCompat.Builder(MainActivity.this)
                        ...
                       .setAutoCancel(true) //设置为自动取消
                        .build();
                manager.notify(1,notification);

(2)、在跳转后的Activity中

 NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.cancel(1);

这里的cancle传入的是一个1,就是我们创建Notification中指定的通知的ID
Here Insert Picture Description
二、通知的进阶使用
都是一行代码进行设置, 这里就不分开写了

 Notification notification = new NotificationCompat.Builder(MainActivity.this)
                        ...
                        .setSound(Uri.fromFile(new File("/system/media/audio/ringtones/Luna.ogg"))) //设置通知提示音
                        .setVibrate(new long[]{0,1000,1000,1000}) //设置振动, 需要添加权限  <uses-permission android:name="android.permission.VIBRATE"/>
                        .setLights(Color.GREEN,1000,1000)//设置前置LED灯进行闪烁, 第一个为颜色值  第二个为亮的时长  第三个为暗的时长
                        .setDefaults(NotificationCompat.DEFAULT_ALL)  //使用默认效果, 会根据手机当前环境播放铃声, 是否振动
                        .build();
                manager.notify(1,notification);

三、通知的高级使用
(1)、设置富文本信息
当我们使用setContentText的时候, 内容为很长的字符串, 显示结果是这样的:
Here Insert Picture Description
内容显示不全。如果产品就要显示完全的内容文本我们怎么办。
可以使用setStyle()

 Notification notification = new NotificationCompat.Builder(MainActivity.this)
                      ...
                        .setStyle(new NotificationCompat.BigTextStyle().bigText("这是一段很长的文字很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长"))                      
                        .build();
                manager.notify(1,notification);

We created NotificationCompat.BigTextStyle objects in setStyle () method. This object is used to encapsulate the long text information, call its bigText () method to pass the text on the line.
Here Insert Picture Description

(2)、设置带有图片消息
Here Insert Picture Description
Notification notification = new NotificationCompat.Builder(MainActivity.this)

.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher)))
.build();
manager.notify(1,notification);

Pictures of the notice calling the method join (large image) of

(3), the importance of setting notification

Notification notification = new NotificationCompat.Builder(MainActivity.this)
                      ...
                        .setPriority(NotificationCompat.PRIORITY_MAX)
                        .build();
                manager.notify(1,notification);

setPriority method receives an integer parameter is used to set the degree of importance of this notification, there are five values can be selected
PRIORITY_DEFAULT: indicates the default importance, and do not set the same effect
PRIORITY_MIN: represents the lowest degree of importance. The system will only show when the user pulled down the status bar
PRIORITY_LOW: represent less important, the system will reduce this type of notification, or change the order of display, will be behind the more important notifications.
PRIORITY_HIGH: indicates a higher degree of importance, such systems might notification methods, or change the order, quite early
PRIORITY_MAX: The most important degree, a separate message box will pop up, allowing users to make the appropriate.
Here Insert Picture Description
Set the display case MAX.

Guess you like

Origin blog.csdn.net/Zephyr_0312/article/details/95094646