Last active
January 19, 2019 20:13
-
-
Save shishirthedev/761466d3111f11fc6dcfec81f4673fa7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.shishirstudio.notification_types_android; | |
| import android.app.NotificationChannel; | |
| import android.app.NotificationManager; | |
| import android.app.PendingIntent; | |
| import android.content.Context; | |
| import android.content.Intent; | |
| import android.graphics.Bitmap; | |
| import android.graphics.BitmapFactory; | |
| import android.net.Uri; | |
| import android.os.AsyncTask; | |
| import android.os.Build; | |
| import android.provider.Settings; | |
| import android.support.v4.app.NotificationCompat; | |
| import android.support.v4.content.ContextCompat; | |
| import android.text.Html; | |
| import android.text.TextUtils; | |
| import android.util.Patterns; | |
| import java.io.IOException; | |
| import java.io.InputStream; | |
| import java.net.HttpURLConnection; | |
| import java.net.MalformedURLException; | |
| import java.net.URL; | |
| import java.util.ArrayList; | |
| import java.util.Date; | |
| import static android.content.Context.NOTIFICATION_SERVICE; | |
| public class NotificationUtils { | |
| private NotificationCompat.Builder mBuilder; | |
| private NotificationManager mNotificationManager; | |
| private String NOTIFICATION_CHANNEL_ID = ""; | |
| private String NOTIFICATION_CHANNEL_NAME = ""; | |
| private Context context; | |
| public NotificationUtils(Context context){ | |
| this.context = context; | |
| NOTIFICATION_CHANNEL_ID = context.getApplicationContext().getPackageName(); | |
| NOTIFICATION_CHANNEL_NAME = context.getApplicationContext().getPackageName(); | |
| } | |
| /*-------------------------------------------------- | |
| BEFORE STARTED | |
| -------------------------------------------------------*/ | |
| private void buildNotification(int NOTIFICATION_ID) { | |
| if (mNotificationManager == null) { | |
| mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); | |
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){ | |
| NotificationChannel mChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH); | |
| mChannel.enableLights(true); | |
| mChannel.enableVibration(true); | |
| mNotificationManager.createNotificationChannel(mChannel); | |
| } | |
| } | |
| mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); | |
| } | |
| private int getNotificationId() { | |
| return (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE); | |
| } | |
| private PendingIntent getDismissIntent(int notificationId){ | |
| Intent buttonIntent = new Intent(context, NotificationReceiver.class); | |
| buttonIntent.putExtra("notificationId", notificationId); | |
| return PendingIntent.getBroadcast(context, 0, buttonIntent, 0); | |
| } | |
| private PendingIntent getLaunchIntent(int notificationId) { | |
| Intent intent = new Intent(context, MainActivity.class); | |
| intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); | |
| intent.putExtra("notificationId", notificationId); | |
| return PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); | |
| } | |
| /*-------------------------------------------------- | |
| BIG PICTURE STYLE NOTIFICATION | |
| -------------------------------------------------------*/ | |
| private void bigPictureStyleNotification(int icon, | |
| String title, | |
| Bitmap bitmap) { | |
| int NOTIFICATION_ID = getNotificationId(); | |
| mBuilder.setSmallIcon(icon); | |
| mBuilder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.jd)); | |
| mBuilder.setContentTitle(title); | |
| mBuilder.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(bitmap)); | |
| mBuilder.setAutoCancel(true); | |
| mBuilder.setContentIntent(getLaunchIntent(NOTIFICATION_ID)); | |
| mBuilder.addAction(android.R.drawable.ic_delete, "DISMISS", getDismissIntent(NOTIFICATION_ID)); | |
| buildNotification(NOTIFICATION_ID); | |
| } | |
| /*-------------------------------------------------- | |
| BIG TEXT STYLE NOTIFICATION | |
| -------------------------------------------------------*/ | |
| private void bigTextStyleNotification(int icon, | |
| String title, | |
| String message) { | |
| int NOTIFICATION_ID = getNotificationId(); | |
| mBuilder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID); | |
| mBuilder.setSmallIcon(icon); | |
| mBuilder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.jd)); | |
| mBuilder.setContentTitle(title); | |
| mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(message)); | |
| mBuilder.setAutoCancel(true); | |
| mBuilder.setContentIntent(getLaunchIntent(NOTIFICATION_ID)); | |
| mBuilder.addAction(android.R.drawable.ic_delete, "DISMISS", getDismissIntent(NOTIFICATION_ID)); | |
| buildNotification(NOTIFICATION_ID); | |
| } | |
| /*-------------------------------------------------- | |
| INBOX STYLE NOTIFICATION | |
| -------------------------------------------------------*/ | |
| private void inboxStyleNotification(int icon, | |
| String title, | |
| String summeryText, | |
| ArrayList<String> messageList) { | |
| if(messageList == null || messageList.isEmpty()) | |
| return; | |
| int NOTIFICATION_ID = 1; | |
| NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(); | |
| inboxStyle.setSummaryText(summeryText); | |
| for (String singleMsg: messageList){ | |
| inboxStyle.addLine(singleMsg); | |
| } | |
| mBuilder.setSmallIcon(icon); | |
| mBuilder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.jd)); | |
| mBuilder.setContentTitle(title); | |
| mBuilder.setStyle(inboxStyle); | |
| mBuilder.setAutoCancel(true); | |
| mBuilder.setContentIntent(getLaunchIntent(NOTIFICATION_ID)); | |
| mBuilder.addAction(android.R.drawable.ic_delete, "DISMISS", getDismissIntent(NOTIFICATION_ID)); | |
| buildNotification(NOTIFICATION_ID); | |
| } | |
| /*-------------------------------------------------- | |
| SHOW NOTIFICATION PUBLIC | |
| -------------------------------------------------------*/ | |
| public void showNotification(final int smallIcon, | |
| final String title, | |
| final String message, | |
| String imageUrl){ | |
| if (TextUtils.isEmpty(message)) | |
| return; | |
| mBuilder = new NotificationCompat.Builder(context.getApplicationContext(), NOTIFICATION_CHANNEL_ID); | |
| if (imageUrl != null && imageUrl.length() > 4 && Patterns.WEB_URL.matcher(imageUrl).matches()) { | |
| ImageDownloader imageDownloader = new ImageDownloader(imageUrl, new ImageDownloadListener() { | |
| @Override | |
| public void onDownloadedImage(Bitmap bitmap) { | |
| if (bitmap != null) { | |
| bigPictureStyleNotification(smallIcon,title,bitmap); | |
| } else { | |
| bigTextStyleNotification(smallIcon,title,message); | |
| } | |
| } | |
| }); | |
| imageDownloader.execute(); | |
| } else { | |
| bigTextStyleNotification(smallIcon,title,message); | |
| } | |
| } | |
| static class ImageDownloader extends AsyncTask<String, Void, Bitmap> { | |
| private ImageDownloadListener downloadListener; | |
| private String imageUrl; | |
| ImageDownloader(String imageUrl, ImageDownloadListener downloadListener){ | |
| this.imageUrl = imageUrl; | |
| this.downloadListener = downloadListener; | |
| } | |
| @Override | |
| protected Bitmap doInBackground(String... params) { | |
| try { | |
| URL url = new URL(imageUrl); | |
| InputStream in; | |
| HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | |
| connection.setDoInput(true); | |
| connection.connect(); | |
| in = connection.getInputStream(); | |
| return BitmapFactory.decodeStream(in); | |
| } catch (MalformedURLException e) { | |
| e.printStackTrace(); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| return null; | |
| } | |
| @Override | |
| protected void onPostExecute(Bitmap bitmap) { | |
| downloadListener.onDownloadedImage(bitmap); | |
| } | |
| } | |
| public interface ImageDownloadListener { | |
| void onDownloadedImage(Bitmap bitmap); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment