Notification Resender. Part 5

Notification Resender - allows Zeroner to resend notifications from all apps

Hi there!

Today I will publish the last article in the series about writing Notification Resender app for Android. Previous episodes you can find here: http://dbondarchuk.com/tag/notification-resender/

Only one thing has left for us to do – receive and process notifications. So, let’s start!

First of all, we need create a class  NotificationListener, which should extend NotificationListenerService. This class has void onNotificationPosted method that receive StatusBarNotification object. It allows us to process this notification, for example, get application name, that fired notification, push a new one etc.

First step, we should check if notification wasn’t fired by our application to not create a loop:

Here, we’re getting package name of the notification and compare to our package name.

The next step is to load all of our settings and check if anyone can process notification (by app name or body):

So workflow is simple:

  1. Check if setting has application in allowed apps
  2. If so, run a new thread for better performance
  3. Check for blacklist words, parts etc (regex or simple)
  4. Generate a new notification and fire it
  5. If we need to remove our notification, set timer for it

Let’s look closer on unknown parts.

We’re creating a notification builder, set our application icon, title and body. But what is this TickerTicker is the text which is sent to accessibility services. Why do we need this? Zeroner reads notifications not by title or body. It reads them from the accessibility property – Ticker. It imitates a work of service for partially sighted (accessibility ser vice). We will return to this later, because we need to ask user about permissions to this service, due to that fact, that only accessibility services are allowed to read notifications. So we are also accessibility service 🙂

Every notification needs an id. We will assume, that we will have max 1000 notifications at one time 🙂 So we can reset counter to 1:

And finally, we can fire our notification, by setting needed parameters: activity, which will be opened by clicking on the notification, our builder, notification id:

The last step is to check if we need remove our notification after some time:

In Java Timer accept a class, that extends TimerTask and have overrided method run. We can create a private class, that accepts notification id in ctr and will remove notification by this id:

Great! We made our notification listener and now we should register it. It could be done in manifest file by adding following code:

Also, Zeroner accepts notification from only that app, which declares itself as a SMS app. We can do this very easily by adding another empty receiver for SMS and declaring it on manifest:

An empty class SmsIntentReceiver, that extends BroadcastReceiver should be created:

Cool! We’re almost done. One thing has left – I mentioned it above – ask user about permission to accessibility service. In MainActivity.java on the end of onCreate method add following:

And here is implementation of these two methods:

Here we’re getting list of applications, that have this permission, check if our application is in the list. If not, show the alert and set on “Yes” button click redirect to system settings for enabling accessibility permissions.

That’s all! Our application is completed. Now you know more about writing the simple Android app 🙂

Thanks!

P.S. #1 All source code you can find in my GitHub – https://github.com/dbondarchuk/Notification-Resender

P.S. #2 You can also download app for your bracelet here – https://github.com/dbondarchuk/Notification-Resender/releases

Leave a Reply