赞
踩
- //创建通知管理类
- NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
- checkNofificationPermission(manager);
- String CHANNEL_ID = PreferenceUtils.getPrefString(this, PreferenceConstants.CHANNEL_ID, "");
- String CHANNEL_NAME = PreferenceUtils.getPrefString(this, PreferenceConstants.CHANNEL_NAME, "");
- int importance = NotificationManager.IMPORTANCE_DEFAULT;
-
- boolean isVibrate = PreferenceUtils.getPrefBoolean(App.getInstance(), PreferenceConstants.VIBRATE_NOTIFY, true);
- boolean isVoice = PreferenceUtils.getPrefBoolean(App.getInstance(), PreferenceConstants.VOICE_NOTIFY, true);
- //解决通知声音、震动无法关闭或开启的问题
- if (TextUtils.isEmpty(CHANNEL_ID)) {
- CHANNEL_ID = "kuda_channel" + new Random().nextInt(100000);
- PreferenceUtils.setPrefString(this, PreferenceConstants.CHANNEL_ID, CHANNEL_ID);
- CHANNEL_NAME = getString(R.string.app_name) + new Random().nextInt(100000);
- PreferenceUtils.setPrefString(this, PreferenceConstants.CHANNEL_NAME, CHANNEL_NAME);
- }
-
- NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, importance);
- // 配置通知渠道的属性
- channel.setDescription("this is a nofifaction description !");
- // 设置通知出现时的闪灯(如果 android 设备支持的话)
- channel.enableLights(true);
- channel.setLightColor(Color.BLUE);
- if (isVibrate) {
- // 设置通知出现时的震动(如果 android 设备支持的话)
- channel.enableVibration(true);
- //如上设置使手机:静止1秒,震动2秒,静止1秒,震动3秒
- channel.setVibrationPattern(new long[]{1000, 500, 2000});
- } else {
- // 设置通知出现时不震动
- channel.enableVibration(false);
- channel.setVibrationPattern(new long[]{0});
- }
-
- if (isVoice) {
- channel.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION), null);
- } else {
- channel.setSound(null, null);
- }
- //把渠道添加到通知中
- manager.createNotificationChannel(channel);
-
- //设置跳转的页面
- PendingIntent intent = PendingIntent.getActivity(this,
- 100, new Intent(this, MainActivity.class),
- PendingIntent.FLAG_UPDATE_CURRENT);
-
- NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
- .setContentTitle(title)
- .setContentText(message)
- .setContentIntent(intent)
- .setWhen(System.currentTimeMillis())
- .setSmallIcon(R.mipmap.ic_cds_launcher)
- .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_cds_launcher))
- .setLights(Color.BLUE, 2000, 1000)
- .setAutoCancel(true);
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
- builder.setVisibility(Notification.VISIBILITY_PUBLIC);
- // 关联PendingIntent
- builder.setFullScreenIntent(intent, false);// 横幅
- }
-
- Notification notification = builder.build();
- manager.notify(2, notification);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。