What code do I use to figure out if an app has a current notification? (SELF SOLVED)

I couldn't find other questions about this, so I thought I would post my solution here in hopes of helping others. If I have missed something simple that makes this easier please let me know.

I want to do something when a certain app has a notification.

Write a for loop through all the notifications. In the output use an IF statement to only print something if the name of the app for this notification matches what you are looking for. Then, if that for loop produces your value, you can act upon it. In some cases you might want to use a simple "true" output, that the for loop caught a notification from your app, other cases you may want to print the index (or i) of the notification so you can use it later to get other information about the notification.

  fl(0, ni(count)-1, "i+1",
    "if(ni(i, app)=APP NAME, true)",
=true, ALWAYS, NEVER)$

In this first example, I am looping through only dismissable notifications, and using the app name. If a notification is found with that app name, the for loop prints out true, once it is done, the outer if statement checks if the output was true. If so, I am printing out ALWAYS for a visible option. Otherwise print out NEVER.

This example does have some issues. If there is more than one notification with the same app this will not work. The reason is, the for loop will print out "truetruetrue", and thus our outer if statement will not work.

$if( tc(cut, 
  fl(0, ni(count)-1, "i+1",
    "if(ni(i, app)=APP NAME, true)",
, 4)=true, ALWAYS, NEVER)$

The addition of the tc(cut) function allows me to look at just the beginning 4 characters in the string. This then, will tell you if at least one notification exists for the app name in question.

Get Index of Specific App Notification

Finally, you may want take more info from that notification, like the image or text. In this case, you'll set up a global value to capture the index of the notification so you can use it later.


$fl(0, ni(count)-1, "i+1",
  "if(ni(i, app)=APP NAME, i)",

This will store the index number of the notification. However this has the same flaw as before. IF there is more than one notification for the same app. For this we can use this formula instead:

  fl(0, ni(count)-1, "i+1",
    "if(ni(i, app)=APP NAME, i+.)",
., 0)$

By adding another character, or split character to the output of the index we can then split that wit the outer tc(split) function. Stay away from using special characters that are used in function like [, + -] etc.. Finally, the zero (0) at the end tells us which index to grab. This will give us the first notification with the lowest index. However, by combining these function calls, you could get the number of notifications for an app, then loop through each index and do something.

Hey Luneye, good explanation for this Fl formulaπŸ‘πŸ‘Œ, I think the fl formulas are difficult for me and for many users of Klwp, and I look for every explanation that I have in the Klwp forums. Thanks for this manπŸ‘ŒπŸ‘πŸ˜€πŸ‘. If you want and have the time you can post this explanations and formulas in the G+ Kustom Official page in the codes section, there are many users that can appreciate this work.πŸ˜ƒπŸ‘


I know it might be a little bit late (3 years late lol), but I figured that the for loop cycles if u have no new notifications because of the limit that you setted, if your notification count is 0, it goes to -1 and the iterations are adding up 1 each time, so it never reaches an end, it might lag your klwp app and the wallpaper aswell (been thru that), a workaround that is create another text global variable with this formula in it:


and in your for loop the limit would be the name of that global variable, that way, if you don't have notifications, it will set the limit to 0 so it will only run only once and won't lag.

Other than that, very nice way of implementing for loops, implemented the formula in my setup and works like a charm!