Android notification is not showing
The code won't work without an icon. So, add the setSmallIcon
call to the builder chain like this for it to work:
.setSmallIcon(R.drawable.icon)
Android Oreo (8.0) and above
Android 8 introduced a new requirement of setting the channelId
property by using a NotificationChannel
.
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's 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.SDK_INT >= Build.VERSION_CODES.O)
{
String channelId = "Your_channel_id";
NotificationChannel channel = new NotificationChannel(
channelId,
"Channel human readable title",
NotificationManager.IMPORTANCE_HIGH);
mNotificationManager.createNotificationChannel(channel);
mBuilder.setChannelId(channelId);
}
mNotificationManager.notify(0, mBuilder.build());
Notification Not Showing In Android Studio
According to your code, you are not creating a Notification channel.
Notification channel is necessary from Android Oreo and above
So if you are running the app Android O and above devices without a notification channel, your notification won't show up.
Create Notification Channel
fun createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channelId = "all_notifications" // You should create a String resource for this instead of storing in a variable
val mChannel = NotificationChannel(
channelId,
"General Notifications",
NotificationManager.IMPORTANCE_DEFAULT
)
mChannel.description = "This is default channel used for all other notifications"
val notificationManager =
mContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(mChannel)
}
}
Then create a notification using the same channelId while creating a notification.
createNotificationChannel()
val channelId = "all_notifications" // Use same Channel ID
val intent = Intent(context, Profile::class.java)
val pendingIntent = PendingIntent.getActivity(this.context, 0, intent, 0);
val builder = NotificationCompat.Builder(this.context, channelId) // Create notification with channel Id
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setPriority(NotificationCompat.PRIORITY_MAX)
builder.setContentIntent(pendingIntent).setAutoCancel(true)
val mNotificationManager =
message.context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
with(mNotificationManager) {
notify(123, builder.build())
Hope it helps.
Android Notifications not showing up in one device
How many times do you call CreateMessage method?
This code is suppose to be called only once for your application:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
String channelId1 = "Finderr_channel_id1";
NotificationChannel channel = new NotificationChannel(
channelId1,
"Finderr Message",
NotificationManager.IMPORTANCE_DEFAULT);
mNotificationManager1.createNotificationChannel(channel);
mBuilder.setChannelId(channelId1);
}
Also I can say that i had some problems with Samsung devices when my app tried to start foreground service with permanent notification. See dontkillmyapp.com for details
android notification not showing from service
Yes you can create notification from service.but as per your code the close bracket of startNotificationListener()
is missing after below code
new Thread(new Runnable() {
@Override
public void run() {
//fetching notifications from server
//if there is notifications then call this method
ShowNotification();
}
}).start();
and you have to register service in AndroidManifest.Xml
file before </application>
.
<service android:name=".NotificationService"/>
after that, you have to start service from your activity as per below.
startService(new Intent(this,NotificationService.class));
there is the code of your service:
public class NotificationService extends Service {
public void startNotificationListener() {
//start's a new thread
new Thread(new Runnable() {
@Override
public void run() {
//fetching notifications from server
//if there is notifications then call this method
ShowNotification();
}
}).start();
}
@Override
public void onCreate()
{
startNotificationListener();
super.onCreate();
}
@Override
public int onStartCommand(Intent intent,int flags,int startId)
{
return super.onStartCommand(intent,flags,startId);
}
@Override
public void onDestroy()
{
super.onDestroy();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
public void ShowNotification()
{
NotificationManager notificationManager =
(NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(getBaseContext(),"notification_id")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("title")
.setContentText("content")
.setDefaults(NotificationCompat.DEFAULT_SOUND)
.build();
notificationManager.notify(0, notification);
//the notification is not showing
}
}
you will get the notification.please check screen shot
How to fix notification not showing up when app is running?
When the app is closed, your notifications are processed by the Google Service process, which take care of displaying your notifications as required, including the default click action (opening the app) and the notification icon.
When the app is in foreground, the received messages are processed by the app, and since there’s no logic to handle it, nothing will happen!
You can create a custom FirebaseMessagingService
which implement the onMessageReceived
and then create the notification in it..
public class NotificationService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle(remoteMessage.getNotification().getTitle())
.setContentText(remoteMessage.getNotification().getBody())
.setSmallIcon(R.mipmap.ic_launcher)
.build();
NotificationManagerCompat manager = NotificationManagerCompat.from(getApplicationContext());
manager.notify(some_int_number (eg : 123), notification);
}
}
And add this to your manifest file
<service android:name=”.NotificationService”>
<intent-filter>
<action android:name=”com.google.firebase.MESSAGING_EVENT”/>
</intent-filter>
</service>
Also make sure you give proper permission in manifest file.
After implementing try again, you should see the notification while your app is in foreground.
Notification Not Appearing on notify()
It has to do with NotificationChannel. In Android Oreo and up you can create a NotificationChannel like this:
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String channelId = "my_channel_id";
CharSequence channelName = "My Channel";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{1000, 2000});
notificationManager.createNotificationChannel(notificationChannel);
Then you create your Notification as below:
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int notifyId = 1;
String channelId = "my_channel_id";
Notification notification = new Notification.Builder(MainActivity.this)
.setContentTitle("My Message")
.setContentText("My test message!")
.setSmallIcon(R.drawable.ic_notification)
.setChannel(channelId)
.build();
notificationManager.notify(id, notification);
This way The notification will use the proper notification channel and will be displayed correctly. You can also create groups for notification channels. Read more here:
Documenation - NotificationChannel
Examples
Related Topics
Android Save View to Jpg or Png
How to Use Setarguments() and Getarguments() Methods in Fragments
Android: Programmatically Detect If Device Has Hardware Menu Button
Facebook - "Cannot Query Users by Their Username" Solution
Debugging SQLite Database on the Device
How to Clear an Imageview in Android
In Android Edittext, How to Force Writing Uppercase
Using Seekbar to Control Volume in Android
Proguard Hell - Can't Find Referenced Class
Getting Access to Media Player Cache
Android: Determine Security Type of Wifi Networks in Range (Without Connecting to Them)
Phonegap - Forcing Landscape Orientation
How to Re-Sign an .Apk with a Different Certificate Than What It Came With