在 Android O 以后,Google引入了通知通道的概念,如果目标API大于 Android O ,不直指定通知渠道是不能发送通知的。
这里放一个我写好的通知方法,大家可以适当的改改再用,当然亦可以直接用
/**
* 通过通知渠道发送通知 Android O 新增API
* 其他的还和以前一样
*
* @param channelID 渠道ID
* @param channelName 渠道名字
* @param subText 小标题
* @param title 大标题
* @param text 内容
*/
@TargetApi(Build.VERSION_CODES.O)
public void sendNotification(String channelID, String channelName, String subText, String title, String text) {
//创建通道管理器
NotificationChannel channel = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_HIGH);
NotificationManager manager;
manager = (NotificationManager) this.getSystemService(getApplicationContext().NOTIFICATION_SERVICE);
manager.createNotificationChannel(channel);
//构建通知
Notification.Builder builder = new Notification.Builder(getApplicationContext());
//设置小图标
builder.setSmallIcon(R.mipmap.ic_launcher);
//设置通知 标题,内容,小标题
builder.setContentTitle(title);
builder.setContentText(text);
builder.setSubText(subText);
//设置通知颜色
builder.setColor(Color.parseColor("#E91E63"));
//设置创建时间
builder.setWhen(System.currentTimeMillis());
//创建通知时指定channelID
builder.setChannelId(channelID);
Intent resultIntent = new Intent(this, ClipActivity.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(resultPendingIntent);
//构建一个通知,最后通知管理器 发送
Notification notification = builder.build();
manager.notify(1, notification);
}
网友评论