i have been working on night filter app but i stuck with sdk version. My app is perfectly working on device whose sdk version is less then 23 but above 23 is not working it’s like when i tested my app in my Xiaomi which has android 9 app crashes and when i track the logcat error will through stating Bad Notification For StartForeGround. see this logcat message
2021-01-05 13:24:04.295 17312-17312/delhisehai.eyecare E/AndroidRuntime: FATAL EXCEPTION: main Process: delhisehai.eyecare, PID: 17312 android.app.RemoteServiceException: Bad notification for startForeground at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1760) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:201) at android.app.ActivityThread.main(ActivityThread.java:6823) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
This is my Service activity
private void updateNotification() { Intent intent = new Intent((Context)this, MyBroadcastReceiver.class); intent.setAction("ACTION_SWTICH_FILTER"); PendingIntent pendingIntent = PendingIntent.getBroadcast((Context)this, (int)0, (Intent)intent, (int)0); Intent intent2 = new Intent((Context)this, MyBroadcastReceiver.class); intent2.setAction("ADJUST_INTENSITY"); intent2.putExtra("intensity", "add"); PendingIntent pendingIntent2 = PendingIntent.getBroadcast((Context)this, (int)1, (Intent)intent2, PendingIntent.FLAG_UPDATE_CURRENT); Intent intent3 = new Intent((Context)this, MyBroadcastReceiver.class); intent3.setAction("ADJUST_INTENSITY"); intent3.putExtra("intensity", "minus"); PendingIntent pendingIntent3 = PendingIntent.getBroadcast((Context)this, (int)2, (Intent)intent3, PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent pendingIntent4 = PendingIntent.getActivity((Context)this, (int)0, (Intent)new Intent((Context)this, MainActivity.class), (int)0); String string2 = getResources().getString(R.string.app_name); int n = MySharedPreferences.getAlpha((Context)this); RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.layout_notification); remoteViews.setTextViewText(R.id.notitext1, (CharSequence)string2); remoteViews.setTextViewText(R.id.notitext4, (CharSequence)((int)(100.0 * (double)n / 200.0) + "%")); remoteViews.setOnClickPendingIntent(R.id.btn1, pendingIntent); remoteViews.setOnClickPendingIntent(R.id.btn2, pendingIntent); remoteViews.setOnClickPendingIntent(R.id.notitext3, pendingIntent2); remoteViews.setOnClickPendingIntent(R.id.notitext45, pendingIntent3); if (filterIsOn) { remoteViews.setViewVisibility(R.id.notitext2, View.INVISIBLE); remoteViews.setViewVisibility(R.id.notilayout, View.VISIBLE); remoteViews.setViewVisibility(R.id.btn2, View.INVISIBLE); remoteViews.setViewVisibility(R.id.btn1, View.VISIBLE); remoteViews.setTextViewText(R.id.btn1, (CharSequence)this.getResources().getString(R.string.turn_on)); } else { remoteViews.setViewVisibility(R.id.notitext2, View.VISIBLE); remoteViews.setViewVisibility(R.id.notilayout, View.INVISIBLE); remoteViews.setViewVisibility(R.id.btn2, View.VISIBLE); remoteViews.setViewVisibility(R.id.btn1, View.INVISIBLE); remoteViews.setTextViewText(R.id.btn2, (CharSequence)this.getResources().getString(R.string.turn_off)); } Notification notification = new NotificationCompat.Builder(getApplicationContext()).setOngoing(false).setSmallIcon(R.drawable.red1).setContentIntent(pendingIntent4).setPriority(2).setWhen(0L).setContent(remoteViews).build(); if (!MySharedPreferences.getShowIcon((Context)this)) { notification = new NotificationCompat.Builder(getApplicationContext()).setOngoing(false).setSmallIcon(R.drawable.red1).setContentIntent(pendingIntent4).setPriority(-2).setWhen(0L).setContent(remoteViews).build(); } startForeground(9216, notification); }
I have using remoteviews to show notification and manipulate there only like music player but the problem is how to tacke this bug. I also use startforeground permission in manifest but same problem app crash. I think there is problem in Notification ID which i set to 9216. so can anyone give me solution for same.
Thanks in Advance!
Advertisement
Answer
I understand the issue. Every source code isn’t compatible with every android device. Something just like your source code. There’s lot of code you wrote. That’s why I can’t re-edit your whole source code. I am just giving a demo. To make compatible notification with android devices.
You can get that repo in my github also.
public void showNotification() { NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,"channelID") .setSmallIcon(R.drawable.ic_launcher_background) .setContentTitle("Notification") .setContentText("Hello! This is a notification.") .setAutoCancel(true); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); int notificationId = 1; createChannel(notificationManager); notificationManager.notify(notificationId, notificationBuilder.build()); } public void createChannel(NotificationManager notificationManager){ if (Build.VERSION.SDK_INT < 26) { return; } NotificationChannel channel = new NotificationChannel("channelID","name", NotificationManager.IMPORTANCE_DEFAULT); channel.setDescription("Hello! This is a notification."); notificationManager.createNotificationChannel(channel); }