Created notification android not been diplayed in the phone - java

Currently developing an android application, and my notifications are not shown in the phone.
I am connecting my phisical phone to android studio, and I see that the method that send notification is been called, but is not displaying any notifications in the phone.
private void createNotification(){
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_chat)
.setContentTitle("New match!!!")
.setContentText("You got a new match!!!")
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
mNotificationManager.notify(001,builder.build());
}
previously im initializing mNotoficationManager as follows:
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
any idea on why my phone doesnt display my notification when I get that method call? is been called from a service that I created to change events in the database.

Found this code that works from android 8 onwards.
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, "Tortuga");
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
mBuilder.setContentTitle("New match on WeRoom")
.setContentText("You got a new match, you can now chat with someone more!!!")
.setAutoCancel(false)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
String NOTIFICATION_CHANNEL_ID = "1001";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
assert mNotificationManager != null;
mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
mNotificationManager.createNotificationChannel(notificationChannel);
}
assert mNotificationManager != null;
mNotificationManager.notify(0 /* Request Code */, mBuilder.build());

Related

Heads up notification not showing in android O and higher

I literally have tried everything seriously every method and every snippet but still, I was not able to show heads-up notifications on Chinese brand devices.
so yesterday I thought why not try it again but after all again still I'm not able to show heads-up notification until I manually goto the app into the settings and give floating permission for the app.
Now most of you may say why not to navigate the user to the setting when he/she first opens up the app but nobody likes that even there are other apps (I'm not talking about white list app like WhatsApp) which have 10K downloads are able to show heads up notification
Here is my code, btw I have tried setting the sound, vibration, and light but still heads up are not showing, and yes I do uninstall my app after every build
public void showNotification(View v){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel nc = new NotificationChannel("n","pop up notification", NotificationManager.IMPORTANCE_HIGH);
nc.enableLights(true);
nc.setLightColor(Color.BLUE);
nc.enableVibration(true);
nc.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
NotificationManager nm = getSystemService(NotificationManager.class);
nm.createNotificationChannel(nc);
}
Notification.Builder notification = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
notification = new Notification.Builder(this,"n")
.setContentTitle("Pop up notification")
.setSmallIcon(R.drawable.ic_launcher_background);
}else{
notification = new Notification.Builder(this)
.setContentTitle("Pop up notification")
// .setPriority(Notification.PRIORITY_MAX)
.setSmallIcon(R.drawable.ic_launcher_background);
}
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1,notification.build());
}
I have two solutions. Try them out and see if any works.
Yours actually works Like this
public void showNotification(){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel nc = new NotificationChannel("n","pop up notification", NotificationManager.IMPORTANCE_HIGH);
nc.enableLights(true);
nc.setLightColor(Color.BLUE);
nc.enableVibration(true);
nc.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
NotificationManager nm = getSystemService(NotificationManager.class);
nm.createNotificationChannel(nc);
}
Notification.Builder notification = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
notification = new Notification.Builder(this,"n")
.setContentTitle("Pop up notification")
.setSmallIcon(R.drawable.alex);
}else{
notification = new Notification.Builder(this)
.setContentTitle("Pop up notification")
//.setPriority(Notification.PRIORITY_MAX)
.setSmallIcon(R.drawable.alex);
}
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1,notification.build());
Mine notifyUser("Order Delivery", "Your order has arrived"):
public void notifyUser(String title, String text){
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "notify_001");
Intent intent = new Intent(this, ActivityToOpenWhenNotificationIsTapped.class);
intent.setAction("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
PendingIntent activity = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
bigTextStyle.setBigContentTitle(title);
bigTextStyle.bigText(text);
bigTextStyle.setSummaryText("Big summary text. Comment if you want.");
builder.setAutoCancel(true);
builder.setContentIntent(activity);
builder.setContentTitle(title);
builder.setContentText(text);
builder.setPriority(1);
builder.setSmallIcon(R.drawable.alex);
builder.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.alex));
builder.setStyle(bigTextStyle);
builder.setAutoCancel(true);
builder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
builder.setGroup("Pixels");
Notification build = builder.build();
build.flags |= 16;
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= 26) {
String str3 = "Pixels_V1.0";
notificationManager.createNotificationChannel(new NotificationChannel(str3, "Pixels Notification", NotificationManager.IMPORTANCE_DEFAULT));
builder.setChannelId(str3);
}
int NOTIFICATION_ID = new Random(System.currentTimeMillis()).nextInt(1000);
notificationManager.notify(NOTIFICATION_ID, builder.build());
}

Notification Timing Issue

