Notification not showing when app is in foreground in Kotlin












0















I am using Firebase push notification in kotlin and below are the code snippet for showing push notification



 mNotifyManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
createChannel(mNotifyManager)
val mBuilder = NotificationCompat.Builder(this, "bks-channel")
.setLargeIcon(largeIcon)
.setContentTitle("Bks")
.setSmallIcon(R.drawable.app_icon)
.setContentText(message)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
mNotifyManager.notify(getRandomNumber(), mBuilder.build())


And the createChannel() function is :



    @TargetApi(26)
private fun createChannel(notificationManager: NotificationManager)
{
val name = "bks"
val description = "bks"
val importance = NotificationManager.IMPORTANCE_DEFAULT

val mChannel = NotificationChannel(name, name, importance)
mChannel.description = description
mChannel.enableLights(true)
mChannel.lightColor = Color.BLUE
notificationManager.createNotificationChannel(mChannel)
}


Below are the server logs:



 array(1) {
[9]=>
array(2) {
["name"]=>
string(14) "ABL Staff USER"
["fcm_response"]=>
array(2) {
["fields"]=>
array(3) {
["data"]=>
array(4) {
["click_action"]=>
int(2)
["title"]=>
string(19) "Attendance Reminder"
["body"]=>
string(49) "Hi ABL Staff USER, Please mark your attendance ! "
["sound"]=>
string(7) "default"
}
["registration_ids"]=>
array(11) {
[0]=>
string(152) "device_token_1"
}
["notification"]=>
array(4) {
["click_action"]=>
int(2)
["title"]=>
string(19) "Attendance Reminder"
["body"]=>
string(49) "Hi ABL Staff USER, Please mark your attendance ! "
["sound"]=>
string(7) "default"
}
}


Same are working for below android o in foreground and background but not in android o or above










share|improve this question





























    0















    I am using Firebase push notification in kotlin and below are the code snippet for showing push notification



     mNotifyManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
    createChannel(mNotifyManager)
    val mBuilder = NotificationCompat.Builder(this, "bks-channel")
    .setLargeIcon(largeIcon)
    .setContentTitle("Bks")
    .setSmallIcon(R.drawable.app_icon)
    .setContentText(message)
    .setSound(defaultSoundUri)
    .setContentIntent(pendingIntent)
    mNotifyManager.notify(getRandomNumber(), mBuilder.build())


    And the createChannel() function is :



        @TargetApi(26)
    private fun createChannel(notificationManager: NotificationManager)
    {
    val name = "bks"
    val description = "bks"
    val importance = NotificationManager.IMPORTANCE_DEFAULT

    val mChannel = NotificationChannel(name, name, importance)
    mChannel.description = description
    mChannel.enableLights(true)
    mChannel.lightColor = Color.BLUE
    notificationManager.createNotificationChannel(mChannel)
    }


    Below are the server logs:



     array(1) {
    [9]=>
    array(2) {
    ["name"]=>
    string(14) "ABL Staff USER"
    ["fcm_response"]=>
    array(2) {
    ["fields"]=>
    array(3) {
    ["data"]=>
    array(4) {
    ["click_action"]=>
    int(2)
    ["title"]=>
    string(19) "Attendance Reminder"
    ["body"]=>
    string(49) "Hi ABL Staff USER, Please mark your attendance ! "
    ["sound"]=>
    string(7) "default"
    }
    ["registration_ids"]=>
    array(11) {
    [0]=>
    string(152) "device_token_1"
    }
    ["notification"]=>
    array(4) {
    ["click_action"]=>
    int(2)
    ["title"]=>
    string(19) "Attendance Reminder"
    ["body"]=>
    string(49) "Hi ABL Staff USER, Please mark your attendance ! "
    ["sound"]=>
    string(7) "default"
    }
    }


    Same are working for below android o in foreground and background but not in android o or above










    share|improve this question



























      0












      0








      0


      0






      I am using Firebase push notification in kotlin and below are the code snippet for showing push notification



       mNotifyManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
      createChannel(mNotifyManager)
      val mBuilder = NotificationCompat.Builder(this, "bks-channel")
      .setLargeIcon(largeIcon)
      .setContentTitle("Bks")
      .setSmallIcon(R.drawable.app_icon)
      .setContentText(message)
      .setSound(defaultSoundUri)
      .setContentIntent(pendingIntent)
      mNotifyManager.notify(getRandomNumber(), mBuilder.build())


      And the createChannel() function is :



          @TargetApi(26)
      private fun createChannel(notificationManager: NotificationManager)
      {
      val name = "bks"
      val description = "bks"
      val importance = NotificationManager.IMPORTANCE_DEFAULT

      val mChannel = NotificationChannel(name, name, importance)
      mChannel.description = description
      mChannel.enableLights(true)
      mChannel.lightColor = Color.BLUE
      notificationManager.createNotificationChannel(mChannel)
      }


      Below are the server logs:



       array(1) {
      [9]=>
      array(2) {
      ["name"]=>
      string(14) "ABL Staff USER"
      ["fcm_response"]=>
      array(2) {
      ["fields"]=>
      array(3) {
      ["data"]=>
      array(4) {
      ["click_action"]=>
      int(2)
      ["title"]=>
      string(19) "Attendance Reminder"
      ["body"]=>
      string(49) "Hi ABL Staff USER, Please mark your attendance ! "
      ["sound"]=>
      string(7) "default"
      }
      ["registration_ids"]=>
      array(11) {
      [0]=>
      string(152) "device_token_1"
      }
      ["notification"]=>
      array(4) {
      ["click_action"]=>
      int(2)
      ["title"]=>
      string(19) "Attendance Reminder"
      ["body"]=>
      string(49) "Hi ABL Staff USER, Please mark your attendance ! "
      ["sound"]=>
      string(7) "default"
      }
      }


      Same are working for below android o in foreground and background but not in android o or above










      share|improve this question
















      I am using Firebase push notification in kotlin and below are the code snippet for showing push notification



       mNotifyManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
      createChannel(mNotifyManager)
      val mBuilder = NotificationCompat.Builder(this, "bks-channel")
      .setLargeIcon(largeIcon)
      .setContentTitle("Bks")
      .setSmallIcon(R.drawable.app_icon)
      .setContentText(message)
      .setSound(defaultSoundUri)
      .setContentIntent(pendingIntent)
      mNotifyManager.notify(getRandomNumber(), mBuilder.build())


      And the createChannel() function is :



          @TargetApi(26)
      private fun createChannel(notificationManager: NotificationManager)
      {
      val name = "bks"
      val description = "bks"
      val importance = NotificationManager.IMPORTANCE_DEFAULT

      val mChannel = NotificationChannel(name, name, importance)
      mChannel.description = description
      mChannel.enableLights(true)
      mChannel.lightColor = Color.BLUE
      notificationManager.createNotificationChannel(mChannel)
      }


      Below are the server logs:



       array(1) {
      [9]=>
      array(2) {
      ["name"]=>
      string(14) "ABL Staff USER"
      ["fcm_response"]=>
      array(2) {
      ["fields"]=>
      array(3) {
      ["data"]=>
      array(4) {
      ["click_action"]=>
      int(2)
      ["title"]=>
      string(19) "Attendance Reminder"
      ["body"]=>
      string(49) "Hi ABL Staff USER, Please mark your attendance ! "
      ["sound"]=>
      string(7) "default"
      }
      ["registration_ids"]=>
      array(11) {
      [0]=>
      string(152) "device_token_1"
      }
      ["notification"]=>
      array(4) {
      ["click_action"]=>
      int(2)
      ["title"]=>
      string(19) "Attendance Reminder"
      ["body"]=>
      string(49) "Hi ABL Staff USER, Please mark your attendance ! "
      ["sound"]=>
      string(7) "default"
      }
      }


      Same are working for below android o in foreground and background but not in android o or above







      android kotlin






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 '18 at 7:04







      Rakesh kushwaha

















      asked Nov 20 '18 at 6:26









      Rakesh kushwahaRakesh kushwaha

      339




      339
























          2 Answers
          2






          active

          oldest

          votes


















          0














          FCM has sent two type of notification




          1. Notification Message

          2. Data Message


          Send the data message to show a notification when the app in the background



          For more details check the below link



          Firebase data message






          share|improve this answer































            0














            Are you creating channel by name "bks-channel" ? Looks like you are not creating bks-channel instead you are creating channel by name bks.



            Update your create channel method.



             @TargetApi(26)
            private fun createChannel(notificationManager: NotificationManager) {
            val name = "bks-channel"
            val description = "bks"
            val importance = NotificationManager.IMPORTANCE_DEFAULT
            val mChannel = NotificationChannel(name, name, importance)
            mChannel.description = description
            mChannel.enableLights(true)
            mChannel.lightColor = Color.BLUE
            notificationManager.createNotificationChannel(mChannel)
            }





            share|improve this answer
























            • It means channel-id and channel name will be same????

              – Rakesh kushwaha
              Nov 20 '18 at 7:17













            • channel ID has to be same but name can be anything.I mean what you pass to notification builder and the created channel ID has to be same.

              – Ramesh Yankati
              Nov 20 '18 at 7:31













            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%2f53387399%2fnotification-not-showing-when-app-is-in-foreground-in-kotlin%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            FCM has sent two type of notification




            1. Notification Message

            2. Data Message


            Send the data message to show a notification when the app in the background



            For more details check the below link



            Firebase data message






            share|improve this answer




























              0














              FCM has sent two type of notification




              1. Notification Message

              2. Data Message


              Send the data message to show a notification when the app in the background



              For more details check the below link



              Firebase data message






              share|improve this answer


























                0












                0








                0







                FCM has sent two type of notification




                1. Notification Message

                2. Data Message


                Send the data message to show a notification when the app in the background



                For more details check the below link



                Firebase data message






                share|improve this answer













                FCM has sent two type of notification




                1. Notification Message

                2. Data Message


                Send the data message to show a notification when the app in the background



                For more details check the below link



                Firebase data message







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 20 '18 at 6:40









                JarvisJarvis

                64411020




                64411020

























                    0














                    Are you creating channel by name "bks-channel" ? Looks like you are not creating bks-channel instead you are creating channel by name bks.



                    Update your create channel method.



                     @TargetApi(26)
                    private fun createChannel(notificationManager: NotificationManager) {
                    val name = "bks-channel"
                    val description = "bks"
                    val importance = NotificationManager.IMPORTANCE_DEFAULT
                    val mChannel = NotificationChannel(name, name, importance)
                    mChannel.description = description
                    mChannel.enableLights(true)
                    mChannel.lightColor = Color.BLUE
                    notificationManager.createNotificationChannel(mChannel)
                    }





                    share|improve this answer
























                    • It means channel-id and channel name will be same????

                      – Rakesh kushwaha
                      Nov 20 '18 at 7:17













                    • channel ID has to be same but name can be anything.I mean what you pass to notification builder and the created channel ID has to be same.

                      – Ramesh Yankati
                      Nov 20 '18 at 7:31


















                    0














                    Are you creating channel by name "bks-channel" ? Looks like you are not creating bks-channel instead you are creating channel by name bks.



                    Update your create channel method.



                     @TargetApi(26)
                    private fun createChannel(notificationManager: NotificationManager) {
                    val name = "bks-channel"
                    val description = "bks"
                    val importance = NotificationManager.IMPORTANCE_DEFAULT
                    val mChannel = NotificationChannel(name, name, importance)
                    mChannel.description = description
                    mChannel.enableLights(true)
                    mChannel.lightColor = Color.BLUE
                    notificationManager.createNotificationChannel(mChannel)
                    }





                    share|improve this answer
























                    • It means channel-id and channel name will be same????

                      – Rakesh kushwaha
                      Nov 20 '18 at 7:17













                    • channel ID has to be same but name can be anything.I mean what you pass to notification builder and the created channel ID has to be same.

                      – Ramesh Yankati
                      Nov 20 '18 at 7:31
















                    0












                    0








                    0







                    Are you creating channel by name "bks-channel" ? Looks like you are not creating bks-channel instead you are creating channel by name bks.



                    Update your create channel method.



                     @TargetApi(26)
                    private fun createChannel(notificationManager: NotificationManager) {
                    val name = "bks-channel"
                    val description = "bks"
                    val importance = NotificationManager.IMPORTANCE_DEFAULT
                    val mChannel = NotificationChannel(name, name, importance)
                    mChannel.description = description
                    mChannel.enableLights(true)
                    mChannel.lightColor = Color.BLUE
                    notificationManager.createNotificationChannel(mChannel)
                    }





                    share|improve this answer













                    Are you creating channel by name "bks-channel" ? Looks like you are not creating bks-channel instead you are creating channel by name bks.



                    Update your create channel method.



                     @TargetApi(26)
                    private fun createChannel(notificationManager: NotificationManager) {
                    val name = "bks-channel"
                    val description = "bks"
                    val importance = NotificationManager.IMPORTANCE_DEFAULT
                    val mChannel = NotificationChannel(name, name, importance)
                    mChannel.description = description
                    mChannel.enableLights(true)
                    mChannel.lightColor = Color.BLUE
                    notificationManager.createNotificationChannel(mChannel)
                    }






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 20 '18 at 6:58









                    Ramesh YankatiRamesh Yankati

                    65848




                    65848













                    • It means channel-id and channel name will be same????

                      – Rakesh kushwaha
                      Nov 20 '18 at 7:17













                    • channel ID has to be same but name can be anything.I mean what you pass to notification builder and the created channel ID has to be same.

                      – Ramesh Yankati
                      Nov 20 '18 at 7:31





















                    • It means channel-id and channel name will be same????

                      – Rakesh kushwaha
                      Nov 20 '18 at 7:17













                    • channel ID has to be same but name can be anything.I mean what you pass to notification builder and the created channel ID has to be same.

                      – Ramesh Yankati
                      Nov 20 '18 at 7:31



















                    It means channel-id and channel name will be same????

                    – Rakesh kushwaha
                    Nov 20 '18 at 7:17







                    It means channel-id and channel name will be same????

                    – Rakesh kushwaha
                    Nov 20 '18 at 7:17















                    channel ID has to be same but name can be anything.I mean what you pass to notification builder and the created channel ID has to be same.

                    – Ramesh Yankati
                    Nov 20 '18 at 7:31







                    channel ID has to be same but name can be anything.I mean what you pass to notification builder and the created channel ID has to be same.

                    – Ramesh Yankati
                    Nov 20 '18 at 7:31




















                    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%2f53387399%2fnotification-not-showing-when-app-is-in-foreground-in-kotlin%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

                    'app-layout' is not a known element: how to share Component with different Modules

                    android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                    WPF add header to Image with URL pettitions [duplicate]