src/android/GeoNotificationNotifier.java
package com.cowbell.cordova.geofence;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.util.Log;
public class GeoNotificationNotifier {
private NotificationManager notificationManager;
private Context context;
private BeepHelper beepHelper;
private Logger logger;
public GeoNotificationNotifier(NotificationManager notificationManager, Context context) {
this.notificationManager = notificationManager;
this.context = context;
this.beepHelper = new BeepHelper();
this.logger = Logger.getLogger();
}
public void notify(Notification notification) {
notification.setContext(context);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setVibrate(notification.getVibrate())
.setSmallIcon(notification.getSmallIcon())
.setLargeIcon(notification.getLargeIcon())
.setAutoCancel(true)
.setContentTitle(notification.getTitle())
.setContentText(notification.getText());
if (notification.openAppOnClick) {
String packageName = context.getPackageName();
Intent resultIntent = context.getPackageManager()
.getLaunchIntentForPackage(packageName);
if (notification.data != null) {
resultIntent.putExtra("geofence.notification.data", notification.getDataJson());
}
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
notification.id, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
}
try {
Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(context, notificationSound);
r.play();
} catch (Exception e) {
beepHelper.startTone("beep_beep_beep");
e.printStackTrace();
}
notificationManager.notify(notification.id, mBuilder.build());
logger.log(Log.DEBUG, notification.toString());
}
}