All the new players are coming to the rapidly developing PaaS market. Not so long ago, IBM joined the battle, introducing the Bluemix service to the general programmer public. Just a cloud with the ability to perform Push-to-Deploy is no longer surprising anyone, and therefore the competition has moved from the “look, we also have a cloud!” To the side “we have a user-friendly interface, out-of-the-box ready services - and this is all for free until your project has grown. ” Under the cut, I'll show you how to make a service using this modern PAAS system and just a few lines of code that will closely monitor the site of some online games and send push notifications as soon as some interesting news appears.
Our solution will consist of four parts:

Bluemix offers a convenient abstraction — tags that allow an application to subscribe and receive push notifications only for relevant events. In our example, you need to go to the “Notifications” section and add a tag called mytag.
compile 'com.google.android.gms:play-services:+' compile 'com.ibm.mobile.services:ibmbluemix:1.+' compile 'com.ibm.mobile.services:ibmpush:1.+' <?xml version="1.0" encoding="utf-8"?> <manifest> ... <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> ... <application> ... <service android:name="com.ibm.mobile.services.push.IBMPushIntentService"> </service> <receiver android:name="com.ibm.mobile.services.push.IBMPushBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" > <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> <category android:name="com.ibm.mbaas.push.android.sample" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <category android:name="com.ibm.mbaas.push.android.sample" /> </intent-filter> </receiver> </application> </manifest> import java.util.List; import com.ibm.mobile.services.core.IBMBluemix; import com.ibm.mobile.services.push.IBMPush; import bolts.Continuation; import bolts.Task; public class MainActivity extends ActionBarActivity { private IBMPush push = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); IBMBluemix.initialize(this, "ff2c1e25-cc27-435d-b93a-a6b46fbc5056", "14d8536672d53bb5eb9d1ba28e83292e6320495c", "http://grigoryvp-test.eu-gb.mybluemix.net" ); push = IBMPush.initializeService(); // . push.register("mydevice", "myuser").continueWith(new Continuation<String, Void>() { @Override public Void then(Task<String> task) throws Exception { if (task.isFaulted()) return null; // , . push.getSubscriptions().continueWith(new Continuation<List<String>,Void>() { @Override public Void then(Task<List<String>> task) throws Exception { if (task.isFaulted()) return null; // ? if (task.getResult().isEmpty()) { // . push.subscribe("mytag").continueWith(new Continuation<String, Void>() { @Override public Void then(Task<String> task) throws Exception { return null; } }); } return null; } }); return null; } }); } 
We only need to teach the Node.js application (which we already created in Bluemix in the first step) to monitor the specified site and send notifications using another library from IBM. By the way, the application can be edited online or added to the git repository, developed and debugged locally, and then deployed using git push. To work with the git repository, you need to go to the “dashboard”, select the application and select “add git” in the upper right corner of the application screen. The upper part of the interface will change: now there will be displayed the url for scheduling the git repository and the button for editing the code in a convenient online IDE:
To explore the site, we use the Request and Cheerio libraries: the first one allows you to send an HTTPS request in one line and receive the contents of a web page, and the second provides the jQuery interface to search for the necessary information in the received HTML code. If you look closely at the studied site through DevTools, we will see that all the most interesting things change according to the CSS selector..mc_2_news: first-child .mc_2_news_paragraph
var content = ""; var ibmpush = require('ibmpush'); var request = require('request'); var cheerio = require('cheerio'); function periodic() { request("https://www.pi-online.ru/", function (err, res, body) { if (!err && 200 == res.statusCode) { var $ = cheerio.load(body); var contentNew = $(".mc_2_news:first-child .mc_2_news_paragraph").text(); if (content && content !== contentNew) { var message = { "alert" : "something updated", "url": "http://www.google.com" }; req.ibmpush.sendBroadcastNotification(message, null).then( function() { logger.info("send success"); }, function() { logger.info("send failure"); } ); } content = contentNew; } setTimeout(periodic, 60 * 1000); }); } periodic(); Source: https://habr.com/ru/post/260933/
All Articles