Advertisement
Guest User

notification

a guest
May 23rd, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.79 KB | None | 0 0
  1.         NotificationManager notifManager = null;
  2.         final int NOTIFY_ID = 0; // ID of notification
  3.         String id = "12345qwerty"; // default_channel_id
  4.         String channelTitle = "qwerty"; // Default Channel
  5.         Intent intent;
  6.         PendingIntent pendingIntent;
  7.         NotificationCompat.Builder builder;
  8.         notifManager = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
  9.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  10.             int importance = NotificationManager.IMPORTANCE_HIGH;
  11.             NotificationChannel mChannel = notifManager.getNotificationChannel(id);
  12.             if (mChannel == null) {
  13.                 mChannel = new NotificationChannel(id, channelTitle, importance);
  14.                 mChannel.enableVibration(true);
  15.                 mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
  16.                 notifManager.createNotificationChannel(mChannel);
  17.             }
  18.             builder = new NotificationCompat.Builder(this, id);
  19.             intent = new Intent(this, MainActivity.class);
  20.             intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
  21.             pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
  22.             builder.setContentTitle(body)                            // required
  23.                     .setSmallIcon(R.drawable.ic_code_black_24dp)   // required
  24.                     .setContentText(this.getString(R.string.app_name)) // required
  25.                     .setDefaults(Notification.DEFAULT_ALL)
  26.                     .setAutoCancel(true)
  27.                     .setContentIntent(pendingIntent)
  28.                     .setTicker(body)
  29.                     .setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
  30.         }
  31.         else {
  32.             builder = new NotificationCompat.Builder(this, id);
  33.             intent = new Intent(this, MainActivity.class);
  34.             intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
  35.             pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
  36.             builder.setContentTitle(body)                            // required
  37.                     .setSmallIcon(R.drawable.ic_code_black_24dp)   // required
  38.                     .setContentText(this.getString(R.string.app_name)) // required
  39.                     .setDefaults(Notification.DEFAULT_ALL)
  40.                     .setAutoCancel(true)
  41.                     .setContentIntent(pendingIntent)
  42.                     .setTicker(body)
  43.                     .setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400})
  44.                     .setPriority(Notification.PRIORITY_HIGH);
  45.         }
  46.         Notification notification = builder.build();
  47.         notifManager.notify(NOTIFY_ID, notification);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement