How I adjusted Telegram notifications to Mi Band 2

    I have been actively using mi band since the first version. Initially, this device was positioned as a fitness bracelet for counting steps, tracking the phases of sleep and a smart alarm clock, and a heart rate monitor and a clock began to be added to new versions. Since the first version, thanks to the Notify & Fitness for Mi Band application, I configured my diode colors for each application in the smartphone and used the bracelet for notifications - if you know which application the notification came from, then you can not pick up the smartphone again. In the second version, a display device has already appeared and with the advent of new firmware mi band 2 learned to show 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.

    Official appMi Fit at the moment is able to display the name of the caller, the name of the sender of SMS and various icons for other applications. Somehow they told me 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 bracelet display.

    image

    I took this opportunity to display the name of the chat / username from which I received a message in a telegram.

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

    image

    In general, exactly what I need, but it works as long as I have unread messages in only one chat. If someone writes in another chat, but I have not yet managed 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 gives me inconvenience. As you know, telegrams are an open source project, and the source codes of official clients for various platforms are available on github. Based on this, I figured that in order to achieve the desired goal, I can fork the official android telegram client and edit the name of the notification to my needs.

    So, after some deliberation, I determined this plan:

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

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

    Fork applications, assembly and installation


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

    of registration in third-party services: in BuildVars.java, you must specify the 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, the reputation of microsoft is already nowhere lower, but it was still insulting. I did not waste time on it, I just built the application without this constant. It’s even better that I not only don’t want to use bing search, but I won’t be able to do such stupid things.

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

    Code editing


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

    image

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

    Elsewhere, up to 10 minutes I spent 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, I was glad that everything worked and set up mi band master, but it didn’t go without it: when sending a message from the second account, my bracelet showed the name of the first and vice versa, if I sent message from the first account, when there were already unread messages from both.

    I read the code, made a little money and figured out what the problem was. With each new message, it sends a telegram by notification for each chat, because in 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 mi band master catches the first of them (the previous chat in my testing case) and shows an incorrect contact. I made a small crutch in the form of a second commit purely under my redmi 4 so that only one last notification is sent, which mi band master receives and sends me the correct chat name to the display of my bracelet.

    Summary


    Now I see the name of the last sender of the message to me in the telegram on the mi band 2 display even with the local password in the application turned on, 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 the open source and clean code of the telegram developers. I did not try any other forks and did not look for other solutions - it was not so interesting how to do it 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.

    Also popular now: