Android 8.1 通知的变化

Android 7.0 通知的写法如下:


NotificationManager manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification=new NotificationCompat.Builder(MainActivity.this)
                        .setContentText("Content ")//设置通知内容

                        .setContentTitle("Content title")//设置通知标题
                                          
                        .setLargeIcon(BitmapFactory.decodeResource(getResources()
                                                                ,R.mipmap.ic_launcher))
                        .setSmallIcon(R.mipmap.ic_launcher)
           
                        .build();

manager.notify(1,notification);

这是7.0通知的基本写法,如果需要加入震动,LED灯 的闪烁,音效等只需要加在build()之前即可,如:


NotificationManager manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification=new NotificationCompat.Builder(MainActivity.this)
                        .setContentText("Content ")//设置通知内容

                        .setContentTitle("Content title")//设置通知标题
                                          
                        .setLargeIcon(BitmapFactory.decodeResource(getResources()
                                                                ,R.mipmap.ic_launcher))
                        .setSmallIcon(R.mipmap.ic_launcher)
                        
                        .setLights(Color.GREEN,1000,1000)
            
                        .setSound(Uri.fromFile(new File("音频文件位置")))

                        .setVibrate(new long[]{0,1000,1000,1000})
           
                        .build();

manager.notify(1,notification);

添加LED灯闪烁,第一个1000是亮的ms,第二个是熄灭的ms,也就是亮一秒,灭一秒(绿色的灯)。                       

.setLights(Color.GREEN,1000,1000)

添加声音 如果你是按照书上的路径,有可能你没有此文件,实验的最好办法就是添加自己的路径。
 .setSound(Uri.fromFile(new File("音频文件位置"))

添加震动:数组中奇数位置是不震动的ms,偶数位是震动的ms,需要注意的是震动是需要权限的,所以需要在Manifest中添加:

<uses-permission android:name="android.permission.VIBRATE">

.setVibrate(new long[]{0,1000,1000,1000})//此不需要添加

              

Android 8.1 (可能8.0等也适用,因为我手机是8.1)

String id="channel_1";
NotificationManager manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);

//配置通道
NotificationChannel channel = new NotificationChannel(id,"my_channel",NotificationManager.IMPORTANCE_DEFAULT);

channel.enableVibration(true);
channel.setVibrationPattern(new long[]{0,1000,1000,1000});
String path="android.resource://com.example.chenx.notification/"+R.raw.music;
channel.setSound(Uri.parse(path),Notification.AUDIO_ATTRIBUTES_DEFAULT);
manager.createNotificationChannel(channel);


Notification notification=new NotificationCompat.Builder(MainActivity.this,id)
                        .setContentText("Content")
                        .setContentTitle("Content title")
                                   
                        .setLargeIcon(BitmapFactory.decodeResource(getResources() 
                                                             ,R.drawable.timg))
                        .setSmallIcon(R.mipmap.ic_launcher_round)
                      
                        .build();

manager.notify(1,notification);

老版本的Android因为通知的不安全性,所以加入了通道这一参数,可以看到:

Notification notification=new NotificationCompat.Builder(MainActivity.this,id)

id就是指定的通道,在此之上配置了一个叫channel_1的通道,同样震动,音频也是在通道里设置,而不是像以前一样,至于LED灯,可能是手机问题,我的手机没有实现,音频的路径是我自己添加的,不是系统的音频路径,所以需要自己改,至于pendingIntent等功能没有变化。

猜你喜欢

转载自blog.csdn.net/daguniang123/article/details/88048686
8.1