0

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!

Ryan M
  • 18,333
  • 31
  • 67
  • 74
  • 1
    i guess android 11 and above need foreground service mandatory. May be try with other device which are old and latest should help you understand whats happening. – Nitin Tej Jan 05 '21 at 08:26
  • Yeah...I had my old lenovo vibe k5 when I used to run my app then it perfectly fine and there is no app crashes but devices whose SDK version is greater then 23 give problem like app crashes. – PArikshit KAmat Jan 05 '21 at 08:47
  • 1
    try creating the foreground notification from stratch. Some better way if it exists – Nitin Tej Jan 06 '21 at 09:32

0 Answers0