I need the code of Android notification with timing when notification was received.
I was testing notification in my app with following code
public void onPageFinished(WebView view, String url) {
Log.d("App Loaded ==", "Do");
NotificationManager notif=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Notification notify=new Notification.Builder
(getApplicationContext()).setContentTitle("testing").setContentText("This is testing").
setContentTitle("time notification").setSmallIcon(R.drawable.ic_notification).build();
notify.flags |= Notification.FLAG_AUTO_CANCEL;
notif.notify(0, notify);
}
Kindly suggest why this is not working on page finished event. No notification is being triggered.
You have to check android version from where you are testing the app.
if its Oreo+ then you have to add notification channel, otherwise you will not get notification.
and please replace the Notification class with NotificationCompat as its deprecated.
try with this code you will surely get notification.
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx, id)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(artist)
.setOngoing(true)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCustomContentView(contentView)
.setColor(ctx.getResources().getColor(R.color.colorPrimary))
.setDefaults(DEFAULT_SOUND | DEFAULT_VIBRATE);
if (notificationManager != null) {
notification =builder.build();
notificationManager.notify(1, builder.build());
}
and if you want to log time while notification coming, you can add 1 line of code inside the Onreceived : Log.d(TAG, "Time: "+System.currentTimeMillis());

Android 9 Api 28 Notification not showing

I'm trying to show notification but it's not working for both Oreo and Pie version.
It's working in kitkat version.
I tried all the possible condition i can do, i don't know what else i'm missing here
Here is my code:
String idChannel = "my_channel_01";
Intent mainIntent;
mainIntent = new Intent(ChecklistOptionActivity.this, CashCountOptionActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, mainIntent, 0);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel mChannel = null;
// The id of the channel.
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setContentTitle(context.getString(R.string.app_name))
.setSmallIcon(R.drawable.bc_icon)
.setContentIntent(pendingIntent)
.setContentText("Test");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mChannel = new NotificationChannel(idChannel, context.getString(R.string.app_name), importance);
// Configure the notification channel.
mChannel.setDescription("Test");
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
mNotificationManager.createNotificationChannel(mChannel);
} else {
builder.setContentTitle(context.getString(R.string.app_name))
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setColor(ContextCompat.getColor(context, R.color.colorBlue))
.setVibrate(new long[]{100, 250})
.setLights(Color.YELLOW, 500, 5000)
.setAutoCancel(true);
}
mNotificationManager.notify(1, builder.build());
String channel_id = createNotificationChannel(context);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, channel_id);
The below method is to generate a new channel_id.
public static String createNotificationChannel(Context context) {
// NotificationChannels are required for Notifications on O (API 26) and above.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// The id of the channel.
String channelId = "Channel_id";
// The user-visible name of the channel.
CharSequence channelName = "Application_name";
// The user-visible description of the channel.
String channelDescription = "Application_name Alert";
int channelImportance = NotificationManager.IMPORTANCE_DEFAULT;
boolean channelEnableVibrate = true;
// int channelLockscreenVisibility = Notification.;
// Initializes NotificationChannel.
NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, channelImportance);
notificationChannel.setDescription(channelDescription);
notificationChannel.enableVibration(channelEnableVibrate);
// notificationChannel.setLockscreenVisibility(channelLockscreenVisibility);
// Adds NotificationChannel to system. Attempting to create an existing notification
// channel with its original values performs no operation, so it's safe to perform the
// below sequence.
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
assert notificationManager != null;
notificationManager.createNotificationChannel(notificationChannel);
return channelId;
} else {
// Returns null for pre-O (26) devices.
return null;
}
}
Here, you will get push notification using channel_id in your device which is consist 26+ SDK version.
Because, the NotificationCompat.Builder(context) method has been deprecated so that you will have to use an updated method NotificationCompat.Builder(context, channel_id) which has two parameters one is for context, the second one is for channel_id.
In the devices which has 26+ SDK version, you can create channel_id every time.

Why it Showing deprecated API usage in Notificationcompat.Builder [duplicate]

