Android NotificationListenerService onNotificationPosted fire twice












5















I listen for notifications like WhatsApp Messages.



But every time a notification comes in the NotificationListenerService fire twice.



Does anyone know this problem??



This is a snippet from the AndroidManifest.xml:



<service android:name=".NotifyService"
android:label="WhatsNotify"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService"></action>
</intent-filter>
</service>


And inside the NotificationListenerService class:



public class NotifyService extends NotificationListenerService {

@Override
public void onNotificationPosted(StatusBarNotification sbn) {
Log.i("NotifyService", "got notification");
}
}


Edit:
Properties of both StatusBarNotifications:



First notification:



0|com.whatsapp|1|xxxxxxxxxx@s.whatsapp.net|10073



Second notification:



0|com.whatsapp|1|null|10073










share|improve this question

























  • have you got this prob's solution

    – Bhanu Sharma
    Dec 1 '16 at 13:07











  • @BhanuSharma No. My solution was to create a new android project and then it worked.

    – gravmatt
    Dec 2 '16 at 11:47











  • means same code u wrote in new projects and it will automatically run properly?

    – Bhanu Sharma
    Dec 2 '16 at 12:38











  • Have u resolved this because i receive this error till date

    – Bhanu Sharma
    Dec 23 '16 at 8:07











  • @BhanuSharma I still don't know why I got two notifications. but I created a new project, copied the code from the class file and paste it into the new project. That worked for me.

    – gravmatt
    Dec 28 '16 at 16:05
















5















I listen for notifications like WhatsApp Messages.



But every time a notification comes in the NotificationListenerService fire twice.



Does anyone know this problem??



This is a snippet from the AndroidManifest.xml:



<service android:name=".NotifyService"
android:label="WhatsNotify"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService"></action>
</intent-filter>
</service>


And inside the NotificationListenerService class:



public class NotifyService extends NotificationListenerService {

@Override
public void onNotificationPosted(StatusBarNotification sbn) {
Log.i("NotifyService", "got notification");
}
}


Edit:
Properties of both StatusBarNotifications:



First notification:



0|com.whatsapp|1|xxxxxxxxxx@s.whatsapp.net|10073



Second notification:



0|com.whatsapp|1|null|10073










share|improve this question

























  • have you got this prob's solution

    – Bhanu Sharma
    Dec 1 '16 at 13:07











  • @BhanuSharma No. My solution was to create a new android project and then it worked.

    – gravmatt
    Dec 2 '16 at 11:47











  • means same code u wrote in new projects and it will automatically run properly?

    – Bhanu Sharma
    Dec 2 '16 at 12:38











  • Have u resolved this because i receive this error till date

    – Bhanu Sharma
    Dec 23 '16 at 8:07











  • @BhanuSharma I still don't know why I got two notifications. but I created a new project, copied the code from the class file and paste it into the new project. That worked for me.

    – gravmatt
    Dec 28 '16 at 16:05














5












5








5


0






I listen for notifications like WhatsApp Messages.



But every time a notification comes in the NotificationListenerService fire twice.



Does anyone know this problem??



This is a snippet from the AndroidManifest.xml:



<service android:name=".NotifyService"
android:label="WhatsNotify"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService"></action>
</intent-filter>
</service>


And inside the NotificationListenerService class:



public class NotifyService extends NotificationListenerService {

@Override
public void onNotificationPosted(StatusBarNotification sbn) {
Log.i("NotifyService", "got notification");
}
}


Edit:
Properties of both StatusBarNotifications:



First notification:



0|com.whatsapp|1|xxxxxxxxxx@s.whatsapp.net|10073



Second notification:



0|com.whatsapp|1|null|10073










share|improve this question
















I listen for notifications like WhatsApp Messages.



But every time a notification comes in the NotificationListenerService fire twice.



Does anyone know this problem??



This is a snippet from the AndroidManifest.xml:



<service android:name=".NotifyService"
android:label="WhatsNotify"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService"></action>
</intent-filter>
</service>


And inside the NotificationListenerService class:



