Reduce unnecessary calls of NotificationListenerService












0














I'm using NotificationListenerService in order to read incoming WhatsApp and Viber messages.



As from the docs, NotificationListenerService is:




A service that receives calls from the system when new notifications
are posted or removed, or their ranking changed.




So my service gets called when any kind of notification appears on the user's screen.
I'm interested whether I can somehow reduce the situations when my service is called? Because, in my case, I'm only interested in WhatsApp and Viber notifications.



Currently, I'm filtering the notifications just by putting an if condition in OnNotificationPosted() method, like this:



 @Override
public void onNotificationPosted(StatusBarNotification sbn) {

String packageName = sbn.getPackageName();

if (packageName.equalsIgnoreCase("com.viber.voip")
|| packageName.equalsIgnoreCase("com.whatsapp")) {

// my code
}
}


So, my question is:



Is it possible to somehow filter the notifications for NotificationListenerSevice so that NotificationListenerService doesn't get triggered by the notification packages that are out of my interest?



If that would be possible I could reduce the unnecessary calls of my servise.










share|improve this question





























    0














    I'm using NotificationListenerService in order to read incoming WhatsApp and Viber messages.



    As from the docs, NotificationListenerService is:




    A service that receives calls from the system when new notifications
    are posted or removed, or their ranking changed.




    So my service gets called when any kind of notification appears on the user's screen.
    I'm interested whether I can somehow reduce the situations when my service is called? Because, in my case, I'm only interested in WhatsApp and Viber notifications.



    Currently, I'm filtering the notifications just by putting an if condition in OnNotificationPosted() method, like this:



     @Override
    public void onNotificationPosted(StatusBarNotification sbn) {

    String packageName = sbn.getPackageName();

    if (packageName.equalsIgnoreCase("com.viber.voip")
    || packageName.equalsIgnoreCase("com.whatsapp")) {

    // my code
    }
    }


    So, my question is:



    Is it possible to somehow filter the notifications for NotificationListenerSevice so that NotificationListenerService doesn't get triggered by the notification packages that are out of my interest?



    If that would be possible I could reduce the unnecessary calls of my servise.










    share|improve this question



























      0












      0








      0


      0





      I'm using NotificationListenerService in order to read incoming WhatsApp and Viber messages.



      As from the docs, NotificationListenerService is:




      A service that receives calls from the system when new notifications
      are posted or removed, or their ranking changed.




      So my service gets called when any kind of notification appears on the user's screen.
      I'm interested whether I can somehow reduce the situations when my service is called? Because, in my case, I'm only interested in WhatsApp and Viber notifications.



      Currently, I'm filtering the notifications just by putting an if condition in OnNotificationPosted() method, like this:



       @Override
      public void onNotificationPosted(StatusBarNotification sbn) {

      String packageName = sbn.getPackageName();

      if (packageName.equalsIgnoreCase("com.viber.voip")
      || packageName.equalsIgnoreCase("com.whatsapp")) {

      // my code
      }
      }


      So, my question is:



      Is it possible to somehow filter the notifications for NotificationListenerSevice so that NotificationListenerService doesn't get triggered by the notification packages that are out of my interest?



      If that would be possible I could reduce the unnecessary calls of my servise.










      share|improve this question















      I'm using NotificationListenerService in order to read incoming WhatsApp and Viber messages.



      As from the docs, NotificationListenerService is:




      A service that receives calls from the system when new notifications
      are posted or removed, or their ranking changed.




      So my service gets called when any kind of notification appears on the user's screen.
      I'm interested whether I can somehow reduce the situations when my service is called? Because, in my case, I'm only interested in WhatsApp and Viber notifications.



      Currently, I'm filtering the notifications just by putting an if condition in OnNotificationPosted() method, like this:



       @Override
      public void onNotificationPosted(StatusBarNotification sbn) {

      String packageName = sbn.getPackageName();

      if (packageName.equalsIgnoreCase("com.viber.voip")
      || packageName.equalsIgnoreCase("com.whatsapp")) {

      // my code
      }
      }


      So, my question is:



      Is it possible to somehow filter the notifications for NotificationListenerSevice so that NotificationListenerService doesn't get triggered by the notification packages that are out of my interest?



      If that would be possible I could reduce the unnecessary calls of my servise.







      java android android-service






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 19 '18 at 16:06

























      asked Nov 19 '18 at 11:03









      Marija

      946




      946
























          1 Answer
          1






          active

          oldest

          votes


















          0














          From Android 8 Oreo , NotificationChannels API introduces to control Notifications.
          So Developer can code according to notification channels and club same types of notification together under one uniq notification Channel and if Requirement comes to stop this notification ,developer can check and filter to tiger notifications.



          Also Form Android 8 Oreo Android Phone user have an option to block notifications by Notification Channels for Notification Settings.



          enter image description here



          Create and Manage Notification Channels






          share|improve this answer























          • Thank you for your answer, but it's not answering my question. The thing is: My question is about NotificationListenerServise, not about Notifications in general. I'm interested in not triggering the NotificationListenerService by the packages that are out of my interest programmatically.
            – Marija
            Nov 19 '18 at 11:20













          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%2f53373235%2freduce-unnecessary-calls-of-notificationlistenerservice%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














          From Android 8 Oreo , NotificationChannels API introduces to control Notifications.
          So Developer can code according to notification channels and club same types of notification together under one uniq notification Channel and if Requirement comes to stop this notification ,developer can check and filter to tiger notifications.



          Also Form Android 8 Oreo Android Phone user have an option to block notifications by Notification Channels for Notification Settings.



          enter image description here



          Create and Manage Notification Channels






          share|improve this answer























          • Thank you for your answer, but it's not answering my question. The thing is: My question is about NotificationListenerServise, not about Notifications in general. I'm interested in not triggering the NotificationListenerService by the packages that are out of my interest programmatically.
            – Marija
            Nov 19 '18 at 11:20


















          0














          From Android 8 Oreo , NotificationChannels API introduces to control Notifications.
          So Developer can code according to notification channels and club same types of notification together under one uniq notification Channel and if Requirement comes to stop this notification ,developer can check and filter to tiger notifications.



          Also Form Android 8 Oreo Android Phone user have an option to block notifications by Notification Channels for Notification Settings.



          enter image description here



          Create and Manage Notification Channels






          share|improve this answer























          • Thank you for your answer, but it's not answering my question. The thing is: My question is about NotificationListenerServise, not about Notifications in general. I'm interested in not triggering the NotificationListenerService by the packages that are out of my interest programmatically.
            – Marija
            Nov 19 '18 at 11:20
















          0












          0








          0






          From Android 8 Oreo , NotificationChannels API introduces to control Notifications.
          So Developer can code according to notification channels and club same types of notification together under one uniq notification Channel and if Requirement comes to stop this notification ,developer can check and filter to tiger notifications.



          Also Form Android 8 Oreo Android Phone user have an option to block notifications by Notification Channels for Notification Settings.



          enter image description here



          Create and Manage Notification Channels






          share|improve this answer














          From Android 8 Oreo , NotificationChannels API introduces to control Notifications.
          So Developer can code according to notification channels and club same types of notification together under one uniq notification Channel and if Requirement comes to stop this notification ,developer can check and filter to tiger notifications.



          Also Form Android 8 Oreo Android Phone user have an option to block notifications by Notification Channels for Notification Settings.



          enter image description here



          Create and Manage Notification Channels







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 19 '18 at 11:21

























          answered Nov 19 '18 at 11:16









          Chetan Joshi

          3,48111420




          3,48111420












          • Thank you for your answer, but it's not answering my question. The thing is: My question is about NotificationListenerServise, not about Notifications in general. I'm interested in not triggering the NotificationListenerService by the packages that are out of my interest programmatically.
            – Marija
            Nov 19 '18 at 11:20




















          • Thank you for your answer, but it's not answering my question. The thing is: My question is about NotificationListenerServise, not about Notifications in general. I'm interested in not triggering the NotificationListenerService by the packages that are out of my interest programmatically.
            – Marija
            Nov 19 '18 at 11:20


















          Thank you for your answer, but it's not answering my question. The thing is: My question is about NotificationListenerServise, not about Notifications in general. I'm interested in not triggering the NotificationListenerService by the packages that are out of my interest programmatically.
          – Marija
          Nov 19 '18 at 11:20






          Thank you for your answer, but it's not answering my question. The thing is: My question is about NotificationListenerServise, not about Notifications in general. I'm interested in not triggering the NotificationListenerService by the packages that are out of my interest programmatically.
          – Marija
          Nov 19 '18 at 11:20




















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53373235%2freduce-unnecessary-calls-of-notificationlistenerservice%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

          MongoDB - Not Authorized To Execute Command

          How to fix TextFormField cause rebuild widget in Flutter

          Npm cannot find a required file even through it is in the searched directory