After upgrading my project to Android O
buildToolsVersion "26.0.1"
Lint in Android Studio is showing a deprecated warning for the follow notification builder method:
new NotificationCompat.Builder(context)
The problem is: Android Developers update their Documentation describing NotificationChannel to support notifications in Android O, and provide us with a snippet, yet with the same deprecated warning:
Notification notification = new Notification.Builder(MainActivity.this)
.setContentTitle("New Message")
.setContentText("You've received new messages.")
.setSmallIcon(R.drawable.ic_notify_status)
.setChannelId(CHANNEL_ID)
.build();
Notifications Overview
My question: Is there is any other solution for building notification, and still support Android O?
A solution I found is to pass the channel ID as a parameter in Notification.Builder constructor. But this solution is not exactly reusable.
new Notification.Builder(MainActivity.this, "channel_id")
It is mentioned in the documentation that the builder method NotificationCompat.Builder(Context context) has been deprecated. And we have to use the constructor which has the channelId parameter:
NotificationCompat.Builder(Context context, String channelId)
NotificationCompat.Builder Documentation:
This constructor was deprecated in API level 26.0.0-beta1. use
NotificationCompat.Builder(Context, String) instead. All posted
Notifications must specify a NotificationChannel Id.
Notification.Builder Documentation:
This constructor was deprecated in API level 26. use
Notification.Builder(Context, String) instead. All posted
Notifications must specify a NotificationChannel Id.
If you want to reuse the builder setters, you can create the builder with the channelId, and pass that builder to a helper method and set your preferred settings in that method.
Here is working code for all android versions as of API LEVEL 26+ with backward compatibility.
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getContext(), "M_CH_ID");
notificationBuilder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_launcher)
.setTicker("Hearty365")
.setPriority(Notification.PRIORITY_MAX) // this is deprecated in API 26 but you can still use for below 26. check below update for 26 API
.setContentTitle("Default notification")
.setContentText("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
.setContentInfo("Info");
NotificationManager notificationManager = (NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, notificationBuilder.build());
UPDATE for API 26 to set Max priority
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "my_channel_id_01";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_MAX);
// Configure the notification channel.
notificationChannel.setDescription("Channel description");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
notificationBuilder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_launcher)
.setTicker("Hearty365")
// .setPriority(Notification.PRIORITY_MAX)
.setContentTitle("Default notification")
.setContentText("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
.setContentInfo("Info");
notificationManager.notify(/*notification id*/1, notificationBuilder.build());
Call the 2-arg constructor: For compatibility with Android O, call support-v4 NotificationCompat.Builder(Context context, String channelId). When running on Android N or earlier, the channelId will be ignored. When running on Android O, also create a NotificationChannel with the same channelId.
Out of date sample code: The sample code on several JavaDoc pages such as Notification.Builder calling new Notification.Builder(mContext) is out of date.
Deprecated constructors: Notification.Builder(Context context) and v4 NotificationCompat.Builder(Context context) are deprecated in favor of Notification[Compat].Builder(Context context, String channelId). (See Notification.Builder(android.content.Context) and v4 NotificationCompat.Builder(Context context).)
Deprecated class: The entire class v7 NotificationCompat.Builder is deprecated. (See v7 NotificationCompat.Builder.) Previously, v7 NotificationCompat.Builder was needed to support NotificationCompat.MediaStyle. In Android O, there's a v4 NotificationCompat.MediaStyle in the media-compat library's android.support.v4.media package. Use that one if you need MediaStyle.
API 14+: In Support Library from 26.0.0 and higher, the support-v4 and support-v7 packages both support a minimum API level of 14. The v# names are historical.
See Recent Support Library Revisions.
Instead of checking for Build.VERSION.SDK_INT >= Build.VERSION_CODES.O as many answers suggest, there is a slightly simpler way -
Add the following line to the application section of AndroidManifest.xml file as explained in the Set Up a Firebase Cloud Messaging Client App on Android doc:
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="#string/default_notification_channel_id" />
Then add a line with a channel name to the values/strings.xml file:
<string name="default_notification_channel_id">default</string>
After that you will be able to use the new version of NotificationCompat.Builder constructor with 2 parameters (since the old constructor with 1 parameter has been deprecated in Android Oreo):
private void sendNotification(String title, String body) {
Intent i = new Intent(this, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pi = PendingIntent.getActivity(this,
0 /* Request code */,
i,
PendingIntent.FLAG_ONE_SHOT);
Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this,
getString(R.string.default_notification_channel_id))
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setSound(sound)
.setContentIntent(pi);
NotificationManager manager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, builder.build());
}
Here is the sample code, which is working in Android Oreo and less than Oreo.
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel notificationChannel = new NotificationChannel("ID", "Name", importance);
notificationManager.createNotificationChannel(notificationChannel);
builder = new NotificationCompat.Builder(getApplicationContext(), notificationChannel.getId());
} else {
builder = new NotificationCompat.Builder(getApplicationContext());
}
builder = builder
.setSmallIcon(R.drawable.ic_notification_icon)
.setColor(ContextCompat.getColor(context, R.color.color))
.setContentTitle(context.getString(R.string.getTitel))
.setTicker(context.getString(R.string.text))
.setContentText(message)
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true);
notificationManager.notify(requestCode, builder.build());
Simple Sample
public void showNotification (String from, String notification, Intent intent) {
PendingIntent pendingIntent = PendingIntent.getActivity(
context,
Notification_ID,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
);
String NOTIFICATION_CHANNEL_ID = "my_channel_id_01";
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_DEFAULT);
// Configure the notification channel.
notificationChannel.setDescription("Channel description");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID);
Notification mNotification = builder
.setContentTitle(from)
.setContentText(notification)
// .setTicker("Hearty365")
// .setContentInfo("Info")
// .setPriority(Notification.PRIORITY_MAX)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
// .setDefaults(Notification.DEFAULT_ALL)
// .setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
.build();
notificationManager.notify(/*notification id*/Notification_ID, mNotification);
}
Notification notification = new Notification.Builder(MainActivity.this)
.setContentTitle("New Message")
.setContentText("You've received new messages.")
.setSmallIcon(R.drawable.ic_notify_status)
.setChannelId(CHANNEL_ID)
.build();
Right code will be :
Notification.Builder notification=new Notification.Builder(this)
with dependency 26.0.1 and new updated dependencies such as 28.0.0.
Some users use this code in the form of this :
Notification notification=new NotificationCompat.Builder(this)//this is also wrong code.
So Logic is that which Method you will declare or initilize then the same methode on Right side will be use for Allocation. if in Leftside of = you will use some method then the same method will be use in right side of = for Allocation with new.
Try this code...It will sure work
Need to declare a Notification channel with Notification_Channel_ID
Build notification with that channel ID.
For example,
...
public static final String NOTIFICATION_CHANNEL_ID = MyLocationService.class.getSimpleName();
...
...
NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,
NOTIFICATION_CHANNEL_ID+"_name",
NotificationManager.IMPORTANCE_HIGH);
NotificationManager notifManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notifManager.createNotificationChannel(channel);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setContentTitle(getString(R.string.app_name))
.setContentText(getString(R.string.notification_text))
.setOngoing(true)
.setContentIntent(broadcastIntent)
.setSmallIcon(R.drawable.ic_tracker)
.setPriority(PRIORITY_HIGH)
.setCategory(Notification.CATEGORY_SERVICE);
startForeground(1, builder.build());
...
This constructor was deprecated in API level 26.1.0.
use NotificationCompat.Builder(Context, String) instead. All posted Notifications must specify a NotificationChannel Id.
I build this code which allows you show notificaciones to android api level < 26 or api level >= 26
private void showNotifcation(String title, String body) {
//Este método muestra notificaciones compatibles con Android Api Level < 26 o >=26
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//Mostrar notificacion en Android Api level >=26
final String CHANNEL_ID = "HEADS_UP_NOTIFICATIONS";
NotificationChannel channel = new NotificationChannel(
CHANNEL_ID,
"MyNotification",
NotificationManager.IMPORTANCE_HIGH);
getSystemService(NotificationManager.class).createNotificationChannel(channel);
Notification.Builder notification = new Notification.Builder(this, CHANNEL_ID)
.setContentTitle(title)
.setContentText(body)
.setSmallIcon(R.drawable.ic_launcher_background)
.setAutoCancel(true);
NotificationManagerCompat.from(this).notify(1, notification.build());
}else{
//Mostrar notificación para Android Api Level Menor a 26
String NOTIFICATION_CHANNEL_ID = "my_channel_id_01";
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setContentTitle(title)
.setContentText(body)
.setSmallIcon(R.drawable.ic_launcher_background)
.setAutoCancel(true);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(/*notification id*/1, notificationBuilder.build());
}
}
Cheers!