public class NotifyService extends NotificationListenerService {

@Override
public void onNotificationPosted(StatusBarNotification sbn) {
Log.i("NotifyService", "got notification");
}
}


Edit:
Properties of both StatusBarNotifications:



First notification:



0|com.whatsapp|1|xxxxxxxxxx@s.whatsapp.net|10073



Second notification:



0|com.whatsapp|1|null|10073







android android-notifications android-notification-bar






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 29 '15 at 12:00









Nikola Despotoski

40.1k1399131




40.1k1399131










asked Oct 29 '15 at 10:46









gravmattgravmatt

306413




306413













  • have you got this prob's solution

    – Bhanu Sharma
    Dec 1 '16 at 13:07











  • @BhanuSharma No. My solution was to create a new android project and then it worked.

    – gravmatt
    Dec 2 '16 at 11:47











  • means same code u wrote in new projects and it will automatically run properly?

    – Bhanu Sharma
    Dec 2 '16 at 12:38











  • Have u resolved this because i receive this error till date

    – Bhanu Sharma
    Dec 23 '16 at 8:07











  • @BhanuSharma I still don't know why I got two notifications. but I created a new project, copied the code from the class file and paste it into the new project. That worked for me.

    – gravmatt
    Dec 28 '16 at 16:05



















  • have you got this prob's solution

    – Bhanu Sharma
    Dec 1 '16 at 13:07











  • @BhanuSharma No. My solution was to create a new android project and then it worked.

    – gravmatt
    Dec 2 '16 at 11:47











  • means same code u wrote in new projects and it will automatically run properly?

    – Bhanu Sharma
    Dec 2 '16 at 12:38











  • Have u resolved this because i receive this error till date

    – Bhanu Sharma
    Dec 23 '16 at 8:07











  • @BhanuSharma I still don't know why I got two notifications. but I created a new project, copied the code from the class file and paste it into the new project. That worked for me.

    – gravmatt
    Dec 28 '16 at 16:05

















have you got this prob's solution

– Bhanu Sharma
Dec 1 '16 at 13:07





have you got this prob's solution

– Bhanu Sharma
Dec 1 '16 at 13:07













@BhanuSharma No. My solution was to create a new android project and then it worked.

– gravmatt
Dec 2 '16 at 11:47





@BhanuSharma No. My solution was to create a new android project and then it worked.

– gravmatt
Dec 2 '16 at 11:47













means same code u wrote in new projects and it will automatically run properly?

– Bhanu Sharma
Dec 2 '16 at 12:38





means same code u wrote in new projects and it will automatically run properly?

– Bhanu Sharma
Dec 2 '16 at 12:38













Have u resolved this because i receive this error till date

– Bhanu Sharma
Dec 23 '16 at 8:07





Have u resolved this because i receive this error till date

– Bhanu Sharma
Dec 23 '16 at 8:07













@BhanuSharma I still don't know why I got two notifications. but I created a new project, copied the code from the class file and paste it into the new project. That worked for me.

– gravmatt
Dec 28 '16 at 16:05





@BhanuSharma I still don't know why I got two notifications. but I created a new project, copied the code from the class file and paste it into the new project. That worked for me.

– gravmatt
Dec 28 '16 at 16:05












3 Answers
3






active

oldest

votes


















2














I'm not sure why this happens. Maybe flags of notifications could be triggering it twice.



You can try to omit duplicate executing yourself:



public class NotifyService extends NotificationListenerService {
private String mPreviousNotificationKey;
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
if(TextUtils.isEmpty(mPreviousNotification) || !TextUtils.isEmpty(mPreviousNotification) && !sbn.getKey().equals(mPreviousNotificationKey)){
Log.i("NotifyService", "got notification");
}
}


Each StatusBarNotification has unique key which is generated:



private String key() {
return user.getIdentifier() + "|" + pkg + "|" + id + "|" + tag + "|" + uid;

}


Holding each previous key can distinguish latter notification for given package.






