All Downloads are FREE. Search and download functionalities are using the official Maven repository.

co.easimart.EasimartNotificationManager Maven / Gradle / Ivy

package co.easimart;

import java.util.concurrent.atomic.AtomicInteger;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;

/**
 * A utility class for building and showing notifications.
 */
/** package */ class EasimartNotificationManager {
  public static final String TAG = "co.easimart.EasimartNotificationManager";
  
  public static class Singleton {
    private static final EasimartNotificationManager INSTANCE = new EasimartNotificationManager();
  }
  
  public static EasimartNotificationManager getInstance() {
    return Singleton.INSTANCE;
  }
  
  private final AtomicInteger notificationCount = new AtomicInteger(0);
  private volatile boolean shouldShowNotifications = true;
  
  public void setShouldShowNotifications(boolean show) {
    shouldShowNotifications = show;
  }
  
  public int getNotificationCount() {
    return notificationCount.get();
  }
  
  public void showNotification(Context context, Notification notification) {
    if (context != null && notification != null) {
      notificationCount.incrementAndGet();
      
      if (shouldShowNotifications) {
        // Fire off the notification
        NotificationManager nm =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        
        // Pick an id that probably won't overlap anything
        int notificationId = (int)System.currentTimeMillis();

        try {
          nm.notify(notificationId, notification);
        } catch (SecurityException e) {
          // Some phones throw an exception for unapproved vibration
          notification.defaults = Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND;
          nm.notify(notificationId, notification);
        }
      }
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy