1.对于Android8.0以上的系统:
1.先要向系统注册一个Channel:
String channelId = "ChannelId";
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel(channelId , channelId ,
NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription(channelId);
//设置呼吸灯
channel.enableLights(true);
channel.setLightColor(Color.RED);
//设置震动,需要震动权限,Long数组偶数Index的数字表示震动毫秒数,奇数Index的数字表示震动暂停毫秒数
channel.enableVibration(true);
channel.setVibrationPattern(new long[]{200, 300, 200, 300, 200, 300});
manager.createNotificationChannel(channel);
2.只有向系统注册了至少一个Channel,才能发通知
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//Notification的接口只有在Android8.0(API26)以上才可以使用
//注意第二个参数是channelId,这个channelId对应的Channel必须已经创建,否则无法显示通知
Notification notification = new Notification.Builder(this, channelId )
.setContentTitle("Title")
.setContentText("Content")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)) //设置大图标
.build();
manager.notify(id, notification);
}
在Android8.0以上系统中,许多设置项都被转交给Channel管理,比如灯光,震动,声音;一个Channel中的所有通知都会自动被配置这些属性;
同一个Channel(通过Channel id标识是否是一个)只可以App安装之后在系统中被注册一次,多次设置只有第一次生效;被注册的Channel生效的生命周期是从Channel被注册到App被卸载
2.对于Android6.0以上8.0以下的系统:
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
//在Android6-8版本中,创建通知使用NotificationCompat,也可以带Channelid,但是不会生效
Notification notification = new NotificationCompat.Builder(this, Channelid)
. setContentTitle("Title" + id)
.setContentText(Channelid)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setVibrate(new long[]{200, 300, 200, 300, 200, 300})
.setLights(Color.RED, 0, 1000)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.build();
manager.notify(id, notification);
}
在 [ 6.0-8.0)版本的系统中也可以注册Channel,并为通知指定Channel,只是Channel中的配置不会生效,这些配置需要在NotificationBuilder中配置才会生效;
3.为通知添加点击事件:
为Notification设置Intent:
Intent intent = new Intent(this, TestActivity.class);
intent.putExtra("key", "hello_world");
PendingIntent pendingIntent = PendingIntent.getActivity(TestActivity.this, 0, intent, 0);
Notification notification = new Notification.Builder(this, Channelid)
.setAutoCancel(true) //点击后Notification消失
.setContentIntent(pendingIntent)
....
.build();
manager.notify(id, notification);
在Android8.0及以上
的系统可已通过这样的方式传递消息给Activity:
public class TestActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
getIntent().getStringExtra("key");
}
}
通知数量上限的问题
Android系统限制了同一App最多可以产生50条通知(不同系统版本可能稍有不同),超过50条未被查收的通知会直接被忽略
可以通过维护一个队列,将超出限制且最早产生的通知在代码中取消掉:
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.cancel(id);
或者可以利用ID相同的通知互相覆盖的现象避免通知数量溢出
检查应用是否允许通知:
NotificationManagerCompat manager = NotificationManagerCompat.from(this);
boolean allowNotification = manager.areNotificationsEnabled();
跳转到打开通知的页面:
try {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
//这种方案适用于 API 26, 即8.0(含8.0)以上可以用
intent.putExtra(EXTRA_APP_PACKAGE, getPackageName());
intent.putExtra(EXTRA_CHANNEL_ID, getApplicationInfo().uid);
//这种方案适用于 API21——25,即 5.0——7.1 之间的版本可以使用
intent.putExtra("app_package", getPackageName());
intent.putExtra("app_uid", getApplicationInfo().uid);
//小米6 -MIUI9.6-8.0.0系统
if ("MI 6".equals(Build.MODEL)) {
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
}
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
//下面这种方案是直接跳转到当前应用的设置界面。
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivity(intent);
}