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中添加:

.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等功能没有变化。

 

 

 

更多相关文章

  1. Android 消息通知栏Notification使用和权限
  2. android studio jni路径配置
  3. android通知栏Notification用法
  4. Android点击通知栏返回正在运行的Activity
  5. Android之Bean属性通知类
  6. android工程没有gen路径
  7. Android如何获取asset目录下所有文件的路径
  8. android通知栏响应事件

随机推荐

  1. Android(安卓)Testing Point
  2. android 取手机号码
  3. Android(安卓)UI控件详解-GridView(网格
  4. android 登陆界面
  5. Android(安卓)用手动组拼的方式生成xml
  6. Android系统版本和API等级(level)对照表
  7. Android开发遇到的问题————android.c
  8. Android检测网络连接并提示
  9. android WiFi 开关代码
  10. android打开文件