Android 9 Api 28 Notification not showing - java

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.

Related

Notification doesn't work - Android Studio

So I am a beginner and I was following one tutorial on how to make notification in Android Studio. This app should make a notification when you press a button, but instead of making notification it shows up a Developer Warning
Error:
No Channel found for pkg=com.example.myapplication, channelId=My notification, id=1, tag=null, opPkg=com.example.myapplication, callingUid=10153, userId=0, incomingUserId=0, notificationUid=10153, notification=Notification(channel=My notification shortcut=null contentView=null vibrate=null sound=null defaults=0x0 flags=0x10 color=0x00000000 vis=PRIVATE)
Code(Java):
public void onClick(View v) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this,"My notification");
builder.setContentTitle("My Title");
builder.setContentText("Test");
builder.setSmallIcon(R.drawable.ic_launcher_background);
builder.setAutoCancel(true);
NotificationManagerCompat managerCompat = NotificationManagerCompat.from(MainActivity.this);
managerCompat.notify(null,0, builder.build());
}
});
Do you know how to fix this?
Starting with Android 8.0 (API level 26), notifications require a notification channel.
This is a category which you create and assign to your notifications. Having multiple notification channels per app, allows users to better manage which kind of notifications they want to receive from an app.
To achieve this, you need to create a notification channel as stated in the example in the Android docs:
private void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.channel_name);
String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
Source
CHANNEL_ID can be any integer number which doesn't change during the lifetime of your app. It is OK if you make it a static final int.
Once you created the channel, you can specify the same CHANNEL_ID when creating notifications:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(textTitle)
.setContentText(textContent)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
Source
private Notification getNotification() {
Intent notificationIntent = new Intent(this, VideoExportActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
NotificationChannel notificationChannel = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationChannel = new NotificationChannel("CHANNEL_ID", "Alarm Time....", NotificationManager.IMPORTANCE_DEFAULT);
}
NotificationManager notificationManager = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
notificationManager = getSystemService(NotificationManager.class);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationManager.createNotificationChannel(notificationChannel);
}
return new NotificationCompat.Builder(this, "CHANNEL_ID")
.setContentTitle("Title")
.setContentText("Your Message")
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentIntent(pendingIntent)
.build();
}
Notification notification = getNotification();

Android notifications : why my notification so small?

I created a notification following the instructions given here (same code).
My problem is that my notification looks very small like these:
and not normal size like here:
Do you know how to have 'normal size' notifications ?
Thanks !
Here is the code:
private void showNotif() {
createNotificationChannel();
String title = "Hi guys !";
String txt = "How are you ? :)";
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, AlarmBroadcastReceiver.CHANNEL_ID)
.setSmallIcon(R.drawable.icon)
.setContentTitle(title)
.setContentText(txt)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(txt))
.setPriority(NotificationCompat.PRIORITY_HIGH);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
// notificationId is a unique int for each notification that you must define
notificationManager.notify(2, builder.build());
}
private void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "XXXXXX"; // getString(R.string.channel_name);
String description = "XXXXXXX"; // getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(AlarmBroadcastReceiver.CHANNEL_ID, name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}

how to display the notification bar?

My program did not show the notificaion bar in android compilesdk28. My code is below. How can I solve this and how do I display the notification in All mobiles?
final String url = userDetailsItem.getIp();
System.out.println("url..."+url);
if (Remaindays .equals("53"))
System.out.println("url..."+url);
{
Context context = null;
String CHANNEL_ID = createNotificationChannel(context);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this,CHANNEL_ID)
.setSmallIcon(R.drawable.lesr)
.setContentTitle("my app")
.setContentText(""+url)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(Remaindays+"Days "))
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
}
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 CHANNEL_ID = "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(CHANNEL_ID, 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 CHANNEL_ID;
} else {
// Returns null for pre-O (26) devices.
return null;
}
}
API => 28 requires Notification Channel
https://developer.android.com/reference/android/app/NotificationChannel
if you want video links
https://www.youtube.com/watch?v=tTbd1Mfi-Sk
Template:
Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent intent = new Intent(getApplicationContext(), DummychatActivty.class);
intent.putExtra("user_id", Config.to_user_id);
intent.putExtra("profile_img_Url", Config.profile_img_Url);
intent.putExtra("user_name", Config.to_user_name);
intent.putExtra("user_image_url", Config.to_user_image_url);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String CHAT_NOTIFICATION_CHANNEL_ID = "101";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
#SuppressLint("WrongConstant") NotificationChannel Chat_notificationChannel = new NotificationChannel(CHAT_NOTIFICATION_CHANNEL_ID, "Chat Notifications", NotificationManager.IMPORTANCE_MAX);
//Configure Notification Channel
// notificationChannel.setDescription("General Notifications");
Chat_notificationChannel.setLightColor(Color.RED);
Chat_notificationChannel.enableLights(true);
Chat_notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
Chat_notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(Chat_notificationChannel);
}
NotificationCompat.Builder chat_notificationBuilder = new NotificationCompat.Builder(getApplicationContext(), CHAT_NOTIFICATION_CHANNEL_ID)
.setVibrate(new long[]{0, 100})
.setPriority(Notification.PRIORITY_MAX)
.setLights(Color.RED, 3000, 3000)
.setAutoCancel(true)
.setContentTitle(Config.fcm_headline)
.setContentIntent(pendingIntent)
.setWhen(System.currentTimeMillis())
.setColor(getResources().getColor(R.color.colorPrimary))
.setSmallIcon(R.drawable.push)
.setLargeIcon(bitmap)
.setGroup("CHAT")
.setContentText(Config.content)
.setAutoCancel(true)
.setSound(defaultSound);
chat_push_count = chat_push_count + 1;
notificationManager.notify(chat_push_count, chat_notificationBuilder.build());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
chat_notificationBuilder.setChannelId("101");
}
private void sendNotification(Notification nobj) {
Intent intent = new Intent(context, NotificationActivity.class);
intent.putExtra("Notification", "click");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri =
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(context, defaultSoundUri);
r.play();
NotificationCompat.Builder notificationBuilder = new
NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(Html.fromHtml(nobj.title))
.setContentText(Html.fromHtml(nobj.desc))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setChannelId("my_channel_01")
.setContentIntent(pendingIntent);
generateNotification(notificationBuilder);
}
Please check this you can get help , its working code in my live project
public void generateNotification(NotificationCompat.Builder notificationBuilder) {
NotificationManager notificationManager =
(NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
String CHANNEL_ID = "my_channel_01";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = context.getString(R.string.app_name);
// The user-visible name of the channel.
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name,
importance);
// Create a notification and set the notification channel.
notificationManager.createNotificationChannel(mChannel);
}
PreferenceHelper preferenceHelper = PreferenceHelper.getInstance(context);
preferenceHelper.setNotificationId(preferenceHelper.getNotificationId() + 1);
notificationManager.notify(preferenceHelper.getNotificationId(),
notificationBuilder.build());
}