share|improve this answer





















  • 1





    When the notification the second time fires, all properties are the same like in the first one. Except tag. Tag is null.

    – gravmatt
    Oct 29 '15 at 11:52











  • @gravmatt First one has a tag? What about key property?

    – Nikola Despotoski
    Oct 29 '15 at 11:53






  • 1





    Yes it has a tag. first time: 0|com.whatsapp|1|xxxxxxxxxx@s.whatsapp.net|10073 second time: 0|com.whatsapp|1|null|10073

    – gravmatt
    Oct 29 '15 at 11:58











  • @gravmatt Try holding reference of the one that has tag, and execute your code when the second one arrives. I don't have any other thoughts about this.

    – Nikola Despotoski
    Oct 29 '15 at 12:04











  • Google Allo application coming twice and Tag is null both time what is the answer now ? please help

    – Bhanu Sharma
    Dec 22 '16 at 11:47





















0














Facing Same Problem For Whatsapp Notification



I just solve this by generating new key with statusBarNotification.key + statusBarNotification.title



Now store this key in my SQLiteDatabase



code written in Kotlin



 override fun onNotificationPosted(sbn: StatusBarNotification?) {
if(sbn?.tag!=null)
{
var key = sbn?.key ?: null
var id = sbn?.id
var postTime = sbn?.postTime
var packageName = sbn?.packageName ?: null
var tikerText = sbn?.notification?.tickerText ?: null

var extraBundle: Bundle? = sbn?.notification?.extras ?: null
var notificationTitle = extraBundle?.get(Notification.EXTRA_TITLE)
var text = extraBundle?.getCharSequence(Notification.EXTRA_TEXT).toString()

var modifiyedUniq = key + notificationTitle

//check key present in database or not
if (!databaseHandler.checkNotification(modifiyedUniq!!))
{
Log.e(TAG, "Notification Key :: ${key}")
Log.e(TAG, "Notification Id :: ${id}")
Log.e(TAG, "Notification postTime :: ${postTime}")
Log.e(TAG, "Notification From :: ${packageName}")
Log.e(TAG, "Notification TikerText :: ${tikerText}")
Log.e(TAG, "Notification Title :: ${notificationTitle}")
Log.e(TAG, "Notification Text :: ${text}")
//now add this record in database
databaseHandler.addNotification(notificationData)
}
}
}


this method databaseHandler.checkNotification(modifiyedUniq!!) returns true if record present with this key else it returns false



each and every time checking the key if no record present means its new notification



fun checkNotification(key: String): Boolean {
var isPresent: Boolean = false
val db = readableDatabase
val selectALLQuery = "SELECT * FROM $TABLE_NAME WHERE $KEY='${key}'"
val cursor = db.rawQuery(selectALLQuery, null)
if (cursor != null) {
if (cursor.count > 0) {
cursor.close()
db.close()
Log.e("","====================================RECORD PRESEBNT=======================")
return true
}
}
cursor.close()
db.close()
Log.e("","===*******=======********=====RECORD NOT PRESENT===*******=======********=====")
return isPresent
}


Notification 0|com.whatsapp|1|XX2X606878@s.whatsapp.net|10171



tag = 91XX06X78@s.whatsapp.net



Notification Id :: 1
Notification postTime :: 15464X794103
Notification From :: com.whatsapp
Notification TikerText :: null
Notification Title :: XXX X Bca (2 messages): ​
Notification Text :: XXXXX(last new Message)





