发送通知
int count=1;
private void testNotification(){
NotificationManager notificationManager =
(NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
String channelID="Channel";
channelID += count;
Notification notification = null;
NotificationChannel channel = new NotificationChannel(
channelID,"noteNotification",NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this,channelID);
builder.setAutoCancel(true)
.setCategory(Notification.CATEGORY_MESSAGE)
.setContentText("3333333333333")
.setContentTitle("333")
.setDefaults(Notification.DEFAULT_SOUND)
.setSmallIcon(R.mipmap.ic_launcher);
Notification notification33 = builder.build();
notificationManager.notify(count, notification33);
count++;
} else {//旧版本发送方式
}
// 获取通知
StatusBarNotification[] StatusBarNotification;
int notifisize = (StatusBarNotification = notificationManager.getActiveNotifications()).length;
Toast.makeText(this,"notification size:"+notifisize,Toast.LENGTH_SHORT).show();
}
//请求notification 权限
if (!isEnabled()) {
Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
startActivity(intent);
//打开settings的一个通知权限界面
} else {
//Toast.makeText(this, "已开启服务权限", Toast.LENGTH_LONG).show();
}
private boolean isEnabled() {
String pkgName = getPackageName();
final String flat = Settings.Secure.getString(getContentResolver(),
ENABLED_NOTIFICATION_LISTENERS);
//flat:com.datamax.note/com.datamax.note.NotificationListenerImp
if (!TextUtils.isEmpty(flat)) {
final String[] names = flat.split(":");
for (int i = 0; i < names.length; i++) {
final ComponentName cn = ComponentName.unflattenFromString(names[i]);
if (cn != null) {
if (TextUtils.equals(pkgName, cn.getPackageName())) {
return true;
}
}
}
}
return false;
}