Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated Notification.java #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions Notification.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,48 @@
NotificationCompat.Builder nbuild= new NotificationCompat.Builder(this);

/********
The previous(commented) code is outdated. Android has brought some changes for Android O version & above
and added notification channel
********/
/*NotificationCompat.Builder nbuild= new NotificationCompat.Builder(this);
nbuild.setContentTitle("Danger")
.setContentText("it will run soon")
.setSmallIcon(R.drawable.play);

NotificationManager manager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

manager.notify(1,nbuild.build());
manager.cancel(1);
manager.cancel(1);*/

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

//Add this if statement code for Android O and above
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("NOTIFICATION_CHANNEL_ID",
"NOTIFICATION_NAME",
NotificationManager.IMPORTANCE_DEFAULT);

//In NOTIFICATION_CHANNEL_DESCRIPTION, you can write about the type of notification
//or anything which describes it in few words.
channel.setDescription("NOTIFICATION_CHANNEL_DESCRIPTION");

notificationManager.createNotificationChannel(channel);
}

if (notificationManager != null) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), "YOUR_CHANNEL_ID")
//To set icon for notification
.setSmallIcon(R.drawable.ic_message)
.setContentTitle("TITLE_OF_NOTIFICATION")
.setContentText("CONTENT_TO_DISPLAY_IN_NOTIFICATION")
//To make notification cancellable
.setAutoCancel(true)
//To make notification visible in all places
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);

// To open MainActivity class when notification is clicked
builder.setContentIntent(PendingIntent.getActivity(getApplicationContext(),
0,
new Intent(getApplicationContext(), MainActivity.class));
// To launch notification
notificationManager.notify(0, builder.build());
}