|
1 | 1 | package doc.saulmm.notification;
|
2 | 2 |
|
| 3 | +import android.app.NotificationManager; |
| 4 | +import android.app.PendingIntent; |
| 5 | +import android.content.Context; |
| 6 | +import android.content.Intent; |
| 7 | +import android.os.AsyncTask; |
| 8 | +import android.os.Build; |
| 9 | +import android.support.v4.app.NotificationCompat; |
| 10 | +import android.support.v4.app.TaskStackBuilder; |
| 11 | +import android.widget.Toast; |
| 12 | + |
| 13 | +import java.util.Random; |
| 14 | + |
3 | 15 | public class NotificationHandler {
|
| 16 | + // Notification handler singleton |
| 17 | + private static NotificationHandler nHandler; |
| 18 | + private static NotificationManager mNotificationManager; |
| 19 | + |
| 20 | + |
| 21 | + private NotificationHandler () {} |
| 22 | + |
| 23 | + |
| 24 | + /** |
| 25 | + * Singleton pattern implementation |
| 26 | + * @return |
| 27 | + */ |
| 28 | + public static NotificationHandler getInstance(Context context) { |
| 29 | + if(nHandler == null) { |
| 30 | + nHandler = new NotificationHandler(); |
| 31 | + mNotificationManager = |
| 32 | + (NotificationManager) context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE); |
| 33 | + } |
| 34 | + |
| 35 | + return nHandler; |
| 36 | + } |
| 37 | + |
| 38 | + |
| 39 | + /** |
| 40 | + * Shows a simple notification |
| 41 | + * @param context aplication context |
| 42 | + */ |
| 43 | + public void createSimpleNotification(Context context) { |
| 44 | + // Creates an explicit intent for an Activity |
| 45 | + Intent resultIntent = new Intent(context, TestActivity.class); |
| 46 | + |
| 47 | + // Creating a artifical activity stack for the notification activity |
| 48 | + TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); |
| 49 | + stackBuilder.addParentStack(TestActivity.class); |
| 50 | + stackBuilder.addNextIntent(resultIntent); |
| 51 | + |
| 52 | + // Pending intent to the notification manager |
| 53 | + PendingIntent resultPending = stackBuilder |
| 54 | + .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); |
| 55 | + |
| 56 | + // Building the notification |
| 57 | + NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context) |
| 58 | + .setSmallIcon(R.drawable.ic_launcher) // notification icon |
| 59 | + .setContentTitle("I'm a simple notification") // main title of the notification |
| 60 | + .setContentText("I'm the text of the simple notification") // notification text |
| 61 | + .setContentIntent(resultPending); // notification intent |
| 62 | + |
| 63 | + // mId allows you to update the notification later on. |
| 64 | + mNotificationManager.notify(10, mBuilder.build()); |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | + public void createExpandableNotification (Context context) { |
| 69 | + if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { |
| 70 | + // Building the expandable content |
| 71 | + NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(); |
| 72 | + String lorem = context.getResources().getString(R.string.long_lorem); |
| 73 | + String [] content = lorem.split("\\."); |
| 74 | + |
| 75 | + inboxStyle.setBigContentTitle("This is a big title"); |
| 76 | + for (String line : content) { |
| 77 | + inboxStyle.addLine(line); |
| 78 | + } |
| 79 | + |
| 80 | + // Building the notification |
| 81 | + NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(context) |
| 82 | + .setSmallIcon(R.drawable.ic_launcher) // notification icon |
| 83 | + .setContentTitle("Expandable notification") // title of notification |
| 84 | + .setContentText("This is an example of an expandable notification") // text inside the notification |
| 85 | + .setStyle(inboxStyle); // adds the expandable content to the notification |
| 86 | + |
| 87 | + mNotificationManager.notify(11, nBuilder.build()); |
| 88 | + |
| 89 | + } else { |
| 90 | + Toast.makeText(context, "Can't show", Toast.LENGTH_LONG).show(); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + |
| 95 | + /** |
| 96 | + * Show a determinate and undeterminate progress notification |
| 97 | + * @param context, activity context |
| 98 | + */ |
| 99 | + public void createProgressNotification (final Context context) { |
| 100 | + |
| 101 | + // used to update the progress notification |
| 102 | + final int progresID = new Random().nextInt(1000); |
| 103 | + |
| 104 | + // building the notification |
| 105 | + final NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(context) |
| 106 | + .setSmallIcon(R.drawable.refresh) |
| 107 | + .setContentTitle("Progres notification") |
| 108 | + .setContentText("Now waiting") |
| 109 | + .setTicker("Progress notification created") |
| 110 | + .setUsesChronometer(true) |
| 111 | + .setProgress(100, 0, true); |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | + AsyncTask<Integer, Integer, Integer> downloadTask = new AsyncTask<Integer, Integer, Integer>() { |
| 116 | + @Override |
| 117 | + protected void onPreExecute () { |
| 118 | + super.onPreExecute(); |
| 119 | + mNotificationManager.notify(progresID, nBuilder.build()); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + protected Integer doInBackground (Integer... params) { |
| 124 | + try { |
| 125 | + // Sleeps 2 seconds to show the undeterminated progress |
| 126 | + Thread.sleep(5000); |
| 127 | + |
| 128 | + // update the progress |
| 129 | + for (int i = 0; i < 101; i+=5) { |
| 130 | + nBuilder |
| 131 | + .setContentTitle("Progress running...") |
| 132 | + .setContentText("Now running...") |
| 133 | + .setProgress(100, i, false) |
| 134 | + .setSmallIcon(R.drawable.download) |
| 135 | + .setContentInfo(i + " %"); |
| 136 | + |
| 137 | + // use the same id for update instead created another one |
| 138 | + mNotificationManager.notify(progresID, nBuilder.build()); |
| 139 | + Thread.sleep(500); |
| 140 | + } |
| 141 | + |
| 142 | + } catch (InterruptedException e) { |
| 143 | + e.printStackTrace(); |
| 144 | + } |
| 145 | + |
| 146 | + return null; |
| 147 | + } |
| 148 | + |
| 149 | + |
| 150 | + @Override |
| 151 | + protected void onPostExecute (Integer integer) { |
| 152 | + super.onPostExecute(integer); |
| 153 | + |
| 154 | + nBuilder.setContentText("Progress finished :D") |
| 155 | + .setContentTitle("Progress finished !!") |
| 156 | + .setTicker("Progress finished !!!") |
| 157 | + .setSmallIcon(R.drawable.accept) |
| 158 | + .setUsesChronometer(false); |
| 159 | + |
| 160 | + mNotificationManager.notify(progresID, nBuilder.build()); |
| 161 | + } |
| 162 | + }; |
| 163 | + |
| 164 | + // Executes the progress task |
| 165 | + downloadTask.execute(); |
| 166 | + } |
| 167 | + |
| 168 | + |
| 169 | + public void createButtonNotification (Context context) { |
| 170 | + if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { |
| 171 | + // Prepare intent which is triggered if the notification button is pressed |
| 172 | + Intent intent = new Intent(context, TestActivity.class); |
| 173 | + PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0); |
| 174 | + |
| 175 | + // Building the notifcation |
| 176 | + NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(context) |
| 177 | + .setSmallIcon(R.drawable.ic_launcher) // notification icon |
| 178 | + .setContentTitle("Button notification") // notification title |
| 179 | + .setContentText("Expand to show the buttons...") // content text |
| 180 | + .setTicker("Showing button notification") // status bar message |
| 181 | + .addAction(R.drawable.accept, "Accept", pIntent) // accept notification button |
| 182 | + .addAction(R.drawable.cancel, "Cancel", pIntent); // cancel notification button |
| 183 | + |
| 184 | + mNotificationManager.notify(1001, nBuilder.build()); |
| 185 | + |
| 186 | + } else { |
| 187 | + Toast.makeText(context, "You need a higher version", Toast.LENGTH_LONG).show(); |
| 188 | + } |
| 189 | + } |
4 | 190 | }
|
0 commit comments