Implementing Action Callbacks¶
Action Callbacks¶
You would need to extend GeomobyUserService Service class in order to have acccess to the GeoMoby callbacks. Each callback passes a confirmation of the triggered event (Enter, Dwell, Exit, Crossed), a GeomobyActionData or a GeomobyActionBasic object.
Our callback methods are called each time the SDK receives a message from our server with an Observation response.
/*************************
* Android Java
*************************/
public class GeoService extends GeomobyUserService {
@Override
public void beaconScan(boolean scanning) {
GeoMobyManager.getInstance().beaconScanChanged(scanning);
}
@Override
public void geomobyActionBasic(GeomobyActionBasic geomobyActionBasic) {
Intent openIntent = new Intent(this, MainActivity.class);
NotificationManager.sendNotification(this, openIntent, geomobyActionBasic.getTitle(), geomobyActionBasic.getBody(), R.mipmap.message);
}
@Override
public void onCreate() {
super.onCreate();
GeoMobyManager.getInstance().start();
}
@Override
public void onDestroy() {
GeoMoby.stop();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
Intent restartService = new Intent(getApplicationContext(), this.getClass());
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 1, restartService, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
if (alarmManager != null) {
alarmManager.set(AlarmManager.ELAPSED_REALTIME, 5000, pendingIntent);
}
}
super.onDestroy();
}
@Override
public void geomobyActionData(GeomobyActionData geomobyActionData) {
String key = "id";
String value = geomobyActionData.getValue(key);
if (value != null) {
Intent openIntent = new Intent(this, DiscountActivity.class);
openIntent.putExtra(key, value);
String event = geomobyActionData.getValue("id");
int icon = R.mipmap.data;
String title = "Data Action Received!";
switch (event) {
case "Enter":
title = "Welcome to our venue";
icon = R.mipmap.hotel;
break;
case "Exit":
title = "Good Bye and see you again soon";
icon = R.mipmap.good_bye;
break;
case "Drink":
title = "Get one drink for only $5";
icon = R.mipmap.drink;
break;
case "Offer":
title = "Today's Special offer";
icon = R.mipmap.offer;
break;
}
NotificationManager.sendNotification(this, openIntent, title, title, icon);
}
}
@Override
public void newFenceList(@NotNull ArrayList<GeomobyFenceView> fences) {
GeoMobyManager.getInstance().fenceListChanged(fences);
}
@Override
public void newInitLocation(@NotNull Location location) {
GeoMobyManager.getInstance().initLocationChanged(location);
}
@NotNull
@Override
public Intent getNotificationIntent() {
return new Intent(this, MainActivity.class);
}
// Since Oreo 8.0 - persistent notification Icon
@Override
public Integer getNotificationSmallIconId() {
return R.mipmap.ic_launcher;
}
// Since Oreo 8.0 - persistent notification Title
@NotNull
@Override
public String getNotificationTitle() {
return "GeoMoby is running";
}
public static void setForeground(Context context) {
Intent intent = new Intent(context, GeoService.class);
intent.setAction(GeomobyUserService.ACTION_FOREGROUND);
ContextCompat.startForegroundService(context, intent);
}
public static void disableForeground(Context context) {
Intent intent = new Intent(context, GeoService.class);
intent.setAction(GeomobyUserService.ACTION_SERVICE);
ContextCompat.startForegroundService(context, intent);
}
@Override
public void newDistance(@NotNull String distance, boolean inside) {
GeoMobyManager.getInstance().distanceChanged(distance, inside);
}
}
/*************************
* Kotlin
*************************/
class GeomobyService : GeomobyUserService() {
override val notificationIntent: Intent?
get() = Intent(this, SomeActivity::class.java)
override val notificationSmallIconId: Int?
get() = R.drawable.small_icon_notification
override val notificationTitle: String?
get() = "GeoMoby is running in the background"
override fun onCreate() {
super.onCreate()
// Make sure that you called the GeoMoby Builder BEFORE calling start() method. TIPS: Use a GeomobyManager class extending GeomobyServiceCallback
GeoMoby.start()
}
override fun onDestroy() {
GeoMoby.stop()
// Keep this code for Android versions prior to Oreo
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
val restartService = Intent(applicationContext, this.javaClass)
val pendingIntent = PendingIntent.getService(applicationContext, 1, restartService, PendingIntent.FLAG_ONE_SHOT)
val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
alarmManager.set(AlarmManager.ELAPSED_REALTIME, 5000, pendingIntent)
}
super.onDestroy()
}
override fun geomobyActionBasic(geomobyActionBasic: GeomobyActionBasic) {
}
override fun geomobyActionData(geomobyActionData: GeomobyActionData) {
GeomobyNotificationManager.dataReceived(geomobyActionData)
}
override fun newDistance(distance: String, inside: Boolean) {
GeomobyManager.distanceChanged(distance, inside)
}
override fun newFenceList(fences: ArrayList<GeomobyFenceView>) {
GeomobyManager.fenceListChanged(fences)
}
override fun newInitLocation(location: Location) {
GeomobyManager.initLocationChanged(location)
}
override fun beaconScan(scanning: Boolean) {
GeomobyManager.beaconScanChanged(scanning)
}
companion object {
const val ACTION_FOREGROUND = "foreground_state"
const val ACTION_SERVICE = "service_state"
fun setForeground(context: Context) {
val intent = Intent(context, GeomobyService::class.java)
intent.action = ACTION_FOREGROUND
ContextCompat.startForegroundService(context, intent)
}
fun disableForeground(context: Context) {
val intent = Intent(context, GeomobyService::class.java)
intent.action = ACTION_SERVICE
ContextCompat.startForegroundService(context, intent)
}
}
}
// Example of GeoMobyNotificationManager class handling data received from the GeoMoby SDK
object GeomobyNotificationManager {
fun dataReceived(geomobyActionData: GeomobyActionBasic) {
// Implement your own logic here
sendNotification(context, openIntent, text
?: "Received Notification", description
?: "You have a new notification!", R.drawable.notification_icon)
}
fun dataReceived(geomobyActionData: GeomobyActionData) {}
// Example
private fun sendNotification(context: Context, intent: Intent, title: String, body: String, @DrawableRes icon: Int) {
val pendingIntent = PendingIntent.getActivity(context, Random(100).nextInt(), intent, 0)
val notificationBuilder = Notification.Builder(context)
.setSmallIcon(icon)
.setContentTitle(title)
.setContentText(body)
.setTicker(title)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
// For Android 8+
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
val notificationChannel = NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH)
.apply {
enableLights(true)
lightColor = Color.RED
enableVibration(true)
vibrationPattern = longArrayOf(500, 500, 500)
notificationBuilder.setChannelId(NOTIFICATION_CHANNEL_ID)
}
notificationBuilder.setChannelId(NOTIFICATION_CHANNEL_ID)
notificationManager.createNotificationChannel(notificationChannel)
}
notificationManager.notify(NotificationID.id, notificationBuilder.build())
}
// Use this class to generate unique notification ID
private object NotificationID {
private val c = AtomicInteger(0)
internal val id: Int
get() = c.incrementAndGet()
}