package com.monitorabrasil.participacidadao.helper;
import android.app.ActivityManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.media.RingtoneManager;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.text.TextUtils;
import com.monitorabrasil.participacidadao.R;
import com.monitorabrasil.participacidadao.application.AppConfig;
import java.util.List;
/**
* Created by 89741803168 on 24/09/2015.
*/
public class NotificationUtils {
private String TAG = NotificationUtils.class.getSimpleName();
private Context mContext;
public NotificationUtils() {
}
public NotificationUtils(Context mContext) {
this.mContext = mContext;
}
public void showNotificationMessage(String title, String message, Intent intent) {
// Check for empty push message
if (TextUtils.isEmpty(message))
return;
if (isAppIsInBackground(mContext)) {
// notification icon
int icon = R.drawable.ic_push;
int iconeGrande = R.mipmap.ic_launcher;
int mNotificationId = AppConfig.NOTIFICATION_ID;
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
mContext,
0,
intent,
PendingIntent.FLAG_CANCEL_CURRENT
);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
mContext);
Notification notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
.setAutoCancel(true)
.setContentTitle(title)
.setStyle(inboxStyle)
.setContentIntent(resultPendingIntent)
//.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), iconeGrande))
.setContentText(message)
.build();
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(mNotificationId, notification);
} else {
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
mContext.startActivity(intent);
}
}
/**
* Method checks if the app is in background or not
*
* @param context
* @return
*/
public static boolean isAppIsInBackground(Context context) {
boolean isInBackground = true;
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {
List<ActivityManager.RunningAppProcessInfo> runningProcesses = am.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) {
if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
for (String activeProcess : processInfo.pkgList) {
if (activeProcess.equals(context.getPackageName())) {
isInBackground = false;
}
}
}
}
} else {
// List<ActivityManager.AppTask> taskInfo = am.getAppTasks();
// ActivityManager.RecentTaskInfo componentInfo = taskInfo.get(0).getTaskInfo();
// if (componentInfo.pa.equals(context.getPackageName())) {
// isInBackground = false;
// }
}
return isInBackground;
}
}