adroid 通知demo
程序员文章站
2022-06-08 23:49:37
...
通知的函数
@SuppressLint("WrongConstant")
void getNotification(){ NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = null;
String channelId = "com.message.notify";
String channelName = "notification";
//判断api版本大于等于26
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel channel =new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_LOW);
manager.createNotificationChannel(channel);
builder = new NotificationCompat.Builder(this, channelId);
Log.e("xx","xxxxxx"+Build.VERSION.SDK_INT);
}else {
builder = new NotificationCompat.Builder(this);
builder.setChannelId(channelId);
}
builder.setContentTitle("消息通知标题")//设置消息通知标题
.setContentText("消息通知内容")//设置消息通知内容
.setWhen(System.currentTimeMillis())//设置消息通知发送时间
.setSmallIcon(R.mipmap.ic_launcher)//设置小童通知状态栏图标
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))//设置消息通知下拉图标
.setPriority(NotificationCompat.PRIORITY_MAX);//设置消息优先级
//设置消息通知点击意图
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com"));
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
builder.setContentIntent(pendingIntent);
//点击消息通知后删除的两种实现方法
//第一种
builder.setAutoCancel(true);
int notificationId = 1;
//第二种
// manager.cancel(notificationId);
//设置消息通知的提醒效果
builder.setSound(Uri.fromFile(new File("/system/media/audio/ringtones/Luan.ogg")))//设置消息声音
.setVibrate(new long[]{0, 1000, 1000, 1000})//设置消息震动,需要加入android.permission.VIBRATE权限
.setLights(Color.GREEN, 1000, 1000)//设置消息提醒灯
.setDefaults(NotificationCompat.DEFAULT_ALL);//设置默认效果
//设置悬挂弹出
builder.setVisibility(Notification.VISIBILITY_PUBLIC);
builder.setFullScreenIntent(pendingIntent, true);
Notification notification = builder.build();
manager.notify(notificationId, notification);}