Invalid channel for startForeground












1















I have this crash in production which I cannot reproduce and have no idea why it's happening.



android.app.RemoteServiceException: (Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=null pri=0 contentView=null vibrate=null sound=null tick defaults=0x0 flags=0x4a color=0x00000000 vis=PRIVATE))
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1771)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6518)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)


That sounds to me like the channel wasn't created but I create the channel every time before I create the notification.



I'm not even sure that it's happening in this part of the code but this is the only place where I'm starting a foreground service so I assume I'm on the right track



Here's the code



String channelId = NotificationChannelConstants.MAIN_NOTIFICATION_CHANNEL_ID;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelName = NotificationChannelConstants.MAIN_NOTIFICATION_CHANNEL_NAME;
NotificationChannel chan = new NotificationChannel(channelId,
channelName, NotificationManager.IMPORTANCE_LOW);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager service = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
service.createNotificationChannel(chan);
}

notificationBuilder = new NotificationCompat.Builder(this, channelId)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.sb_unknown)
.setContentTitle(getString(R.string.app_name))
.setTicker(getString(R.string.app_name)).setContentIntent(broadcastIntent)
.setContentText(mNotificationText)
.setAutoCancel(false)
.setOngoing(true)
.setOnlyAlertOnce(true);

startForeground(R.id.notification, notificationBuilder.build());









share|improve this question























  • This crash occur on Android 8.1?

    – Alex
    Nov 27 '18 at 11:17











  • @Alex Yes only on 8.1

    – noev
    Nov 27 '18 at 11:19











  • Did you added permission in manifest? <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

    – Alex
    Nov 27 '18 at 11:27











  • I have added it yesterday but haven't pushed it to production yet. I know it's required for the foreground service to run but this crash message sounds like it would have nothing to do with it?

    – noev
    Nov 27 '18 at 11:31











  • Do you able to reproduce this crash on your device/emulator running on Android 8.1? If yes, you can test if the missing permission fixed the issue.

    – Alex
    Nov 27 '18 at 11:33
















1















I have this crash in production which I cannot reproduce and have no idea why it's happening.



android.app.RemoteServiceException: (Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=null pri=0 contentView=null vibrate=null sound=null tick defaults=0x0 flags=0x4a color=0x00000000 vis=PRIVATE))
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1771)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6518)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)


That sounds to me like the channel wasn't created but I create the channel every time before I create the notification.



I'm not even sure that it's happening in this part of the code but this is the only place where I'm starting a foreground service so I assume I'm on the right track



Here's the code



String channelId = NotificationChannelConstants.MAIN_NOTIFICATION_CHANNEL_ID;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelName = NotificationChannelConstants.MAIN_NOTIFICATION_CHANNEL_NAME;
NotificationChannel chan = new NotificationChannel(channelId,
channelName, NotificationManager.IMPORTANCE_LOW);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager service = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
service.createNotificationChannel(chan);
}

notificationBuilder = new NotificationCompat.Builder(this, channelId)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.sb_unknown)
.setContentTitle(getString(R.string.app_name))
.setTicker(getString(R.string.app_name)).setContentIntent(broadcastIntent)
.setContentText(mNotificationText)
.setAutoCancel(false)
.setOngoing(true)
.setOnlyAlertOnce(true);

startForeground(R.id.notification, notificationBuilder.build());









share|improve this question























  • This crash occur on Android 8.1?

    – Alex
    Nov 27 '18 at 11:17











  • @Alex Yes only on 8.1

    – noev
    Nov 27 '18 at 11:19











  • Did you added permission in manifest? <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

    – Alex
    Nov 27 '18 at 11:27











  • I have added it yesterday but haven't pushed it to production yet. I know it's required for the foreground service to run but this crash message sounds like it would have nothing to do with it?

    – noev
    Nov 27 '18 at 11:31











  • Do you able to reproduce this crash on your device/emulator running on Android 8.1? If yes, you can test if the missing permission fixed the issue.

    – Alex
    Nov 27 '18 at 11:33














1












1








1


1






I have this crash in production which I cannot reproduce and have no idea why it's happening.



android.app.RemoteServiceException: (Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=null pri=0 contentView=null vibrate=null sound=null tick defaults=0x0 flags=0x4a color=0x00000000 vis=PRIVATE))
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1771)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6518)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)


That sounds to me like the channel wasn't created but I create the channel every time before I create the notification.



I'm not even sure that it's happening in this part of the code but this is the only place where I'm starting a foreground service so I assume I'm on the right track



Here's the code



String channelId = NotificationChannelConstants.MAIN_NOTIFICATION_CHANNEL_ID;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelName = NotificationChannelConstants.MAIN_NOTIFICATION_CHANNEL_NAME;
NotificationChannel chan = new NotificationChannel(channelId,
channelName, NotificationManager.IMPORTANCE_LOW);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager service = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
service.createNotificationChannel(chan);
}

