📜 ⬆️ ⬇️

Make a call in one click

The article describes the feature Push2Dial, which is currently not supported. Temporarily. Surely, in future versions, it will return, so that you can familiarize yourself with the device today.

One Click Call or Push2Dial (P2D) is another feature that was announced with the new 2GIS. The essence is simple - choose an organization, click on the phone number - and it is ready to dial on your smartphone.

The feature eliminates the need to dial a phone number yourself, which means the chances of being mistaken and not going there tend to zero.
')


You can call in one click from the beta version of the new online 2GIS, as well as from the good old offline. Both of them do this through an API written for this occasion, which we will discuss.

The P2D API, as you can guess from the name, works with the system of push-notifications . On mobile devices, its use is justified by the fact that if you allow applications to uncontrollably surf the Internet when they want, jokes about Android and the outlet will become a harsh reality.

By the way, depending on the platform, the work with the notification system is slightly different. On iOS, push notifications are part of the system's API. The 2GIS application interacts directly with APNS .
On Android, our application first communicates with the Google Play Services library, which, in turn, interacts with Google Cloud Messaging .

So, what happens after the user clicks on the handset icon in the mobile 2GIS? He will be offered to connect the device with 2GIS for PC or with beta.2gis.ru. Mobile 2GIS connects to the push notification system (or tries to do it), sends a Push2Dial request to the API server, containing the phone's token and information about its platform (iOS or Android).



API Push2Dial generates a pin-code for this phone and creates a bunch of “pin-> device”, which it puts in memcache for 10 minutes. In response, the API Push2Dial returns this pin to the phone.

API Push2Dial written in Lua. To work with memcache, we use the lua-resty-memcached library from a developer named Yichun Zhang. Strangely enough, lua-resty-memcached, written in Lua, was faster and more stable than the native module for nginx HttpMemcModule, written by him.

The code that works with memcache is simple and short. So, for example, Lua writes a bunch:

--  memcached memcached = require("memcached"); --  local memc, err = memcached:new(); local connected, err = memc:connect(, ); --   -    -- ttl = 10  memc:set(PIN, json.encode({['token'] = token, ['platform'] = platform}), 600); 


The user has received a pincode and enters it on the computer. From the “large” 2GIS, a request is sent to the API for the existence of the “pin-> device” bundle. If such a link is found, then API Push2Dial shifts the data about the phone from the memcache to the database and gives the product (online or offline 2GIS) an identifier from the database corresponding to the device.

You may have noticed that authorization appeared in the new 2GIS. If at this moment the user is authorized, then the API Push2Dial records its authorization id. Thus, the next time, where the user does not log in, his binding will take effect and he will be able to make calls immediately. Of course, in one click.

Actually, about the calls. When a user clicks on an organization’s 2GIS phone number on a computer, a request is generated to generate a push notification containing the device ID. The P2D API finds this ID in the database and generates a push notification in XML or JSON format, depending on the platform. Which, in turn, goes to the appropriate Google or Apple server, and from there to the user's phone.



At this stage, you can note another nuance. On iOS, dialing a number always occurs through the 2GIS application, after the user clicks the notification. On Android, our code is launched in real time (immediately upon receipt of a notification), which opens the dealer. Thus, if the 2GIS application was not launched, it will not start.

If you have not tried a call in one click, it's time to do it. At your service are 2GIS for iOS or Android , a new 2GIS and an old acquaintance, 2GIS for PC .

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


All Articles