Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.compgensoft.notificationtutorial;
- import androidx.appcompat.app.AppCompatActivity;
- import androidx.core.app.NotificationCompat;
- import androidx.core.app.NotificationManagerCompat;
- import android.app.NotificationChannel;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.content.Intent;
- import android.os.Build;
- import android.os.Bundle;
- public class MainActivity extends AppCompatActivity {
- public static final String CHANNEL_ID = "channel";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- // creating the channel
- if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
- String name = CHANNEL_ID;
- String description = "This is the channel description";
- int importance = NotificationManager.IMPORTANCE_DEFAULT;
- NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
- channel.setDescription(description);
- NotificationManager notificationManager = getSystemService(NotificationManager.class);
- notificationManager.createNotificationChannel(channel);
- }
- // setting on tap notification
- Intent intent = new Intent(this, AlertDetails.class);
- intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
- PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
- // building the notification
- NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
- .setSmallIcon(R.drawable.call)
- .setContentTitle("Channel")
- .setContentText("I am a notification!")
- // to set longer style do .setStyle(new NotificationCompat.BigTextStyle().bigText("much longer text that can't fit on one line"))
- .setPriority(NotificationCompat.PRIORITY_DEFAULT)
- .setContentIntent(pendingIntent)
- .setAutoCancel(true);
- // showing the notification
- NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
- int notificationId = 1;
- notificationManagerCompat.notify(notificationId, builder.build());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement