I have used local notification in my app. User will get notification on daily basis.It is working fine in below 7 version but not in 8.0.
why Normal Notification Builder doesn't show notifications on Android O.
How could I show notification on Android 8 Oreo?
Is there any new piece of code to add for showing notification on Android O?
here is my Notification Service code this worked on android 7 but in android 8 is not worked
public class NotificationService extends Service {
private static final String NOTIFICATION_DELETED_ACTION = "NOTIFICATION_DELETED";
boolean canceledNotification = false;
int imageId;
int largeImageId;
String lastCancelDate = "";
Methods methods = new Methods();
int notifyID = 1000;
private final BroadcastReceiver receiver = new C03671();
String title;
CalendarTool tool = new CalendarTool();
int updateMinutes = 0;
public void onReceive(Context context, Intent intent) {
Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit();
editor.putBoolean("CANCELED_NOTIFICATION", true);
editor.putString("CANCELED_DATE", NotificationService.this.methods.calendarToString(Calendar.getInstance(), Methods.DATEFORMAT));
editor.apply();
NotificationService.this.unregisterReceiver(this);
}
}
public IBinder onBind(Intent intent) {
return null;
}
@SuppressLint("WrongConstant")
public int onStartCommand(Intent intent, int flags, int startId) {
this.tool = new CalendarTool();
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
boolean showNotification = sharedPref.getBoolean("SHOW_NOTIFICATION", true);
this.canceledNotification = sharedPref.getBoolean("CANCELED_NOTIFICATION", false);
this.lastCancelDate = sharedPref.getString("CANCELED_DATE", "");
this.updateMinutes = sharedPref.getInt("UPDATE_NOTIFICATION_MIN", 0);
if (showNotification) {
Bitmap bitmap;
this.title = this.methods.ReplaceNumber(this.tool.getJalaliStringLong());
this.imageId = getResources().getIdentifier("d" + String.valueOf(this.tool.getIranianDay()), "drawable", getPackageName());
this.largeImageId = getResources().getIdentifier("date" + String.valueOf(this.tool.getIranianDay()), "drawable", getPackageName());
if (VERSION.SDK_INT < 24) {
bitmap = this.methods.getFontBitmap(this, this.methods.ReplaceNumber(String.valueOf(this.tool.getIranianDay())), ContextCompat.getColor(this, R.color.colorPrimary), 50, "nazanin.ttf");
} else {
bitmap = BitmapFactory.decodeResource(getResources(), this.largeImageId);
}
int[] attrs = new int[]{16842904};
TypedArray titleStyle = obtainStyledAttributes(R.style.NotificationTitle, attrs);
TypedArray textStyle = obtainStyledAttributes(R.style.NotificationText, attrs);
int titleColor = titleStyle.getColor(0, ViewCompat.MEASURED_STATE_MASK);
int textColor = textStyle.getColor(0, -7829368);
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification_layout);
contentView.setImageViewBitmap(R.id.notifImage, bitmap);
contentView.setImageViewBitmap(R.id.notifTitle, this.methods.getFontBitmap(this, this.title, titleColor, 16, "iranyekanregular.ttf"));
contentView.setImageViewBitmap(R.id.notifGregText, this.methods.getFontBitmap(this, this.methods.ReplaceNumber(this.tool.getGregorianStringFarsiShort()), textColor, 14, "iranyekanregular.ttf"));
contentView.setImageViewBitmap(R.id.notifLunarText, this.methods.getFontBitmap(this, this.methods.ReplaceNumber(this.tool.getLunarStringShort()), textColor, 14, "iranyekanregular.ttf"));
contentView.setImageViewBitmap(R.id.notifSpaceText, this.methods.getFontBitmap(this, " - ", textColor, 14, "iranyekanregular.ttf"));
titleStyle.recycle();
textStyle.recycle();
PendingIntent deletePendIntent;
NotificationManager mNotificationManager;
Intent intent2;
Builder mBuilder;
if (this.canceledNotification) {
Date lastDate = this.methods.StringDateToDate(this.lastCancelDate, Methods.DATEFORMAT);
Calendar lastCalendar = Calendar.getInstance();
lastCalendar.setTime(lastDate);
Calendar nowCalendar = Calendar.getInstance();
lastCalendar.add(12, this.updateMinutes);
if (nowCalendar.getTime().after(lastCalendar.getTime())) {
deletePendIntent = PendingIntent.getBroadcast(this, 0, new Intent(NOTIFICATION_DELETED_ACTION), 0);
registerReceiver(this.receiver, new IntentFilter(NOTIFICATION_DELETED_ACTION));
mNotificationManager = (NotificationManager) getSystemService("notification");
intent2 = new Intent(this, MainActivity.class);
intent2.setFlags(603979776);
mBuilder = new Builder(this).setSmallIcon(this.imageId).setContent(contentView).setDeleteIntent(deletePendIntent).setContentIntent(PendingIntent.getActivity(this, 0, intent2, 0));
if (this.updateMinutes == 0) {
mBuilder.setAutoCancel(false).setOngoing(true);
} else {
mBuilder.setAutoCancel(true);
}
mNotificationManager.notify(this.notifyID, mBuilder.build());
}
} else {
deletePendIntent = PendingIntent.getBroadcast(this, 0, new Intent(NOTIFICATION_DELETED_ACTION), 0);
registerReceiver(this.receiver, new IntentFilter(NOTIFICATION_DELETED_ACTION));
mNotificationManager = (NotificationManager) getSystemService("notification");
intent2 = new Intent(this, MainActivity.class);
intent2.setFlags(603979776);
mBuilder = new Builder(this).setSmallIcon(this.imageId).setContent(contentView).setDeleteIntent(deletePendIntent).setContentIntent(PendingIntent.getActivity(this, 0, intent2, 0));
if (this.updateMinutes == 0) {
mBuilder.setAutoCancel(false).setOngoing(true);
} else {
mBuilder.setAutoCancel(true);
}
mNotificationManager.notify(this.notifyID, mBuilder.build());
}
}
return 1;
}
}