Advertisement
vitareinforce

Android Notification

Apr 10th, 2019
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.32 KB | None | 0 0
  1. public void showNotification(Context context, String title, String body, Intent intent) {
  2.         NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
  3.  
  4.         int notificationId = 1;
  5.         String channelId = "channel-01";
  6.         String channelName = "Channel Name";
  7.         int importance = NotificationManager.IMPORTANCE_HIGH;
  8.  
  9.         if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
  10.             NotificationChannel mChannel = new NotificationChannel(
  11.                     channelId, channelName, importance);
  12.             notificationManager.createNotificationChannel(mChannel);
  13.         }
  14.  
  15.         NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, channelId)
  16.                 .setSmallIcon(R.drawable.bawasluicon)
  17.                 .setContentTitle(title)
  18.                 .setContentText(body);
  19.  
  20.         TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
  21.         stackBuilder.addNextIntent(intent);
  22.         PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
  23.                 0,
  24.                 PendingIntent.FLAG_UPDATE_CURRENT
  25.         );
  26.         mBuilder.setContentIntent(resultPendingIntent);
  27.  
  28.         notificationManager.notify(notificationId, mBuilder.build());
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement