0

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 figure it out for the last two months without success. I failed to find anything helpful online and have tried everything I could possibly think of, but nothing has worked. Please help and thanks in advance.

 Intent notificationIntent = new Intent(context, bibleReader.class); Bundle bundle = new Bundle(); notificationIntent.putExtras(bundle); notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_MULTIPLE_TASK); PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); Notification.BigTextStyle bigText = new Notification.BigTextStyle(); bigText.bigText(by); bigText.setSummaryText(bv); Notification.Builder NotificationBuilder; // check Android API verson and do as needed if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { NotificationBuilder = new Notification.Builder(context, "ID_BN"); } else { NotificationBuilder = new Notification.Builder(context); } Notification.Builder mBuilder = NotificationBuilder; mBuilder.setSmallIcon(R.drawable.nicon); mBuilder.setContentTitle("A Word"); mBuilder.setContentText(bv); mBuilder.setStyle(bigText); mBuilder.setAutoCancel(true); mBuilder.setTicker("text"); mBuilder.setContentIntent(contentIntent); (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { mBuilder.setChannelId(CHANNEL_ID); } mNotificationManager.notify(1, mBuilder.build()); 

3 Answers 3

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.

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "A Word", NotificationManager.IMPORTANCE_LOW); mNotificationManager.createNotificationChannel(channel); } Notification notification = new NotificationCompat.Builder(getBaseContext(),CHANNEL_ID) .setSmallIcon(R.drawable.nicon) .setContentTitle("A Word") .setAutoCancel(true) .setContentText(bv) .setContentIntent(contentIntent) .build(); mNotificationManager.notify(0, notification); 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you all so much, now I see what I was doing wrong. For the benefit of others I will post the full working code in an answer.
1

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 :)

Comments

0

With the help of the others on this forum I was able to put together the below code. Thank you all so much!

 Intent notificationIntent = new Intent(context, bibleReader.class); Bundle bundle = new Bundle(); notificationIntent.putExtras(bundle); notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_MULTIPLE_TASK); PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { notificationChannel = new NotificationChannel(CHANNEL_ID, name, NotificationManager.IMPORTANCE_HIGH); } NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { mNotificationManager.createNotificationChannel(notificationChannel); } Notification.BigTextStyle bigText = new Notification.BigTextStyle(); bigText.bigText(textHere); bigText.setSummaryText(textHere); Notification.Builder NotificationBuilder; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { NotificationBuilder = new Notification.Builder(context, "ID"); } else { NotificationBuilder = new Notification.Builder(context); } Notification.Builder mBuilder = 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()); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.