share|improve this answer

































    -1














    Using Split you can achieve this thing.



     String separated = Your Notification key.split("\|");
    if (!separated[3].equalsIgnoreCase("null")){//Add Your Data in list or DB }





    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%2f33412124%2fandroid-notificationlistenerservice-onnotificationposted-fire-twice%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      I'm not sure why this happens. Maybe flags of notifications could be triggering it twice.



      You can try to omit duplicate executing yourself:



      public class NotifyService extends NotificationListenerService {
      private String mPreviousNotificationKey;
      @Override
      public void onNotificationPosted(StatusBarNotification sbn) {
      if(TextUtils.isEmpty(mPreviousNotification) || !TextUtils.isEmpty(mPreviousNotification) && !sbn.getKey().equals(mPreviousNotificationKey)){
      Log.i("NotifyService", "got notification");
      }
      }


      Each StatusBarNotification has unique key which is generated:



      private String key() {
      return user.getIdentifier() + "|" + pkg + "|" + id + "|" + tag + "|" + uid;

      }


      Holding each previous key can distinguish latter notification for given package.






      share|improve this answer





















      • 1





        When the notification the second time fires, all properties are the same like in the first one. Except tag. Tag is null.

        – gravmatt
        Oct 29 '15 at 11:52











      • @gravmatt First one has a tag? What about key property?

        – Nikola Despotoski
        Oct 29 '15 at 11:53






      • 1





        Yes it has a tag. first time: 0|com.whatsapp|1|xxxxxxxxxx@s.whatsapp.net|10073 second time: 0|com.whatsapp|1|null|10073

        – gravmatt
        Oct 29 '15 at 11:58











      • @gravmatt Try holding reference of the one that has tag, and execute your code when the second one arrives. I don't have any other thoughts about this.

        – Nikola Despotoski
        Oct 29 '15 at 12:04











      • Google Allo application coming twice and Tag is null both time what is the answer now ? please help

        – Bhanu Sharma
        Dec 22 '16 at 11:47


















      2














      I'm not sure why this happens. Maybe flags of notifications could be triggering it twice.



      You can try to omit duplicate executing yourself:



      public class NotifyService extends NotificationListenerService {
      private String mPreviousNotificationKey;
      @Override
      public void onNotificationPosted(StatusBarNotification sbn) {
      if(TextUtils.isEmpty(mPreviousNotification) || !TextUtils.isEmpty(mPreviousNotification) && !sbn.getKey().equals(mPreviousNotificationKey)){
      Log.i("NotifyService", "got notification");
      }
      }


      Each StatusBarNotification has unique key which is generated:



      private String key() {
      return user.getIdentifier() + "|" + pkg + "|" + id + "|" + tag + "|" + uid;

      }


      Holding each previous key can distinguish latter notification for given package.






      share|improve this answer





















      • 1





        When the notification the second time fires, all properties are the same like in the first one. Except tag. Tag is null.

        – gravmatt
        Oct 29 '15 at 11:52











      • @gravmatt First one has a tag? What about key property?

        – Nikola Despotoski
        Oct 29 '15 at 11:53






      • 1





        Yes it has a tag. first time: 0|com.whatsapp|1|xxxxxxxxxx@s.whatsapp.net|10073 second time: 0|com.whatsapp|1|null|10073

        – gravmatt
        Oct 29 '15 at 11:58











      • @gravmatt Try holding reference of the one that has tag, and execute your code when the second one arrives. I don't have any other thoughts about this.

        – Nikola Despotoski
        Oct 29 '15 at 12:04











      • Google Allo application coming twice and Tag is null both time what is the answer now ? please help

        – Bhanu Sharma
        Dec 22 '16 at 11:47
















      2












      2








      2







      I'm not sure why this happens. Maybe flags of notifications could be triggering it twice.



      You can try to omit duplicate executing yourself:



      public class NotifyService extends NotificationListenerService {
      private String mPreviousNotificationKey;
      @Override
      public void onNotificationPosted(StatusBarNotification sbn) {
      if(TextUtils.isEmpty(mPreviousNotification) || !TextUtils.isEmpty(mPreviousNotification) && !sbn.getKey().equals(mPreviousNotificationKey)){
      Log.i("NotifyService", "got notification");
      }
      }


      Each StatusBarNotification has unique key which is generated:



      private String key() {
      return user.getIdentifier() + "|" + pkg + "|" + id + "|" + tag + "|" + uid;

      }


      Holding each previous key can distinguish latter notification for given package.






      share|improve this answer















      I'm not sure why this happens. Maybe flags of notifications could be triggering it twice.



      You can try to omit duplicate executing yourself:



      public class NotifyService extends NotificationListenerService {
      private String mPreviousNotificationKey;
      @Override
      public void onNotificationPosted(StatusBarNotification sbn) {
      if(TextUtils.isEmpty(mPreviousNotification) || !TextUtils.isEmpty(mPreviousNotification) && !sbn.getKey().equals(mPreviousNotificationKey)){
      Log.i("NotifyService", "got notification");
      }
      }


      Each StatusBarNotification has unique key which is generated:



      private String key() {
      return user.getIdentifier() + "|" + pkg + "|" + id + "|" + tag + "|" + uid;

      }


      Holding each previous key can distinguish latter notification for given package.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Oct 29 '15 at 11:50

























      answered Oct 29 '15 at 11:32









      Nikola DespotoskiNikola Despotoski

      40.1k1399131




      40.1k1399131








      • 1





        When the notification the second time fires, all properties are the same like in the first one. Except tag. Tag is null.

        – gravmatt
        Oct 29 '15 at 11:52











      • @gravmatt First one has a tag? What about key property?

        – Nikola Despotoski
        Oct 29 '15 at 11:53






      • 1





        Yes it has a tag. first time: 0|com.whatsapp|1|xxxxxxxxxx@s.whatsapp.net|10073 second time: 0|com.whatsapp|1|null|10073

        – gravmatt
        Oct 29 '15 at 11:58











      • @gravmatt Try holding reference of the one that has tag, and execute your code when the second one arrives. I don't have any other thoughts about this.

        – Nikola Despotoski
        Oct 29 '15 at 12:04











      • Google Allo application coming twice and Tag is null both time what is the answer now ? please help

        – Bhanu Sharma
        Dec 22 '16 at 11:47
















      • 1





        When the notification the second time fires, all properties are the same like in the first one. Except tag. Tag is null.

        – gravmatt
        Oct 29 '15 at 11:52











      • @gravmatt First one has a tag? What about key property?

        – Nikola Despotoski
        Oct 29 '15 at 11:53






      • 1





        Yes it has a tag. first time: 0|com.whatsapp|1|xxxxxxxxxx@s.whatsapp.net|10073 second time: 0|com.whatsapp|1|null|10073

        – gravmatt
        Oct 29 '15 at 11:58











      • @gravmatt Try holding reference of the one that has tag, and execute your code when the second one arrives. I don't have any other thoughts about this.

        – Nikola Despotoski
        Oct 29 '15 at 12:04











      • Google Allo application coming twice and Tag is null both time what is the answer now ? please help

        – Bhanu Sharma
        Dec 22 '16 at 11:47










      1




      1





      When the notification the second time fires, all properties are the same like in the first one. Except tag. Tag is null.

      – gravmatt
      Oct 29 '15 at 11:52





      When the notification the second time fires, all properties are the same like in the first one. Except tag. Tag is null.

      – gravmatt
      Oct 29 '15 at 11:52













      @gravmatt First one has a tag? What about key property?

      – Nikola Despotoski
      Oct 29 '15 at 11:53





      @gravmatt First one has a tag? What about key property?

      – Nikola Despotoski
      Oct 29 '15 at 11:53




      1




      1





      Yes it has a tag. first time: 0|com.whatsapp|1|xxxxxxxxxx@s.whatsapp.net|10073 second time: 0|com.whatsapp|1|null|10073

      – gravmatt
      Oct 29 '15 at 11:58





      Yes it has a tag. first time: 0|com.whatsapp|1|xxxxxxxxxx@s.whatsapp.net|10073 second time: 0|com.whatsapp|1|null|10073

      – gravmatt
      Oct 29 '15 at 11:58













      @gravmatt Try holding reference of the one that has tag, and execute your code when the second one arrives. I don't have any other thoughts about this.

      – Nikola Despotoski
      Oct 29 '15 at 12:04





      @gravmatt Try holding reference of the one that has tag, and execute your code when the second one arrives. I don't have any other thoughts about this.

      – Nikola Despotoski
      Oct 29 '15 at 12:04













      Google Allo application coming twice and Tag is null both time what is the answer now ? please help

      – Bhanu Sharma
      Dec 22 '16 at 11:47







      Google Allo application coming twice and Tag is null both time what is the answer now ? please help

      – Bhanu Sharma
      Dec 22 '16 at 11:47















      0














      Facing Same Problem For Whatsapp Notification



      I just solve this by generating new key with statusBarNotification.key + statusBarNotification.title



      Now store this key in my SQLiteDatabase



      code written in Kotlin



       override fun onNotificationPosted(sbn: StatusBarNotification?) {
      if(sbn?.tag!=null)
      {
      var key = sbn?.key ?: null
      var id = sbn?.id
      var postTime = sbn?.postTime
      var packageName = sbn?.packageName ?: null
      var tikerText = sbn?.notification?.tickerText ?: null

      var extraBundle: Bundle? = sbn?.notification?.extras ?: null
      var notificationTitle = extraBundle?.get(Notification.EXTRA_TITLE)
      var text = extraBundle?.getCharSequence(Notification.EXTRA_TEXT).toString()

      var modifiyedUniq = key + notificationTitle

      //check key present in database or not
      if (!databaseHandler.checkNotification(modifiyedUniq!!))
      {
      Log.e(TAG, "Notification Key :: ${key}")
      Log.e(TAG, "Notification Id :: ${id}")
      Log.e(TAG, "Notification postTime :: ${postTime}")
      Log.e(TAG, "Notification From :: ${packageName}")
      Log.e(TAG, "Notification TikerText :: ${tikerText}")
      Log.e(TAG, "Notification Title :: ${notificationTitle}")
      Log.e(TAG, "Notification Text :: ${text}")
      //now add this record in database
      databaseHandler.addNotification(notificationData)
      }
      }
      }


      this method databaseHandler.checkNotification(modifiyedUniq!!) returns true if record present with this key else it returns false



      each and every time checking the key if no record present means its new notification



      fun checkNotification(key: String): Boolean {
      var isPresent: Boolean = false
      val db = readableDatabase
      val selectALLQuery = "SELECT * FROM $TABLE_NAME WHERE $KEY='${key}'"
      val cursor = db.rawQuery(selectALLQuery, null)
      if (cursor != null) {
      if (cursor.count > 0) {
      cursor.close()
      db.close()
      Log.e("","====================================RECORD PRESEBNT=======================")
      return true
      }
      }
      cursor.close()
      db.close()
      Log.e("","===*******=======********=====RECORD NOT PRESENT===*******=======********=====")
      return isPresent
      }


      Notification 0|com.whatsapp|1|XX2X606878@s.whatsapp.net|10171



      tag = 91XX06X78@s.whatsapp.net



      Notification Id :: 1
      Notification postTime :: 15464X794103
      Notification From :: com.whatsapp
      Notification TikerText :: null
      Notification Title :: XXX X Bca (2 messages): ​
      Notification Text :: XXXXX(last new Message)





      share|improve this answer






























        0














        Facing Same Problem For Whatsapp Notification



        I just solve this by generating new key with statusBarNotification.key + statusBarNotification.title



        Now store this key in my SQLiteDatabase



        code written in Kotlin



         override fun onNotificationPosted(sbn: StatusBarNotification?) {
        if(sbn?.tag!=null)
        {
        var key = sbn?.key ?: null
        var id = sbn?.id
        var postTime = sbn?.postTime
        var packageName = sbn?.packageName ?: null
        var tikerText = sbn?.notification?.tickerText ?: null

        var extraBundle: Bundle? = sbn?.notification?.extras ?: null
        var notificationTitle = extraBundle?.get(Notification.EXTRA_TITLE)
        var text = extraBundle?.getCharSequence(Notification.EXTRA_TEXT).toString()

        var modifiyedUniq = key + notificationTitle

        //check key present in database or not
        if (!databaseHandler.checkNotification(modifiyedUniq!!))
        {
        Log.e(TAG, "Notification Key :: ${key}")
        Log.e(TAG, "Notification Id :: ${id}")
        Log.e(TAG, "Notification postTime :: ${postTime}")
        Log.e(TAG, "Notification From :: ${packageName}")
        Log.e(TAG, "Notification TikerText :: ${tikerText}")
        Log.e(TAG, "Notification Title :: ${notificationTitle}")
        Log.e(TAG, "Notification Text :: ${text}")
        //now add this record in database
        databaseHandler.addNotification(notificationData)
        }
        }
        }


        this method databaseHandler.checkNotification(modifiyedUniq!!) returns true if record present with this key else it returns false



        each and every time checking the key if no record present means its new notification



        fun checkNotification(key: String): Boolean {
        var isPresent: Boolean = false
        val db = readableDatabase
        val selectALLQuery = "SELECT * FROM $TABLE_NAME WHERE $KEY='${key}'"
        val cursor = db.rawQuery(selectALLQuery, null)
        if (cursor != null) {
        if (cursor.count > 0) {
        cursor.close()
        db.close()
        Log.e("","====================================RECORD PRESEBNT=======================")
        return true
        }
        }
        cursor.close()
        db.close()
        Log.e("","===*******=======********=====RECORD NOT PRESENT===*******=======********=====")
        return isPresent
        }


        Notification 0|com.whatsapp|1|XX2X606878@s.whatsapp.net|10171



        tag = 91XX06X78@s.whatsapp.net



        Notification Id :: 1
        Notification postTime :: 15464X794103
        Notification From :: com.whatsapp
        Notification TikerText :: null
        Notification Title :: XXX X Bca (2 messages): ​
        Notification Text :: XXXXX(last new Message)





        share|improve this answer




























          0












          0








          0







          Facing Same Problem For Whatsapp Notification



          I just solve this by generating new key with statusBarNotification.key + statusBarNotification.title



          Now store this key in my SQLiteDatabase



          code written in Kotlin



           override fun onNotificationPosted(sbn: StatusBarNotification?) {
          if(sbn?.tag!=null)
          {
          var key = sbn?.key ?: null
          var id = sbn?.id
          var postTime = sbn?.postTime
          var packageName = sbn?.packageName ?: null
          var tikerText = sbn?.notification?.tickerText ?: null

          var extraBundle: Bundle? = sbn?.notification?.extras ?: null
          var notificationTitle = extraBundle?.get(Notification.EXTRA_TITLE)
          var text = extraBundle?.getCharSequence(Notification.EXTRA_TEXT).toString()

          var modifiyedUniq = key + notificationTitle

          //check key present in database or not
          if (!databaseHandler.checkNotification(modifiyedUniq!!))
          {
          Log.e(TAG, "Notification Key :: ${key}")
          Log.e(TAG, "Notification Id :: ${id}")
          Log.e(TAG, "Notification postTime :: ${postTime}")
          Log.e(TAG, "Notification From :: ${packageName}")
          Log.e(TAG, "Notification TikerText :: ${tikerText}")
          Log.e(TAG, "Notification Title :: ${notificationTitle}")
          Log.e(TAG, "Notification Text :: ${text}")
          //now add this record in database
          databaseHandler.addNotification(notificationData)
          }
          }
          }


          this method databaseHandler.checkNotification(modifiyedUniq!!) returns true if record present with this key else it returns false



          each and every time checking the key if no record present means its new notification



          fun checkNotification(key: String): Boolean {
          var isPresent: Boolean = false
          val db = readableDatabase
          val selectALLQuery = "SELECT * FROM $TABLE_NAME WHERE $KEY='${key}'"
          val cursor = db.rawQuery(selectALLQuery, null)
          if (cursor != null) {
          if (cursor.count > 0) {
          cursor.close()
          db.close()
          Log.e("","====================================RECORD PRESEBNT=======================")
          return true
          }
          }
          cursor.close()
          db.close()
          Log.e("","===*******=======********=====RECORD NOT PRESENT===*******=======********=====")
          return isPresent
          }


          Notification 0|com.whatsapp|1|XX2X606878@s.whatsapp.net|10171



          tag = 91XX06X78@s.whatsapp.net



          Notification Id :: 1
          Notification postTime :: 15464X794103
          Notification From :: com.whatsapp
          Notification TikerText :: null
          Notification Title :: XXX X Bca (2 messages): ​
          Notification Text :: XXXXX(last new Message)





          share|improve this answer















          Facing Same Problem For Whatsapp Notification



          I just solve this by generating new key with statusBarNotification.key + statusBarNotification.title



          Now store this key in my SQLiteDatabase



          code written in Kotlin



           override fun onNotificationPosted(sbn: StatusBarNotification?) {
          if(sbn?.tag!=null)
          {
          var key = sbn?.key ?: null
          var id = sbn?.id
          var postTime = sbn?.postTime
          var packageName = sbn?.packageName ?: null
          var tikerText = sbn?.notification?.tickerText ?: null

          var extraBundle: Bundle? = sbn?.notification?.extras ?: null
          var notificationTitle = extraBundle?.get(Notification.EXTRA_TITLE)
          var text = extraBundle?.getCharSequence(Notification.EXTRA_TEXT).toString()

          var modifiyedUniq = key + notificationTitle

          //check key present in database or not
          if (!databaseHandler.checkNotification(modifiyedUniq!!))
          {
          Log.e(TAG, "Notification Key :: ${key}")
          Log.e(TAG, "Notification Id :: ${id}")
          Log.e(TAG, "Notification postTime :: ${postTime}")
          Log.e(TAG, "Notification From :: ${packageName}")
          Log.e(TAG, "Notification TikerText :: ${tikerText}")
          Log.e(TAG, "Notification Title :: ${notificationTitle}")
          Log.e(TAG, "Notification Text :: ${text}")
          //now add this record in database
          databaseHandler.addNotification(notificationData)
          }
          }
          }


          this method databaseHandler.checkNotification(modifiyedUniq!!) returns true if record present with this key else it returns false



          each and every time checking the key if no record present means its new notification



          fun checkNotification(key: String): Boolean {
          var isPresent: Boolean = false
          val db = readableDatabase
          val selectALLQuery = "SELECT * FROM $TABLE_NAME WHERE $KEY='${key}'"
          val cursor = db.rawQuery(selectALLQuery, null)
          if (cursor != null) {
          if (cursor.count > 0) {
          cursor.close()
          db.close()
          Log.e("","====================================RECORD PRESEBNT=======================")
          return true
          }
          }
          cursor.close()
          db.close()
          Log.e("","===*******=======********=====RECORD NOT PRESENT===*******=======********=====")
          return isPresent
          }


          Notification 0|com.whatsapp|1|XX2X606878@s.whatsapp.net|10171



          tag = 91XX06X78@s.whatsapp.net



          Notification Id :: 1
          Notification postTime :: 15464X794103
          Notification From :: com.whatsapp
          Notification TikerText :: null
          Notification Title :: XXX X Bca (2 messages): ​
          Notification Text :: XXXXX(last new Message)






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 2 at 7:39

























          answered Jan 2 at 7:30









          Ashvin solankiAshvin solanki

          1,471628




          1,471628























              -1














              Using Split you can achieve this thing.



               String separated = Your Notification key.split("\|");
              if (!separated[3].equalsIgnoreCase("null")){//Add Your Data in list or DB }





              share|improve this answer






























                -1














                Using Split you can achieve this thing.



                 String separated = Your Notification key.split("\|");
                if (!separated[3].equalsIgnoreCase("null")){//Add Your Data in list or DB }





                share|improve this answer




























                  -1












                  -1








                  -1







                  Using Split you can achieve this thing.



                   String separated = Your Notification key.split("\|");
                  if (!separated[3].equalsIgnoreCase("null")){//Add Your Data in list or DB }





                  share|improve this answer















                  Using Split you can achieve this thing.



                   String separated = Your Notification key.split("\|");
                  if (!separated[3].equalsIgnoreCase("null")){//Add Your Data in list or DB }






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited May 16 '18 at 15:28









                  Jeremy Thille

                  13.8k42339




                  13.8k42339










                  answered May 16 '18 at 12:39









                  Hitesh ManwaniHitesh Manwani

                  223




                  223






























                      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%2f33412124%2fandroid-notificationlistenerservice-onnotificationposted-fire-twice%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

                      Monofisismo

                      Angular Downloading a file using contenturl with Basic Authentication

                      Olmecas