notificationBuilder = new NotificationCompat.Builder(this, channelId)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.sb_unknown)
.setContentTitle(getString(R.string.app_name))
.setTicker(getString(R.string.app_name)).setContentIntent(broadcastIntent)
.setContentText(mNotificationText)
.setAutoCancel(false)
.setOngoing(true)
.setOnlyAlertOnce(true);

startForeground(R.id.notification, notificationBuilder.build());









share|improve this question














I have this crash in production which I cannot reproduce and have no idea why it's happening.



android.app.RemoteServiceException: (Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=null pri=0 contentView=null vibrate=null sound=null tick defaults=0x0 flags=0x4a color=0x00000000 vis=PRIVATE))
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1771)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6518)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)


That sounds to me like the channel wasn't created but I create the channel every time before I create the notification.



I'm not even sure that it's happening in this part of the code but this is the only place where I'm starting a foreground service so I assume I'm on the right track



Here's the code



String channelId = NotificationChannelConstants.MAIN_NOTIFICATION_CHANNEL_ID;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelName = NotificationChannelConstants.MAIN_NOTIFICATION_CHANNEL_NAME;
NotificationChannel chan = new NotificationChannel(channelId,
channelName, NotificationManager.IMPORTANCE_LOW);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager service = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
service.createNotificationChannel(chan);
}

notificationBuilder = new NotificationCompat.Builder(this, channelId)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.sb_unknown)
.setContentTitle(getString(R.string.app_name))
.setTicker(getString(R.string.app_name)).setContentIntent(broadcastIntent)
.setContentText(mNotificationText)
.setAutoCancel(false)
.setOngoing(true)
.setOnlyAlertOnce(true);

startForeground(R.id.notification, notificationBuilder.build());






android android-notifications foreground-service






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 27 '18 at 11:09









noevnoev

502317




502317













  • This crash occur on Android 8.1?

    – Alex
    Nov 27 '18 at 11:17











  • @Alex Yes only on 8.1

    – noev
    Nov 27 '18 at 11:19











  • Did you added permission in manifest? <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

    – Alex
    Nov 27 '18 at 11:27











  • I have added it yesterday but haven't pushed it to production yet. I know it's required for the foreground service to run but this crash message sounds like it would have nothing to do with it?

    – noev
    Nov 27 '18 at 11:31











  • Do you able to reproduce this crash on your device/emulator running on Android 8.1? If yes, you can test if the missing permission fixed the issue.

    – Alex
    Nov 27 '18 at 11:33



















  • This crash occur on Android 8.1?

    – Alex
    Nov 27 '18 at 11:17











  • @Alex Yes only on 8.1

    – noev
    Nov 27 '18 at 11:19











  • Did you added permission in manifest? <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

    – Alex
    Nov 27 '18 at 11:27











  • I have added it yesterday but haven't pushed it to production yet. I know it's required for the foreground service to run but this crash message sounds like it would have nothing to do with it?

    – noev
    Nov 27 '18 at 11:31











  • Do you able to reproduce this crash on your device/emulator running on Android 8.1? If yes, you can test if the missing permission fixed the issue.

    – Alex
    Nov 27 '18 at 11:33

















This crash occur on Android 8.1?

– Alex
Nov 27 '18 at 11:17





This crash occur on Android 8.1?

– Alex
Nov 27 '18 at 11:17













@Alex Yes only on 8.1

– noev
Nov 27 '18 at 11:19





@Alex Yes only on 8.1

– noev
Nov 27 '18 at 11:19













Did you added permission in manifest? <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

– Alex
Nov 27 '18 at 11:27





Did you added permission in manifest? <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

– Alex
Nov 27 '18 at 11:27













I have added it yesterday but haven't pushed it to production yet. I know it's required for the foreground service to run but this crash message sounds like it would have nothing to do with it?

– noev
Nov 27 '18 at 11:31





I have added it yesterday but haven't pushed it to production yet. I know it's required for the foreground service to run but this crash message sounds like it would have nothing to do with it?

– noev
Nov 27 '18 at 11:31













Do you able to reproduce this crash on your device/emulator running on Android 8.1? If yes, you can test if the missing permission fixed the issue.

– Alex
Nov 27 '18 at 11:33





Do you able to reproduce this crash on your device/emulator running on Android 8.1? If yes, you can test if the missing permission fixed the issue.

– Alex
Nov 27 '18 at 11:33












1 Answer
1






active

oldest

votes


















0














You can use this for version Oreo and higher



if you use service class you can put this code



@Override
public void onCreate(){
super.onCreate();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
startMyOwnForeground();
else
startForeground(1, new Notification());
}

