@RequiresApi(api = Build.VERSION_CODES.O) private static Notification.Builder getChannelNotification(Context mContext, String id, String title, String messsage) { return new Notification.Builder(mContext, id) .setContentTitle(title) .setContentText(messsage) .setOngoing(true) .setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.ic_launcher)) .setSmallIcon(R.mipmap.ic_launcher) .setAutoCancel(true); }
private static void buildnotify(Context context, String title, String content, String voice, Class classname) { boolean isMsgVoice = PreferencesHelper.getBooleanData(PreferenceKey.SET_VOICE); boolean isMsgShake = PreferencesHelper.getBooleanData(PreferenceKey.SET_SHAKE); if (index > 100) { index = 0; } if (!TextUtils.isEmpty(title) && !TextUtils.isEmpty(content)) { NotificationManager manager = (NotificationManager) context .getSystemService(NOTIFICATION_SERVICE); Notification noti; ++index; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel notificationChannel = new NotificationChannel(String.valueOf(index), "温馨提醒", NotificationManager.IMPORTANCE_LOW); notificationChannel.enableLights(false); notificationChannel.setLightColor(Color.RED); notificationChannel.enableVibration(false); if (manager != null) { manager.createNotificationChannel(notificationChannel); } Notification.Builder mBuilderO = getChannelNotification(context,String.valueOf(index),title,content); noti = mBuilderO.build(); }else{ NotificationCompat.Builder builder = new NotificationCompat.Builder(context); builder.setContentTitle(title); builder.setSmallIcon(R.mipmap.ic_launcher); builder.setContentText(content); Intent intent = new Intent(context, classname); PendingIntent pi = PendingIntent.getActivity(context, 10, intent, 0); builder.setContentIntent(pi); if (isMsgShake) { long[] pattern = {10, 10, 20}; builder.setVibrate(pattern); } noti = builder.build(); } if (isMsgShake) { noti.defaults = Notification.DEFAULT_VIBRATE; } noti.flags = Notification.FLAG_AUTO_CANCEL; if (manager != null) { manager.notify(index, noti); } if (isMsgVoice) { SysUtils.ring(context, voice, index); } } }