Android notification not showing

I need a program that will add a notification on Android. And when someone clicks on the notification, this should lead him to the second type of activity.

I installed the code. The notification should work, but for some reason it does not work. Notification not displayed at all. I do not know what I am missing.

The code for these files:

 Notification n = new Notification.Builder(this) .setContentTitle("New mail from " + "test@gmail.com") .setContentText("Subject") .setContentIntent(pIntent).setAutoCancel(true) .setStyle(new Notification.BigTextStyle().bigText(longText)) .build(); NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); // Hide the notification after it selected notificationManager.notify(0, n); 
+100
android notifications
Apr 16 '13 at 19:43
source share
10 answers

Code will not work without an icon. So, add a setSmallIcon call to the linker chain as follows:

 .setSmallIcon(R.drawable.icon) 



Android Oreo (8.0) and higher

Android 8 introduces a new requirement for setting the channelId property using NotificationChannel .

 private NotificationManager mNotificationManager; NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext.getApplicationContext(), "notify_001"); Intent ii = new Intent(mContext.getApplicationContext(), RootActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, ii, 0); NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle(); bigText.bigText(verseurl); bigText.setBigContentTitle("Today Bible Verse"); bigText.setSummaryText("Text in detail"); mBuilder.setContentIntent(pendingIntent); mBuilder.setSmallIcon(R.mipmap.ic_launcher_round); mBuilder.setContentTitle("Your Title"); mBuilder.setContentText("Your text"); mBuilder.setPriority(Notification.PRIORITY_MAX); mBuilder.setStyle(bigText); mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); // === Removed some obsoletes if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O) { String channelId = "Your_channel_id"; NotificationChannel channel = new NotificationChannel( channelId, "Channel human readable title", Android.App.NotificationImportance.Default); mNotificationManager.createNotificationChannel(channel); mBuilder.setChannelId(channelId); } mNotificationManager.notify(0, mBuilder.build()); 
+367
May 08 '13 at 19:04
source share

In fact, the answer of Sernando Valle does not seem to be correct. Again, your question is too vague because you are not mentioning what is wrong or not working.

Looking at your code, I assume that Notification just isn't showing up.

Your notification is not displayed because you did not provide an icon. Despite the fact that the SDK documentation does not indicate that it is necessary, in fact it is, and your Notification will not be displayed without it.

addAction is available only with 4.1. Before that, you used PendingIntent to start the Activity . It seems you are specifying PendingIntent , so your problem lies elsewhere. Logically, we need to conclude that this is a missing icon.

+57
Apr 28 '13 at 9:14
source share

You have missed a small icon. I made the same mistake, and the above step resolved it.

According to official documentation: the notification object should contain the following:

  1. Small icon setSmallIcon () set

  2. The header set by setContentTitle ()

  3. Long text set by setContentText ()

  4. On Android 8.0 (API level 26) and above, the valid notification channel identifier set by setChannelId () or provided in the NotificationCompat.Builder constructor when creating the channel.

See http://developer.android.com/guide/topics/ui/notifiers/notifications.html.

+31
Oct 02 '15 at 3:14
source share

This baffled me today, but I realized that this is because in Android 9.0 (Pie), the Do Not Disturb function also hides all notifications by default, and not just turns them off, as in Android 8.1 (Oreo) before. This does not apply to notifications.

I like it when DND is turned on for my development device, so by going into the DND settings and changing the setting, just turning off notifications (but not hiding them), I fixed it.

+8
Oct. 16 '18 at 7:59
source share

Creating notification channels is required for Android versions after Android 8.1 (Oreo) to display notifications. If notifications do not appear in your Android Oreo + application, you need to call the following function when the application starts:

 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 behaviours after this NotificationManager notificationManager = getSystemService(NotificationManager.class); notificationManager.createNotificationChannel(channel); } } 
+7
Jan 10 '19 at 11:25
source share

I think you forgot

 addAction(int icon, CharSequence title, PendingIntent intent) 

See here: Add action

+1
Apr 16 '13 at 19:50
source share

For me it was a problem with deviceToken . Check if the token of the recipient and sender device is updated correctly in your database or where you contact it to send notifications.

For example, use the following command to update the device token at application startup. Therefore, it will always be updated properly.

 // Device token for push notifications FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener( new OnSuccessListener<InstanceIdResult>() { @Override public void onSuccess(InstanceIdResult instanceIdResult) { deviceToken = instanceIdResult.getToken(); // Insert device token into Firebase database fbDbRefRoot.child("user_detail_profile").child(currentUserId).child("device_token")).setValue(deviceToken) .addOnSuccessListener( new OnSuccessListener<Void>() { @Override public void onSuccess(Void aVoid) { } }); } }); 
+1
Apr 7 '19 at 4:40
source share

Not sure if it is still active, but you also need to modify the build.gradle file and add the version of the Android SDK in use,

implementation of 'com.android.support:appcompat-v7:28.0.0'

It works like a charm in my case

+1
Jun 15 '19 at 23:53 on
source share

I had the same problem with my android app. I tried notifications and found that notifications were displayed on my Android emulator, which was running Android 7.0 (Nougat), while it didn’t work on my Android 8.1 (Oreo) phone.

After reading the documentation, I found that Android has a feature called the notification channel, without which notifications will not be displayed on Oreo devices. Below is a link to the official Android documentation on the notification channels.

0
Mar 01 '19 at 14:16
source share

If you have one version> = Android O when using the notification channel, set its value to high.

 int importance = NotificationManager.IMPORTANCE_HIGH; NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance); 
0
Sep 05 '19 at 11:21
source share



All Articles