Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static<T> void initNotifications(Class<T> activityClass,Context context){
- clientIntent = new Intent(context,activityClass);
- clientIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- clientIntent.putExtra("someData","This is some extras");
- clientContext = context;
- }
- private static PendingIntent createPendingIntent(Context c, int requestCode){
- PendingIntent pendingIntent =
- PendingIntent.getBroadcast(c,requestCode,clientIntent,PendingIntent.FLAG_UPDATE_CURRENT);
- return pendingIntent;
- }
- public class NotificationHelper {
- private static final String NOTIFICATION_TAG = "NewMessage";
- private static NotificationChannel notifChannel = null;
- private static int requestCode = 100;
- private static Intent clientIntent = null;
- private static Context clientContext = null;
- public static void notify(final Context context, final String notificationTitle, final String notificationMessage) {
- if (clientIntent == null){
- throw new RuntimeException("Notifications haven't been enabled. Call NotificationHelper.initNotifications to initialize them");
- }
- if (context == null){
- RuntimeException t = new RuntimeException("Null context when trying to post notification");
- t.printStackTrace();
- t.getCause();
- throw t;
- }
- requestCode+=1;
- PendingIntent newPendingIntent = createPendingIntent(clientContext,requestCode);
- final NotificationCompat.Builder builder = new NotificationCompat.Builder(context,channelId)
- // Set appropriate defaults for the notification light, sound,
- // and vibration.
- .setDefaults(Notification.DEFAULT_ALL)
- // Set required fields, including the small icon, the
- // notification title, and text.
- .setSmallIcon(R.drawable.ic_stat_new_message)
- // .setContentTitle(context.getResources().getString(R.string.action_reply))
- .setContentTitle(title)
- .setContentText(text)
- // All fields below this line are optional.
- // Use a default priority (recognized on devices running Android
- // 4.1 or later)
- .setPriority(NotificationCompat.PRIORITY_DEFAULT)
- // Provide a large icon, shown with the notification in the
- // notification drawer on devices running Android 3.0 or later.
- .setLargeIcon(picture)
- // Set ticker text (preview) information for this notification.
- .setTicker(ticker)
- // Show a number. This is useful when stacking notifications of
- // a single type.
- // .setNumber(number)
- .setContentIntent(newPendingIntent)
- // Show expanded text content on devices running Android 4.1 or
- // Automatically dismiss the notification when it is touched.
- .setAutoCancel(true);
- notify(clientContext, builder.build());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement