Troubles Getting Notification Working On Android 10
My code for a notification works on Android API 25, but when I tried it on Android 10 it didn't work. I received no errors, it just simply did nothing at all. I have been trying to
Solution 1:
For Oreo and above device have you try after create notification channel like below. Please have a look and let me know anything missing.
NotificationManagermNotificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannelchannel=newNotificationChannel(CHANNEL_ID, "A Word", NotificationManager.IMPORTANCE_LOW);
mNotificationManager.createNotificationChannel(channel);
}
Notificationnotification=newNotificationCompat.Builder(getBaseContext(),CHANNEL_ID)
.setSmallIcon(R.drawable.nicon)
.setContentTitle("A Word")
.setAutoCancel(true)
.setContentText(bv)
.setContentIntent(contentIntent)
.build();
mNotificationManager.notify(0, notification);
Solution 2:
this code work fine in any version of android.
you can put this code to any brodcastresciver or some things like that (service , etc...)
Intent pIntent = new Intent(context, SecondActivity.class);
mpIntent = PendingIntent.getActivity(context, 0, pIntent, PendingIntent.FLAG_UPDATE_CURRENT);
int notificationId = intent.getIntExtra("notificationId", 0);
String message = intent.getStringExtra("todo");
Intent mainIntent = new Intent(context, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, mainIntent,PendingIntent.FLAG_ONE_SHOT );
String channelId="channel_id";
CharSequence name="channel_name";
String descruption="description";
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel channel=new NotificationChannel(channelId,name,NotificationManager.IMPORTANCE_HIGH);
channel.setDescription(descruption);
NotificationManager notificationManager=context.getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
NotificationCompat.BigTextStyle bStyle = new NotificationCompat.BigTextStyle()
.setBigContentTitle("Your Title");
Notification notification=new NotificationCompat.Builder(context,channelId)
.setSmallIcon(R.drawable.notif_icon)
.setVibrate(new long[]{100, 500, 500, 500, 500, 500})
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.logo))
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setContentTitle("Your Title")
.setContentText(message)
.setDeleteIntent(contentIntent)
.setStyle(bStyle)
.setContentIntent(mpIntent)
.setAutoCancel(true)
.build();
NotificationManagerCompat notificationManagerCompat=NotificationManagerCompat.from(context);
notificationManagerCompat.notify(notificationId,notification);
` have fun :)
Solution 3:
With the help of the others on this forum I was able to put together the below code. Thank you all so much!
IntentnotificationIntent=newIntent(context, bibleReader.class);
Bundlebundle=newBundle();
notificationIntent.putExtras(bundle);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
PendingIntentcontentIntent= PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
notificationChannel = newNotificationChannel(CHANNEL_ID, name, NotificationManager.IMPORTANCE_HIGH);
}
NotificationManagermNotificationManager= (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
mNotificationManager.createNotificationChannel(notificationChannel);
}
Notification.BigTextStylebigText=newNotification.BigTextStyle();
bigText.bigText(textHere);
bigText.setSummaryText(textHere);
Notification.Builder NotificationBuilder;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationBuilder = newNotification.Builder(context, "ID");
} else {
NotificationBuilder = newNotification.Builder(context);
}
Notification.BuildermBuilder= NotificationBuilder;
mBuilder.setSmallIcon(R.drawable.nicon);
mBuilder.setContentTitle("text");
mBuilder.setContentText(textHere);
mBuilder.setStyle(bigText);
mBuilder.setAutoCancel(false);
mBuilder.setContentIntent(contentIntent);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
mBuilder.setChannelId(CHANNEL_ID);
}
mNotificationManager.notify(1, mBuilder.build());
Post a Comment for "Troubles Getting Notification Working On Android 10"