Advertisement
urksiful

Notification With Oreo Support

Jul 18th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.90 KB | None | 0 0
  1. private void displayNotification(RemoteMessage.Notification notification) {
  2.         Random rnd = new Random();
  3.         Random rnd2 = new Random();
  4.         int r = rnd.nextInt();
  5.         int r2 = rnd2.nextInt();
  6.         int id = (r * r2)+r;
  7.         NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  8.         String NOTIFICATION_CHANNEL_ID = "102356";
  9.         Intent intent = new Intent(this, MainActivity.class); //Set the activity
  10.         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  11.         PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,PendingIntent.FLAG_ONE_SHOT);
  12.         Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  13.        
  14.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  15.             @SuppressLint("WrongConstant") NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_MAX);
  16.  
  17.            
  18.             notificationChannel.setDescription("Channel description");
  19.             notificationChannel.enableLights(true);
  20.             notificationChannel.setLightColor(Color.RED);
  21.             notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
  22.             notificationChannel.enableVibration(true);
  23.             notificationManager.createNotificationChannel(notificationChannel);
  24.  
  25.  
  26.             NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
  27.  
  28.             notificationBuilder.setAutoCancel(true)
  29.                     .setDefaults(Notification.DEFAULT_ALL)
  30.                     .setWhen(System.currentTimeMillis())
  31.                     .setSmallIcon(R.drawable.myicon)
  32.                     .setTicker("ApptojoBusiness pr the name of the app")
  33.                     .setContentIntent(pendingIntent)
  34.                     .setContentTitle(notification.getTitle())
  35.                     .setContentText(notification.getBody())
  36.                     .setContentInfo(notification.getBody());
  37.  
  38.             notificationManager.notify(id, notificationBuilder.build());
  39.         }else{
  40.            
  41.             NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
  42.                     .setSmallIcon(R.drawable.myicon)
  43.                     .setContentTitle(notification.getTitle())
  44.                     .setContentText(notification.getBody())
  45.                     .setAutoCancel(true)
  46.                     .setSound(defaultSoundUri)
  47.                     .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
  48.                     .setContentIntent(pendingIntent);
  49.             NotificationManager notificationManager2 = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  50.             notificationManager2.notify(id, notificationBuilder.build());
  51.         }
  52.  
  53.  
  54.        
  55.  
  56.        
  57.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement