📜 ⬆️ ⬇️

VK notifer on java

Once I happened to work on a project, the customer of which maintained feedback exclusively through the social network Vkontakte. Since I am not a very active user of this social network, there was a problem in terms of the speed of receiving messages sent to me. It so happened that I pulled the lower back and spent a couple of days on the bed. So how to do lying down, and with a sore back, nothing special, then I decided to kill several problems with a swoop:

Results of the resulting java therapy under the cut.

I would like to start by saying that I have never programmed java, so I do not pretend to the genius of my application.
Pooh, settling down and pushing the exercises closer, I began my first application in this wonderful language.
The first thing I decided to do was to build a “skeleton” for my notifer. All I needed was a simple tray menu that would show pop-up messages.

//   PopupMenu popup = new PopupMenu(); //   MenuItem exitItem = new MenuItem(""); //    exitItem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { System.exit(0); } }); //    popup.add(exitItem); SystemTray systemTray = SystemTray.getSystemTray(); Image image = Toolkit.getDefaultToolkit().getImage("vk_icon.png"); TrayIcon trayIcon = new TrayIcon(image,"VKNotifer",popup); trayIcon.setImageAutoSize(true); //    systemTray.add(trayIcon); //  trayIcon.displayMessage("VKNotifer", "",TrayIcon.MessageType.INFO); 

Now the main task is to get a list of current messages. For this purpose, I used the provided api Vkontakte. The main information on api can be found here .
The first thing you need to do in order to be able to use api is to create a Vkontakte application , after which you will receive your application id needed to get the token.
The second action to access api is to get the token itself. To do this, you need to go through the following steps in your application:
  1. Authorization
  2. Confirmation of Requested Requirements
  3. Directly getting token

After that, thanks to the received token, you can execute api methods. The authorization process is described here.
For these purposes, I created the VKapi class, which will have two methods: Get token and get a list of messages.
Method one - getting a token:
 HttpClient httpClient = new DefaultHttpClient(); //    HttpPost post = new HttpPost("http://oauth.vk.com/authorize?" + "client_id="+client_id+ "&scope="+scope+ "&redirect_uri="+redirect_uri+ "&display="+display+ "&response_type="+response_type); HttpResponse response; response = httpClient.execute(post); post.abort(); //  String HeaderLocation = response.getFirstHeader("location").getValue(); URI RedirectUri = new URI(HeaderLocation); //          //ip_h  to_h String ip_h= RedirectUri.getQuery().split("&")[2].split("=")[1]; String to_h=RedirectUri.getQuery().split("&")[4].split("=")[1]; //    post = new HttpPost("https://login.vk.com/?act=login&soft=1"+ "&q=1"+ "&ip_h="+ip_h+ "&from_host=oauth.vk.com"+ "&to="+to_h+ "&expire=0"+ "&email="+email+ "&pass="+pass); response = httpClient.execute(post); post.abort(); //       HeaderLocation = response.getFirstHeader("location").getValue(); post = new HttpPost(HeaderLocation); //    response = httpClient.execute(post); post.abort(); //       HeaderLocation = response.getFirstHeader("location").getValue(); //    post = new HttpPost(HeaderLocation); response = httpClient.execute(post); post.abort(); //       HeaderLocation = response.getFirstHeader("location").getValue(); //     access_token = HeaderLocation.split("#")[1].split("&")[0].split("=")[1]; 

For getting the list of messages, api Vkontakte has the method messages.get
Method two - getting a list of messages:
 //   String url = "https://api.vk.com/method/"+ "messages.get"+ "?out=0"+ "&access_token="+access_token ; String line = ""; try { URL url2 = new URL(url); BufferedReader reader = new BufferedReader(new InputStreamReader(url2.openStream())); line = reader.readLine(); reader.close(); } catch (MalformedURLException e) { // ... } catch (IOException e) { // ... } return line; 

Class fully:
 public class VKapi { private String client_id = "2971510"; private String scope = "messages"; private String redirect_uri = "http://oauth.vk.com/blank.html"; private String display = "popup"; private String response_type = "token"; private String access_token; private String email = "******";//    email private String pass = "******";//     public void setConnection() throws IOException, URISyntaxException { //    token'a } public String getNewMessage() throws ClientProtocolException, IOException, NoSuchAlgorithmException, URISyntaxException { //      } } 

All that is left is to create an instance of the class, get a token and in the infinite loop get lists of messages. If the newly received list is different from the previous one, then we display a notification about the new message.
The result is:
 public static void main(String[] args) throws IOException, URISyntaxException, AWTException, InterruptedException, NoSuchAlgorithmException { //   PopupMenu popup = new PopupMenu(); //   MenuItem exitItem = new MenuItem(""); //    exitItem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { System.exit(0); } }); //    popup.add(exitItem); SystemTray systemTray = SystemTray.getSystemTray(); //  Image image = Toolkit.getDefaultToolkit().getImage("vk_icon.png"); TrayIcon trayIcon = new TrayIcon(image,"VKNotifer",popup); trayIcon.setImageAutoSize(true); //    systemTray.add(trayIcon); trayIcon.displayMessage("VKNotifer", "  ",TrayIcon.MessageType.INFO); //    VKapi vkAPI = new VKapi(); //  vkAPI.setConnection(); trayIcon.displayMessage("VKNotifer", " ",TrayIcon.MessageType.INFO); //  String oldMessage = vkAPI.getNewMessage(); String newMessage; int i = 0; for (;;){ //        3  Thread.sleep(3000); //    if (i == 15000){ //   45 000  (   ,     ) vkAPI.setConnection(); //   Thread.sleep(3000); //        i = 0; } //  newMessage = vkAPI.getNewMessage(); if (!newMessage.equals(oldMessage)) { oldMessage = newMessage; trayIcon.displayMessage("VKNotifer", "  ",TrayIcon.MessageType.INFO); Tools.playDrum(Drum.d53_Ride_Bell, 127,0); } i++; } } 

In addition to the pop-up window, an audible alert is also used.
 Tools.playDrum(Drum.d53_Ride_Bell, 127,0); 

To do this, use the library from this topic.

I know that this is not an ideal performance, but the most important thing is that it completely solved my problems. Thanks for attention.

')

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


All Articles