How to create a notification channel group?

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String channel_Id = "Daily";
CharSequence name = "Daily";
String description = "Get Notifications about today's rupee changes";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel(channel_Id,name,importance);
channel.setDescription(description);
channel.enableLights(true);
channel.setLightColor(Color.RED);
channel.enableVibration(true);
channel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
notificationManager.createNotificationChannel(channel);
String group = "Daily_group";
CharSequence group_name = "Daily Notifications";
notificationManager.createNotificationChannelGroup(new NotificationChannelGroup(group,group_name));
channel.setGroup("Daily_group");
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this, channel_Id)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setContentTitle("My notification")
.setContentText("Hello World!").setGroup("Daily_group");
notificationManager.notify(123,mBuilder.build());
Log.d(tag,"Done");
I used this code to create a group but, sadly a group is not created can anyone help me with this problem
this is how I do and it works:
NotificationManager notificationManager =getSystemService(NotificationManager.class);
NotificationChannelGroup notificationChannelGroup=
new NotificationChannelGroup(GROUP_ID,
getApplicationContext().getString(R.string.GROUP_NAME));
notificationManager.createNotificationChannelGroup(notificationChannelGroup);
More reading on groups can be found here: channels and Notif. Groups and Group of notif.
Note that there is a difference between Notifications group (Group of Notifications, previously called Bundled notifications) and Notification Channel Group.Confusing ...

Categories

Resources