📜 ⬆️ ⬇️

How I adjusted Telegram notifications for Mi Band 2

I actively use mi band since the first version. Initially, this device was positioned as a fitness bracelet for counting steps, tracking sleep phases and a smart alarm clock, and a heart rate monitor and a clock were also added to new versions. Even from the first version, thanks to the Notify & Fitness for Mi Band application, I configured my own diode colors for each application in the smartphone and used a bracelet for notifications - if you know from which application the notification came, then you can never take the smartphone into your hands. In the second version the display already appeared and with the advent of new firmware mi band 2 learned to show the text, which made it possible to display not only the application icon, but also additional information, such as the name of the sender of the message in the messenger.

The official Mi Fit application at the moment is able to display the name of the caller, the name of the SMS sender and various icons for other applications. Somehow I was told about the presence of a third-party application Mi Band Master , in the settings of which you can choose which part of the push notification to display on the display of the bracelet.

image
')
I used this opportunity to display the name of the chat / user name, from which I received a message in the telegraph.

As a rule, push-notification in android, at least in my android 6 on redmi 4 consists of a name (name) and a message (message). The telegram sends the user / chat nickname to the notification name.

image

In general, exactly what I need, but it works as long as I have unread messages in just one chat. If someone writes in another chat, and I have not yet had time to read the messages from the previous one, then the name of the notification turns into “Telegram”.

image

Accordingly, on my mi band 2, I see “Telegram”, which causes me inconvenience. As is well known, telegrams are an open source project and the source codes of official clients for different platforms are available on github. Based on this, I figured out that to achieve the desired goal I can fork the official android client telegram and edit the name of the notification to my needs.

So, after some deliberation, I determined such a plan:

  1. fork android application, successfully assemble and install on your smartphone;
  2. find in the code the compilation of the name of the notification and make sure that the name of the last chat from which the notification came from is displayed there;
  3. configure mi band master on my fork, tell it to display the name of the notification in my mi band 2.

The plan is quite simple, so I almost immediately began to implement it.

Fork applications, build and install


The android client repository in the readme provides all the instructions for building the application and this is more than enough. I just executed “git clone”, downloaded the android studio, opened a project in it, clicked “build”, and then clicked “ok” to install all the necessary sdk versions. The main stupor was to register with services such as google, telegram, hockey and creating a keystore to sign my apk.

Speaking of registration in third-party services: in BuildVars.java, you must specify app_key from bing search api

public static String BING_SEARCH_KEY = ""; //obtain your own KEY at https://www.bing.com/dev/en-us/dev-center 

During registration in this service, at the step of receiving a code on the phone to confirm registration, I received this error at the first attempt and received it further:
Usage limit exceeded. Try again tomorrow.

In my eyes, Microsoft’s reputation is already nowhere below, but still it was offensive. I did not spend time on it, just assembled the application without this constant. It's even better that I not only do not want to use bing search, but I can’t do such a stupid thing.

The application was successfully assembled, installed on my redmi 4 and everything I needed for it worked, I did not notice the differences between the official ones.

Code editing


Here, I also did not experience any difficulties. With Shift + Shift (or Ctrl + N) I entered “notification”, I saw “NotificationsController” among the classes,

image

I opened it using file and method search (Ctrl + F12). I found the showOrUpdateNotification method, in which the name and message of the notification is formed. Total search for the right place in the code took no more than 5 minutes.

Still somewhere before 10 minutes I spent on reading the code and my first commit .

I compiled the application, with the help of two extraneous accounts, I tested the notifications in the smartphone, was glad that everything worked and set up the mi band master, but of course it didn’t do without: when sending a message from the second account, my bracelet showed the first name and vice versa message from the first account, when there were already unread messages from both.

I read the code, podebazhil a bit and figured out what the problem is. With each new message, telegrams are sent by notifications for each chat, because in the new versions of android (I have android 6), many sent notifications from the application are grouped into one, in which you can see the list:

image

In my case, the entire group of notifications sent in a short period of time is displayed as one, and the mi band master catches the first one (the previous chat in my testing case) and shows the wrong contact. I made a small crutch in the form of a second commit purely under my redmi 4, so that only one last notification was sent, which mi band master receives and sends me the correct chat name on the display of my bracelet.

Results


Now I see the name of the last sender of the message to me in the telegraph on the display of mi band 2, even when the local password is turned on in the application, as well as with a lot of unread messages. My solution works only in a bundle of two applications and probably only on android 6. All this turned out to be so simple thanks to open source and the clean code of the telegraph developers. I did not try any other forks and did not look for other solutions - it wasn’t as interesting as making one myself. A similar feature for notifications can be further developed as a client setup and promoted if not in the official application, then in some of the most successful forks or alternative clients, for example, Telegram X, but this is another story.

Source: https://habr.com/ru/post/349370/


All Articles