So I tried to make notification
Intent resultIntent = new Intent(
this, NavigationActivity.class
);
PendingIntent resultPendingIntent = PendingIntent.getActivity(
this, 0, resultIntent, PendingIntent.FLAG_CANCEL_CURRENT
);
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.drawable.app_icon)
.setContentTitle("Message received")
.setContentText("Message body")
.setContentIntent(resultPendingIntent)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
And it doesn't appear. Just nothing happen, no error or something. The log is empty. Android 6.0. The icon resource is exists.
Is there a way to know for sure what is the problem? Where to look for the error message?
Try to use NotificationCompat.Builder instead of Notification.Builder. For more information look at link.
Wow, I'm sooooo idiot. I had the system settings switch on, that blocks all the notifications for the app. Sorry for that.
Related
I have been using notification on my other app and it is working just fine. I have also implemented channelId, but when I am using the similar code for new app, notification is just not showing up. No errors reported.
Following is the code I am using
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notIntent = new Intent(context, hk.class);
// notIntent.setAction(BuildConfig.APP_ID + ".inspire");
notIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendInt = PendingIntent.getActivity(context, ALARM_REQUEST_CODE,
notIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification.Builder mBuilder =
new Notification.Builder(context)
.setContentIntent(pendInt)
.setSmallIcon(R.drawable.spl)
// .setLargeIcon()
.setContentTitle(intent.getStringExtra("not_title"))
.setContentText(intent.getStringExtra("not_text"))
.setPriority(Notification.PRIORITY_HIGH)
.setDefaults(Notification.DEFAULT_VIBRATE)
.addAction(android.R.drawable.ic_menu_share, "Share", PendingIntent.getActivity(context, 0,
new Intent().setAction(Intent.ACTION_SEND).putExtra(Intent.EXTRA_TEXT,
"\n\n" + "-His Holiness Bhakti Rasamrita Swami\n\n").setType("text/plain"), 0))
.setWhen(System.currentTimeMillis())
.setStyle(new Notification.BigTextStyle().bigText(intent.getStringExtra("big_text")));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mBuilder.setVisibility(Notification.VISIBILITY_PUBLIC);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("daily", "Daily Nectar", NotificationManager.IMPORTANCE_HIGH);
channel.setDescription("Daily Nectar");
channel.enableLights(true);
mNotificationManager.createNotificationChannel(channel);
mBuilder.setChannelId("chId");
}
mNotificationManager.notify(ALARM_REQUEST_CODE, mBuilder.build());
Log.d("AlarmReceiver", "Notification Created"); //this log is printed in console
I have tested using Logs and thus I can ensure that this function is called, so no problem with alarm.
Strangely it doesn't throw any errors also and very similar code on other app is working well. So, I checked the notification setting for this app and found that notification settings are also enabled.
Unable to detect what is the problem. Thank you for the help.
There was a silly mistake.
Note the following code
NotificationChannel channel = new NotificationChannel("daily", "Daily Nectar", NotificationManager.IMPORTANCE_HIGH);
channel.setDescription("Daily Nectar");
channel.enableLights(true);
mNotificationManager.createNotificationChannel(channel);
mBuilder.setChannelId("chId");
channel id passed to new NotificationChannel and channel id set on builder are different. A bug introduced mistakenly during the update.
Happy coding...
I searched many other questions related to this topic but found not satisfactory answer also none of them are working for me.
I want to show a continuous notification which should only be terminated by app. But the code i wrote was working a few days ago but not now.
private void GenNotification(String title, String body)
{
try
{
Log.i(Config.TAGWorker, "Generating Notification . . .");
Intent myIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(
this,
0,
myIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle(title)
.setContentText(body)
.setChannelId("myID")
.setTicker("Notification!")
.setWhen(System.currentTimeMillis())
.setContentIntent(pendingIntent)
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(false)
.setSmallIcon(R.drawable.floppy)
.setOngoing(true)
.build();
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, notification);
}
catch (Exception e)
{
Log.e(Config.TAGWorker, e.getMessage());
}
}
There is no exception recorded in Logcat, regarding ths. The code is called in onCreate of service. The service is starting correctly i can see in Log cat also there is no exception but notification is not shown. My OS is Android ONE for nokia (PI)
You are using a deprecated NotificationCompat.Builder constructor which takes a single argument (context); and that won't work on starting from Android 8.0 (API level 26).
So, to solve this:
Step 1: Create a Notification channel with the NotificationManager
NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Notification channels are only available in OREO and higher.
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel
("PRIMARY_CHANNEL_ID",
"Service",
NotificationManager.IMPORTANCE_HIGH);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setDescription("Description");
mNotificationManager.createNotificationChannel(notificationChannel);
}
Note: changeargument values as you wish
Step 2:: Use the non-deprecated Notification.Builder class with its two-argument constructor that takes a second argument as the channel ID which you assigned in the first step, where I set it to "PRIMARY_CHANNEL_ID"
Notification notification = new NotificationCompat.Builder
(this, "PRIMARY_CHANNEL_ID")
.setContentTitle("title")
.setContentText("body")
.setTicker("Notification!")
.setWhen(System.currentTimeMillis())
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_launcher_background)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setOngoing(true)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setAutoCancel(true)
.build();
mNotificationManager.notify(0, notification);
Did you check your strings (title and body) is not null if it's null notification wont show
Also check that you call your notification channels when you start your service every time if your android above 7.0
Clear notification when you recall it same id in your case is 1.
I have set up a notification like this in my .MainActivity class:
Intent downloadUpdateIntent = new Intent(mContext, updateAppDialogFragment.class);
PendingIntent downloadAppPendingIntent = PendingIntent.getActivity(mContext,0,downloadUpdateIntent,0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Update Available")
.setContentText("A new version of this app is available.")
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(Notification.PRIORITY_HIGH)
.addAction(0,"Update",downloadAppPendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(mContext.NOTIFICATION_SERVICE);
notificationManager.notify(1, mBuilder.build());
What i need is to, when i click on update button, it should show updateAppDialogFragment to show an updating progress. When i generally use this class, i need to make such a call updateAppDialogFragment.newInstance(). But here, I cant understand how to accomplish this. Anyone to help?
I have very basic knowledge about intent. Actually, what i need to know is to use the intent to call a method.
I'm new to android. And to show notification on android phones I got this below code
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /*Request code*/ , intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.mytune_default)
.setContentTitle("FCM Message")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int m = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);
notificationManager.notify(m /*ID of notification*/ , notificationBuilder.build());
But the thing is I just want that notification unread count on code appview on system, but dont want other things loke message, title etc that appear on notification tray of system. How come this be acheived>
It's not possible to replace launcher icon with widget. You can say to user to place your widget on the home screen, but not in application list. Users must do that in their own. Nevertheless, it's iOs pattern. It's not good idea to bring iOS design elements to android. Android have great notification bar, where user expect to see notification from your app. Android users don't expect to see it over the icon.
I want to create a Notification in my Android App, but my code doesn't work!
NotificationManager nManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder nBuilder = new Notification.Builder(this);
nBuilder.setContentTitle("Title...");
nBuilder.setContentText("Text...");
nBuilder.setContentInfo("Info?");
nBuilder.setWhen(System.currentTimeMillis());
//Intent notificationIntent = new Intent(this, MainActivity.class);
//PendingIntent pendingIntent = new PendingIntent.getActivity(this, 0, notificationIntent, 0);
nManager.notify(noti_ID, nBuilder.build());
Thanks :)
From developer.android.com
Required notification contents
A Notification object must contain the following:
A small icon, set by setSmallIcon()
A title, set by setContentTitle()
Detail text, set by setContentText()
So set a small icon too.