CHAPTER 5 ■ IOT PATTERNS: REALTIME CLIENTS
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(getApplicationContext(),
MainActivity.class);
// The stack builder object will contain an artificial back
// stack for the started Activity. This ensures that navigating
// backward from the Activity leads out of your application to the
// Home screen.
TaskStackBuilder stackBuilder =
TaskStackBuilder.create(getApplicationContext());
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(100, mBuilder.build());
}
MQTT Client
The final piece of your app is the MQTT client. It will connect to an MQTT server and
subscribe to the codifythings/intrusiondetection topic.
In order to communicate with an MQTT broker, your app requires MQTT libraries.
Therefore, download the following two libraries:
- MQTT client library: https://eclipse.org/paho/clients/java/
- Android service library: https://eclipse.org/paho/clients/
android/
Once you have downloaded both JAR files, switch the view of Android Studio from
Android to Project , as shown in Figure 5-15.