1

I have looked at all the SO posts related to this error, that I could find on Google. Most of them were about the requirement to add CHANNEL_ID in Android 8. Others were due to missing pieces of code, that I think are fixed in mine.

I have referred this article, and tried implementing the same. I am looking to send a Full Screen Intent notification.

Notifier.java

public class Notifier extends Service {
    @Override
    public void onCreate() {
        super.onCreate();
        Context context = this;

        Intent fullScreenIntent = new Intent(context, CamView.class);
        PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(context, 0,
                fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        CharSequence name = getString(R.string.channel_name);
        String description = getString(R.string.channel_description);
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel doorBellChannel = new NotificationChannel(getString(R.string.channel_name), name, importance);
        doorBellChannel.setDescription(description);
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(doorBellChannel);



        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(context, String.valueOf(R.string.channel_name))
                        //.setChannelId(String.valueOf(R.string.channel_name))
                        .setSmallIcon(R.drawable.ic_launcher_foreground)
                        .setContentTitle("Incoming call")
                        .setContentText("(919) 555-1234")
                        .setPriority(NotificationCompat.PRIORITY_MAX)
                        .setCategory(NotificationCompat.CATEGORY_CALL)
                        .setChannelId(String.valueOf(R.string.channel_name))
                        .setFullScreenIntent(fullScreenPendingIntent, true)
                        .setAutoCancel(true);




        Notification incomingCallNotification = notificationBuilder.build();

        Log.d(TAG, "onCreate: Here 1");

        int notificationId = createID();
        startForeground(notificationId, incomingCallNotification);


        Log.d(TAG, "onCreate: Here 2");

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {


        return super.onStartCommand(intent, flags, startId);
    }

    public int createID(){
        Date now = new Date();
        return Integer.parseInt(new SimpleDateFormat("ddHHmmss",  Locale.US).format(now));
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

I have these 2 lines in the AndroidManifest.xml:

    <uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

When the service is called, I get the following error:

2021-06-11 17:17:27.213 9432-9432/? D/Oscillator: onCreate: Here 1
2021-06-11 17:17:27.231 9432-9432/? D/Oscillator: onCreate: Here 2
2021-06-11 17:17:27.240 9432-9432/? D/AndroidRuntime: Shutting down VM
2021-06-11 17:17:27.242 9432-9432/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.smartlock, PID: 9432
    android.app.RemoteServiceException: Bad notification for startForeground
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1946)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7397)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:935)
2021-06-11 17:17:27.260 9432-9432/? I/Process: Sending signal. PID: 9432 SIG: 9
Dinesh
  • 1,410
  • 2
  • 16
  • 29
Manish S
  • 113
  • 1
  • 5

1 Answers1

2

I think, the problem is here. String.valueOf(R.string.channel_name) just gives a string containing the integer ID. As a result, due to unknown/unregistered channel, you got bad notification error. Hence, use the actual String.

NotificationCompat.Builder notificationBuilder = /* Do not use String.valueOf() */
                new NotificationCompat.Builder(context, getString(R.string.channel_name))
                        .setSmallIcon(R.drawable.ic_launcher_foreground)
                        .setContentTitle("Incoming call")
                        .setContentText("(919) 555-1234")
                        .setPriority(NotificationCompat.PRIORITY_MAX)
                        .setCategory(NotificationCompat.CATEGORY_CALL)
                        .setChannelId(name) /* Do not use String.valueOf() */
                        .setFullScreenIntent(fullScreenPendingIntent, true)
                        .setAutoCancel(true);
Ananta Raha
  • 1,011
  • 5
  • 14
  • Are you suggesting getString, instead of String.valueOf? Also, I have tried putting the same string, as a value, within double quotes earlier too. That didn't work either. Will try this tomorrow morning and get back. – Manish S Jun 11 '21 at 18:14
  • I actually just understood the difference between getString and String.valueOf. Makes more sense now. Don't know how I made such a mistake. Still, yet to try it. – Manish S Jun 11 '21 at 18:16