SHARE
TWEET

Untitled

a guest Jun 26th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public static<T> void initNotifications(Class<T> activityClass,Context context){
  2.             clientIntent = new Intent(context,activityClass);
  3.             clientIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  4.             clientIntent.putExtra("someData","This is some extras");
  5.             clientContext = context;
  6.         }
  7.      
  8. private static PendingIntent createPendingIntent(Context c, int requestCode){
  9.     PendingIntent pendingIntent =
  10.  PendingIntent.getBroadcast(c,requestCode,clientIntent,PendingIntent.FLAG_UPDATE_CURRENT);
  11.             return pendingIntent;
  12. }
  13.      
  14. public class NotificationHelper {
  15.     private static final String NOTIFICATION_TAG = "NewMessage";
  16.     private static NotificationChannel notifChannel = null;
  17.     private static int requestCode = 100;
  18.     private static Intent clientIntent = null;
  19.     private static Context clientContext = null;
  20.  
  21.     public static void notify(final Context context, final String notificationTitle, final String notificationMessage) {
  22.  
  23.         if (clientIntent == null){
  24.             throw new RuntimeException("Notifications haven't been enabled. Call NotificationHelper.initNotifications to initialize them");
  25.         }
  26.  
  27.         if (context == null){
  28.             RuntimeException t = new RuntimeException("Null context when trying to post notification");
  29.             t.printStackTrace();
  30.             t.getCause();
  31.             throw t;
  32.         }
  33.  
  34.         requestCode+=1;
  35.  
  36.         PendingIntent newPendingIntent = createPendingIntent(clientContext,requestCode);
  37.         final NotificationCompat.Builder builder = new NotificationCompat.Builder(context,channelId)
  38.  
  39.                 // Set appropriate defaults for the notification light, sound,
  40.                 // and vibration.
  41.                 .setDefaults(Notification.DEFAULT_ALL)
  42.  
  43.                 // Set required fields, including the small icon, the
  44.                 // notification title, and text.
  45.                 .setSmallIcon(R.drawable.ic_stat_new_message)
  46. //                .setContentTitle(context.getResources().getString(R.string.action_reply))
  47.                 .setContentTitle(title)
  48.                 .setContentText(text)
  49.  
  50.                 // All fields below this line are optional.
  51.  
  52.                 // Use a default priority (recognized on devices running Android
  53.                 // 4.1 or later)
  54.                 .setPriority(NotificationCompat.PRIORITY_DEFAULT)
  55.  
  56.                 // Provide a large icon, shown with the notification in the
  57.                 // notification drawer on devices running Android 3.0 or later.
  58.                 .setLargeIcon(picture)
  59.  
  60.                 // Set ticker text (preview) information for this notification.
  61.                 .setTicker(ticker)
  62.  
  63.                 // Show a number. This is useful when stacking notifications of
  64.                 // a single type.
  65. //                .setNumber(number)
  66.  
  67.                 .setContentIntent(newPendingIntent)
  68.  
  69.                 // Show expanded text content on devices running Android 4.1 or
  70.  
  71.  
  72.                 // Automatically dismiss the notification when it is touched.
  73.                 .setAutoCancel(true);
  74.  
  75.         notify(clientContext, builder.build());
  76.     }
  77. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top