private void startMyOwnForeground(){
String NOTIFICATION_CHANNEL_ID = "com.example.simpleapp";
String channelName = "My Background Service";
NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
chan.setLightColor(Color.BLUE);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
assert manager != null;
manager.createNotificationChannel(chan);

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
Notification notification = notificationBuilder.setOngoing(true)
.setSmallIcon(R.drawable.icon_1)
.setContentTitle("App is running in background")
.setPriority(NotificationManager.IMPORTANCE_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.build();
startForeground(2, notification);
}


Of course don not forget ForeGroundService Permission your AndroidManifest.xml



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





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53498351%2finvalid-channel-for-startforeground%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You can use this for version Oreo and higher



    if you use service class you can put this code



    @Override
    public void onCreate(){
    super.onCreate();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
    startMyOwnForeground();
    else
    startForeground(1, new Notification());
    }

    private void startMyOwnForeground(){
    String NOTIFICATION_CHANNEL_ID = "com.example.simpleapp";
    String channelName = "My Background Service";
    NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
    chan.setLightColor(Color.BLUE);
    chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    assert manager != null;
    manager.createNotificationChannel(chan);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
    Notification notification = notificationBuilder.setOngoing(true)
    .setSmallIcon(R.drawable.icon_1)
    .setContentTitle("App is running in background")
    .setPriority(NotificationManager.IMPORTANCE_MIN)
    .setCategory(Notification.CATEGORY_SERVICE)
    .build();
    startForeground(2, notification);
    }


    Of course don not forget ForeGroundService Permission your AndroidManifest.xml



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





    share|improve this answer




























      0














      You can use this for version Oreo and higher



      if you use service class you can put this code



      @Override
      public void onCreate(){
      super.onCreate();
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
      startMyOwnForeground();
      else
      startForeground(1, new Notification());
      }

      private void startMyOwnForeground(){
      String NOTIFICATION_CHANNEL_ID = "com.example.simpleapp";
      String channelName = "My Background Service";
      NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
      chan.setLightColor(Color.BLUE);
      chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
      NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
      assert manager != null;
      manager.createNotificationChannel(chan);

      NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
      Notification notification = notificationBuilder.setOngoing(true)
      .setSmallIcon(R.drawable.icon_1)
      .setContentTitle("App is running in background")
      .setPriority(NotificationManager.IMPORTANCE_MIN)
      .setCategory(Notification.CATEGORY_SERVICE)
      .build();
      startForeground(2, notification);
      }


      Of course don not forget ForeGroundService Permission your AndroidManifest.xml



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





      share|improve this answer


























        0












        0








        0







        You can use this for version Oreo and higher



        if you use service class you can put this code



        @Override
        public void onCreate(){
        super.onCreate();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        startMyOwnForeground();
        else
        startForeground(1, new Notification());
        }

        private void startMyOwnForeground(){
        String NOTIFICATION_CHANNEL_ID = "com.example.simpleapp";
        String channelName = "My Background Service";
        NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
        chan.setLightColor(Color.BLUE);
        chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        assert manager != null;
        manager.createNotificationChannel(chan);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
        Notification notification = notificationBuilder.setOngoing(true)
        .setSmallIcon(R.drawable.icon_1)
        .setContentTitle("App is running in background")
        .setPriority(NotificationManager.IMPORTANCE_MIN)
        .setCategory(Notification.CATEGORY_SERVICE)
        .build();
        startForeground(2, notification);
        }


        Of course don not forget ForeGroundService Permission your AndroidManifest.xml



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





        share|improve this answer













        You can use this for version Oreo and higher



        if you use service class you can put this code



        @Override
        public void onCreate(){
        super.onCreate();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        startMyOwnForeground();
        else
        startForeground(1, new Notification());
        }

        private void startMyOwnForeground(){
        String NOTIFICATION_CHANNEL_ID = "com.example.simpleapp";
        String channelName = "My Background Service";
        NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
        chan.setLightColor(Color.BLUE);
        chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        assert manager != null;
        manager.createNotificationChannel(chan);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
        Notification notification = notificationBuilder.setOngoing(true)
        .setSmallIcon(R.drawable.icon_1)
        .setContentTitle("App is running in background")
        .setPriority(NotificationManager.IMPORTANCE_MIN)
        .setCategory(Notification.CATEGORY_SERVICE)
        .build();
        startForeground(2, notification);
        }


        Of course don not forget ForeGroundService Permission your AndroidManifest.xml



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






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Feb 8 at 8:46









        DenizDeniz

        111




        111
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53498351%2finvalid-channel-for-startforeground%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

            Calculate evaluation metrics using cross_val_predict sklearn

            Insert data from modal to MySQL (multiple modal on website)