private void setNotification(String reminderTitle, String reminderId) {
int num = Integer.parseInt(SharePrefUtils.getString(Constant_ad.currentNotificationId, “0”));
num++;
SharePrefUtils.putString(Constant_ad.currentNotificationId, String.valueOf(num));
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder notificationBuilder;
Log.e("getVibrateStatus", SharePrefUtils.getBoolean(Constant_ad.IS_VIBRATE, false) + "");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, getString(R.string.channel_name)
, NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setDescription(getString(R.string.channel_description));
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
if (SharePrefUtils.getBoolean(Constant_ad.IS_VIBRATE, false)){
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
}
// notificationChannel.enableVibration(true);
notificationChannel.enableVibration(SharePrefUtils.getBoolean(Constant_ad.IS_VIBRATE, false));
notificationManager.createNotificationChannel(notificationChannel);
}
notificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID);
notificationBuilder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setPriority(Notification.PRIORITY_MAX)
.setContentTitle(getString(R.string.notification_ctitle))
.setContentText(reminderTitle);
Intent notifyIntent = new Intent(this, RemindersActivity.class);
notifyIntent.putExtra("type", "notificationData");
notifyIntent.putExtra("id", reminderId);
notifyIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
int contentIntentFlags = PendingIntent.FLAG_CANCEL_CURRENT;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
contentIntentFlags |= PendingIntent.FLAG_IMMUTABLE;
PendingIntent pendingIntent = PendingIntent.getActivity(this, Integer.parseInt(SharePrefUtils.getString(Constant_ad.currentNotificationId, "0"))
, notifyIntent, contentIntentFlags);
notificationBuilder.setContentIntent(pendingIntent);
notificationManager.notify(Integer.parseInt(SharePrefUtils.getString(Constant_ad.currentNotificationId, "0"))
, notificationBuilder.build());
}
Here is my notification function.
Dev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.