Advertisement
Guest User

Untitled

a guest
Jan 2nd, 2022
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.88 KB | None | 0 0
  1. public class Main extends AppCompatActivity {
  2.  
  3.     @Override
  4.     public void onCreate(Bundle savedInstanceState) {
  5.         super.onCreate(savedInstanceState);
  6.         setContentView(R.layout.notification);
  7.  
  8.  
  9.         Button btn = findViewById(R.id.button);
  10.         btn.setOnClickListener(v -> {
  11.             NotificationManager manager = getSystemService(NotificationManager.class);
  12.  
  13.             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  14.                 //create the NotificationChannelGroup first as the NotificationChannel requires one
  15.                 NotificationChannelGroup channelGroup = new NotificationChannelGroup("Notifica", "Notifica");
  16.                 String groupDescription = "group description";
  17.                 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
  18.                     channelGroup.setDescription(groupDescription);
  19.                 }
  20.                 manager.createNotificationChannelGroup(channelGroup);
  21.                
  22.                 NotificationChannel channel = new NotificationChannel("Notifica", "Notifica", NotificationManager.IMPORTANCE_HIGH);
  23.                 channel.setGroup(channelGroup.getId()); //This was "n" but should be "Notifica" (the ID of the group we created)
  24.                 manager.createNotificationChannel(channel);
  25.                 System.out.println(manager.getNotificationChannel("Notifica"));
  26.             }
  27.  
  28.             NotificationCompat.Builder notifica = new NotificationCompat.Builder(Main.this, "Notifica")
  29.                     .setPriority(4)
  30.                     .setSmallIcon(R.drawable.ic_launcher_background)
  31.                     .setContentTitle("Circolare")
  32.                     .setContentText("Nome Circolare")
  33.                     .setGroup("n")
  34.                     .setChannelId("Notifica");
  35.  
  36.  
  37.             System.out.println(notifica);
  38.             manager.notify(1, notifica.build());
  39.         });
  40.  
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement