|
import android.app.PendingIntent; |
|
import android.content.Context; |
|
import android.content.Intent; |
|
import android.graphics.Bitmap; |
|
import android.media.RingtoneManager; |
|
import android.net.Uri; |
|
import android.os.AsyncTask; |
|
import android.os.Bundle; |
|
import android.support.v4.app.NotificationCompat; |
|
import android.support.v4.content.ContextCompat; |
|
import android.text.TextUtils; |
|
|
|
import com.squareup.picasso.Picasso; |
|
|
|
import java.io.IOException; |
|
import java.util.Random; |
|
|
|
public class NotificationManager { |
|
|
|
private static android.app.NotificationManager notificationManager; |
|
|
|
public static void createNotification(final Context context, Bundle bundle, NotificationListener notificationListener) { |
|
final Notification notification = Notification.fromBundle(bundle); |
|
|
|
String title = !TextUtils.isEmpty(notification.title) ? notification.title : context.getString(R.string.app_name); |
|
PendingIntent pendingIntent = PendingIntent.getActivity(context, new Random().nextInt(), getIntentByType(notification.url, notification.getType(), notificationListener, context), |
|
PendingIntent.FLAG_ONE_SHOT); |
|
|
|
NotificationCompat.Builder notificationBuilder = createSimpleNotification(context, notification.message, title); |
|
notificationBuilder.setContentIntent(pendingIntent); |
|
|
|
notificationBuilder = createActions(notificationBuilder, notification, notificationListener); |
|
if (!TextUtils.isEmpty(notification.image)) { |
|
loadImageAndCreateNotification(notification.image, notification.imageLarge, notificationBuilder, notification.notificationId, false); |
|
}else if(!TextUtils.isEmpty(notification.imageLarge)){ |
|
loadImageAndCreateNotification(null, notification.imageLarge, notificationBuilder, notification.notificationId, true); |
|
}else{ |
|
getNotificationManager(notificationBuilder.mContext).notify(notification.notificationId, notificationBuilder.build()); |
|
} |
|
|
|
} |
|
|
|
private static void loadImageAndCreateNotification(final String image, final String imageLarge, final NotificationCompat.Builder notificationBuilder, final int notificationId, final boolean isImageLarge){ |
|
new AsyncTask<Void, Void, Void>() { |
|
@Override |
|
protected Void doInBackground(Void... params) { |
|
|
|
try { |
|
Bitmap bitmap; |
|
if (isImageLarge){ |
|
bitmap = Picasso.with(notificationBuilder.mContext).load(imageLarge).resize(400, 400).get(); |
|
}else{ |
|
bitmap = Picasso.with(notificationBuilder.mContext).load(image).resize(400, 400).transform(new CircleTransform()).get(); |
|
} |
|
if (isImageLarge){ |
|
notificationBuilder.setStyle(new NotificationCompat.BigPictureStyle() |
|
.bigPicture(bitmap)); |
|
getNotificationManager(notificationBuilder.mContext).notify(notificationId, notificationBuilder.build()); |
|
}else{ |
|
notificationBuilder.setLargeIcon(bitmap); |
|
if (!TextUtils.isEmpty(imageLarge)){ |
|
loadImageAndCreateNotification(null, imageLarge, notificationBuilder, notificationId, true); |
|
}else{ |
|
getNotificationManager(notificationBuilder.mContext).notify(notificationId, notificationBuilder.build()); |
|
} |
|
} |
|
} catch (IOException e) { |
|
e.printStackTrace(); |
|
if (!TextUtils.isEmpty(imageLarge) && !isImageLarge){ |
|
loadImageAndCreateNotification(null, imageLarge, notificationBuilder, notificationId, true); |
|
}else{ |
|
getNotificationManager(notificationBuilder.mContext).notify(notificationId, notificationBuilder.build()); |
|
} |
|
} |
|
|
|
return null; |
|
} |
|
}.execute(); |
|
|
|
} |
|
|
|
private static NotificationCompat.Builder createSimpleNotification(Context context, String message, String title){ |
|
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); |
|
return new NotificationCompat.Builder(context) |
|
.setSmallIcon(R.drawable.ic_notification_ico) |
|
.setContentTitle(title) |
|
.setStyle(new NotificationCompat.BigTextStyle().bigText(message)) |
|
.setContentText(message) |
|
.setAutoCancel(true) |
|
.setColor(ContextCompat.getColor(context, R.color.colorPrimary)) |
|
.setSound(defaultSoundUri); |
|
} |
|
|
|
private static NotificationCompat.Builder createActions(NotificationCompat.Builder notificationBuilder, Notification notification, NotificationListener notificationListener){ |
|
if (notification.actions != null && !notification.actions.isEmpty()) { |
|
for (Action action : notification.actions) { |
|
PendingIntent pendingAction = PendingIntent.getActivity(notificationBuilder.mContext , 0, getIntentByType(action.url, action.getType(), notificationListener, notificationBuilder.mContext), |
|
PendingIntent.FLAG_UPDATE_CURRENT); |
|
notificationBuilder = notificationBuilder.addAction(0, action.label, pendingAction); |
|
} |
|
} |
|
return notificationBuilder; |
|
} |
|
|
|
|
|
private static android.app.NotificationManager getNotificationManager(Context context){ |
|
if (notificationManager == null){ |
|
notificationManager = (android.app.NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); |
|
} |
|
return notificationManager; |
|
} |
|
|
|
private static Intent getIntentByType(String url, Type type, NotificationListener notificationListener, Context context) { |
|
if (TextUtils.isEmpty(url)){ |
|
return new Intent(); |
|
} |
|
switch (type) { |
|
case ACTIVITY: |
|
return notificationListener.openActivity(context, url); |
|
case NAVIGATOR: |
|
return notificationListener.openNavigator(context, url); |
|
default: |
|
return new Intent(); |
|
} |
|
} |
|
} |