Notification not appearing (Notification Channel)

I want to set a reminder from my app. After setting the time, I want to be able to receive the reminder when the set time comes. I have set a notification and I'm trying to set up notification channels but I am not getting any notification.
This is my code:
protected void onHandleIntent(Intent intent) {
createNotificationChannel();
mTodoText = intent.getStringExtra(TODOTEXT);
mTodoUUID = (UUID) intent.getSerializableExtra(TODOUUID);
Intent i = new Intent(this, ReminderActivity.class);
i.putExtra(TodoNotificationService.TODOUUID, mTodoUUID);
Intent deleteIntent = new Intent(this, DeleteNotificationService.class);
deleteIntent.putExtra(TODOUUID, mTodoUUID);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, getString(R.string.default_notification_channel_id)).setContentTitle("Reminder").setContentText(mTodoText).setDeleteIntent(PendingIntent.getService(this, mTodoUUID.hashCode(), deleteIntent, PendingIntent.FLAG_UPDATE_CURRENT)).setContentIntent(PendingIntent.getActivity(this, mTodoUUID.hashCode(), i, PendingIntent.FLAG_UPDATE_CURRENT)).setPriority(NotificationCompat.PRIORITY_DEFAULT);
int mNotificationId = (int) System.currentTimeMillis();
NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(mNotificationId, builder.build());
}
private void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "Reminders Notifications";
String desc = "Stores reminders";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel notificationChannel = new NotificationChannel(getString(R.string.default_notification_channel_id), name, importance);
notificationChannel.setDescription(desc);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(notificationChannel);
}
}

o android notification sound is not playing

This is my code for making notification and notification is showing but not playing sound please help me to identify mistakes in my code and do we need any permission to play sound,vibrate for notification?
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String id = "my_channel_01";
CharSequence name = "oreiomilla";
String description ="i love me";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(id, name, importance);
mChannel.setDescription(description);
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel .setSound(alarmSound,attributes);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
mNotificationManager.createNotificationChannel(mChannel);
int notifyID = 1;
String CHANNEL_ID = "my_channel_01";
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
Notification notification = new Notification.Builder(MainActivity.this)
.setContentTitle("New Message")
.setContentText("You've received new messages. "+ct)
.setSmallIcon(R.drawable.ic_app_icon)
.setChannelId(CHANNEL_ID) .setTicker("Showing button notification") //
.addAction(android.R.drawable.ic_dialog_alert, "Visit", pIntent) // accept notification button
.addAction(android.R.drawable.ic_dialog_email, "ignore", pIntent)
.build();
mNotificationManager.notify(notifyID, notification);
To set a sound to notifications in Oreo, you must set sound on NotificationChannel and not on Notification Builder itself. You can do this as follows
Uri sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName() + "/" + R.raw.notification_mp3);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel("YOUR_CHANNEL_ID",
"YOUR CHANNEL NAME",
NotificationManager.IMPORTANCE_DEFAULT)
AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID,
context.getString(R.string.app_name),
NotificationManager.IMPORTANCE_HIGH);
// Configure the notification channel.
mChannel.setDescription(msg);
mChannel.enableLights(true);
mChannel.enableVibration(true);
mChannel.setSound(sound, attributes); // This is IMPORTANT
if (mNotificationManager != null)
mNotificationManager.createNotificationChannel(mChannel);
}
Try this it will work for you.
private void BigTextNotificationForDays(Context context, String Message, String Author) {
Intent resultIntent = new Intent(context, SplashActivity.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, (int) Calendar.getInstance().getTimeInMillis(), resultIntent, PendingIntent.FLAG_ONE_SHOT);
//To set large icon in notification
// Bitmap icon1 = BitmapFactory.decodeResource(getResources(), R.drawable.big_image);
//Assign inbox style notification
NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();
bigText.bigText(Message);
bigText.setBigContentTitle(context.getString(R.string.app_name));
bigText.setSummaryText(Author);
//build notification
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context,createNotificationChannel(context))
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(context.getString(R.string.app_name))
.setContentText(Message)
.setStyle(bigText)
.setLargeIcon(icon)
.setColor(ContextCompat.getColor(context, R.color.colorPrimary))
.setVibrate(new long[]{1000, 1000})
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setPriority(Notification.PRIORITY_HIGH)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
//.setOngoing(true);
// Gets an instance of the LocalNotificationManager service
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
//to post your notification to the notification bar
mNotificationManager.notify(3730, mBuilder.build()); // (int) System.currentTimeMillis() set instead of id. if at a time more notification require
}
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;
}
}

Categories

Resources