Last active
August 29, 2015 14:13
-
-
Save rozag/5bc0a4e0388cf1da6d89 to your computer and use it in GitHub Desktop.
A simple class for Android local notifications.
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
public class Notificator { | |
public static Notification with(final Context context) { | |
return new Notification(context); | |
} | |
public static class Notification { | |
private final Context mContext; | |
private Notification(final Context context) { | |
mContext = context; | |
} | |
public void sendNotification(final String title, final String text, final int smallIconId, final int largeIconId) { | |
final Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); | |
long[] pattern = {500, 500}; | |
final NotificationCompat.Builder mBuilder = | |
new NotificationCompat.Builder(mContext) | |
.setSmallIcon(smallIconId) | |
.setLargeIcon(getLargeIconBitmap(largeIconId)) | |
.setAutoCancel(true) | |
.setSound(sound) | |
.setVibrate(pattern) | |
.setLights(Color.RED, 500, 2000) | |
.setContentTitle(title) | |
.setContentText(text); | |
final Intent resultIntent = new Intent(mContext, MainActivity.class); | |
TaskStackBuilder stackBuilder = TaskStackBuilder.create(mContext); | |
stackBuilder.addParentStack(MainActivity.class); | |
stackBuilder.addNextIntent(resultIntent); | |
PendingIntent resultPendingIntent = | |
stackBuilder.getPendingIntent( | |
0, | |
PendingIntent.FLAG_UPDATE_CURRENT | |
); | |
mBuilder.setContentIntent(resultPendingIntent); | |
NotificationManager mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); | |
mNotificationManager.notify(1, mBuilder.build()); | |
} | |
private Bitmap getLargeIconBitmap(final int largeIconId) { | |
BitmapDrawable icon = (BitmapDrawable) mContext.getResources().getDrawable(largeIconId); | |
return icon.getBitmap(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment