Why my Android notification's setTicker doesn't work? - android

my code is below:
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(MainActivity.this)
.setContentTitle("nothing")
.setContentText("nothing").setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setTicker("help help help:)")
.setContentInfo("nothing")
.build();
manager.notify(1, notification);
When I run this, "help help help:)" didn't appear.

TickerText will only appear on phone before Android 5.0 (L)
Extracted from Documentation :
Text that summarizes this notification for accessibility services. As
of the L release, this text is no longer shown on screen, but it is
still useful to accessibility services (where it serves as an audible
announcement of the notification's appearance).
https://developer.android.com/reference/android/app/Notification.html#tickerText

Related

How to set badge count in Oreo without showing a notification?

I used to show a number in app icon using this library as follows:
ShortcutBadger.applyCount(context, numberToShow);
OneSignal also has same function in its Android SDK.
Now in Oreo, with the introduction of notification channels, things get complex to me. I can create a channel. Then, I can also create a notification as follows:
public static void createNotification(Context context, int numberToShow) {
Notification notification = new NotificationCompat.Builder(context, context.getString(R.string.notification_channel_id))
.setContentTitle("Dummy Title")
.setContentText("Dummy content")
.setSmallIcon(R.drawable.app_icon)
.setNumber(numberToShow)
.build();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(0, notification);
}
However, I have to show a notification with this solution, which I don't need and thus don't want. Is there any way in Oreo that I can achieve the same thing I have done previously, i.e. just showing 'notification dot' or a number attached to the app icon?
Sorry, but there is no SDK-level support for showing numbers or other badges on launcher icons, other than the Notification scenario that you described.
set the importance of the notification channel to
IMPORTANCE_MIN
like int importance = NotificationManager.IMPORTANCE_MIN;
and then create the channel as -
NotificationChannel nChannel = new NotificationChannel
(channelId, title, importance);
This will the set the badge count(shown on the long press of the icon) without notifying the user about any notification in the system tray. Though the notification will be in the tray, but will not pop up and quietly reside there.

Android heads-up notification not showing

I am trying to make heads-up notification work. Notification is created, but it's not displayed at the top of the app.
Here's the code responsible for building a notification:
Notification notification = new NotificationCompat.Builder(context)
.setSmallIcon(android.R.drawable.arrow_up_float)
.setContentTitle("Check running time - click!")
.setContentText(String.valueOf(elapsedTime))
.setContentIntent(pendingIntent)
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(Notification.PRIORITY_HIGH)
.setVibrate(new long[0])
.build();
The device that I'm trying to run the app is API 21. I've seen many threads, but no solution given works for me.
You need to do two things:
Make sure that your notification is properly configured. See: https://developer.android.com/guide/topics/ui/notifiers/notifications#Heads-up
AND make sure the phone is properly configured (I think this is where most get stuck).
Step 1. Configure the notification.
First, register your notification channel like so
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String name = getString(R.string.channel_name);
String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_HIGH; //Important for heads-up notification
NotificationChannel channel = new NotificationChannel("1", name, importance);
channel.setDescription(description);
channel.setShowBadge(true);
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
Then, create a notification, like so:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, "1")
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(textTitle)
.setContentText(textContent)
.setDefaults(DEFAULT_SOUND | DEFAULT_VIBRATE) //Important for heads-up notification
.setPriority(Notification.PRIORITY_MAX); //Important for heads-up notification
Finally, send the notifciations as you would do normally, e.g.:
Notification buildNotification = mBuilder.build();
NotificationManager mNotifyMgr = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(001, buildNotification);
Step 2. Configure the phone.
I noticed that I have to enable some additional settings on my phone (a Xiaomi Note 3):
Here are some ways to reach the menu:
Long press a notification, in the notification bar.
Go to: Settings > Installed apps > Select your app > Notifications
Improving on the previous step, you can help users partially by sending them to the installed apps menu, by using this intent:
startActivity(new Intent(Settings.ACTION_MANAGE_APPLICATIONS_SETTINGS));
Finally, when you reach this menu enable a setting called something like "Floating notification" (the name of this setting varies between devices).
your code is almost fine. I'm using DEFAULT_VIBRATE instead of DEFAULT_ALL:
builder.setPriority(Notification.PRIORITY_HIGH);
if (Build.VERSION.SDK_INT >= 21) {
mBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
}
But, you can also set
builder.fullScreenIntent(sameAsContentPendingtIntent);
but ^ this one is non-deterministic. I mean that system choose to display a HeadsUp or launch Intent. In most cases it shows HeadUp, but I'm not counting on it because of Android versions, Manufacturers, launchers and so so on.
Not at last, you also need to define right Notification Channel. I think you had already done this, because you wouldn't see any notification if you don't :-) Oh lovely Android :-) Anyway, I want to say that also NotificationChannel needs to be in high priority:
channel = new NotificationChannel("uniqueId", "name", NotificationManager.IMPORTANCE_HIGH);
channel.enableVibration(true);
And also, I advise to you, check latest opinions at developer.android.com
Google is going to be more strict from now. Not only for notifications but also for 'targetApi', but that is another story, pardon me :-)
Have a nice code today
try do this :
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(android.R.drawable.arrow_up_float)
.setContentTitle("Check running time - click!")
.setContentText(String.valueOf(elapsedTime))
.setContentIntent(pendingIntent)
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(Notification.PRIORITY_HIGH)
.setVibrate(new long[0]);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
I've encountered the same issue. The solution is simply properly setting the vibration builder.setVibration(NotificationCompat.DEFAULT_VIBRATE).
According to Google:
The notification has high priority and uses ringtones or vibrations on devices running Android 7.1 (API level 25) and lower.
I hope it helps :)
The answers provided above were correct, but are partially correct now.
For future readers, notice that
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(Notification.PRIORITY_HIGH)
will NOT resolve the problem.
According to "Android for developers",
https://developer.android.com/guide/topics/ui/notifiers/notifications.html#Heads-up
Example conditions that might trigger heads-up notifications include the following:
The user's activity is in fullscreen mode (the app uses fullScreenIntent).
The notification has high priority and uses ringtones or vibrations on devices running Android 7.1 (API level 25) and lower.
The notification channel has high importance on devices running Android 8.0 (API level 26) and higher.
Notice that the second solution (which is mostly mentioned under this question), only works for Android 7.1 or lower.
For Android 8.0 and higher, you should create a notification channel whose importance is NotificationManager.IMPORTANCE_HIGH.
Some sample code:
NotificationChannel channel =
new NotificationChannel(
"MY_OWN_CHANNEL_ID",
"MY_OWN_CHANNEL_NAME",
NotificationManager.IMPORTANCE_HIGH);
and after that, make sure that your notification builder uses this channel to display
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(smallIcon)
.setContentTitle("Title")
.setContentText("Content")
.setChannelId("MY_OWN_CHANNEL_ID")
.setPriority(Notification.PRIORITY_HIGH)
.setVibrate(new long[0]);
Now you should be able to see heads-up notifications properly.
Just use like
NotificationManager mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent myintent = new Intent(this, MainActivity.class);
myintent.putExtra("message", msg);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
myintent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("ttile")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(1, mBuilder.build());

Android Notification Not working on Android 6.0 (Marshmallow)

I'm implementing local notification on Android and I have the problem that they are not appearing on Android 6.0 (Samsung S7).
I was searching for solutions, but I coulnd't find anything for this problem. I have the icon in the proper res/drawable folder, also I have defined a notification title, text, ringtone (raw folder) but it's not showing up...
There is my code:
Context acontext = getApplicationContext();
PackageManager pm = acontext.getPackageManager();
Intent notificationIntent = pm.getLaunchIntentForPackage(acontext.getPackageName());
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(acontext, 0, notificationIntent, 0);
int notification_icon = acontext.getResources().getIdentifier("icon", "drawable", acontext.getPackageName());
int notificationID = 0;
// Build notification
Notification noti = new Notification.Builder(acontext)
.setContentTitle("Title")
.setContentText("Incoming text")
.setSmallIcon(notification_icon)
.setContentIntent(pendingIntent)
.setLights(Color.RED, 1, 1)
.build();
NotificationManager notificationManager = (NotificationManager) acontext.getSystemService(Context.NOTIFICATION_SERVICE);
// hide the notification after its selected
noti.sound = Uri.parse("android.resource://" + acontext.getPackageName() + "/raw/incoming");
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(notificationID, noti);
Did anyone else experience this problem? Any help would be appreciated. Thank you.
There are some changes in new notification. new NotificationCompat.Builder(this) is deprecated and need NotificationChannelfor android oreo above. You can try this solution.
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("Title");
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);
NotificationManager mNotificationManager =
(NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("notify_001",
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT);
mNotificationManager.createNotificationChannel(channel);
}
mNotificationManager.notify(0, mBuilder.build());
There are various layers involved in showing notifications in Android, Please first check if this works for other devices of same OS version. Normally these kind of issues are device specific not OS specific, Also check notification logs how to check notification logs in android ?
If issue is device specific, following can be handy tips:
Check notifications not blocked for your app by device settings.
Check power settings blocking non-priority notifications.
If there are notifications in logs but not showing up, there can be some issue with your internal OS settings/configs. (believe me Android is not very clean OS)
If you can, try factory resetting your device. (In my case this worked)
Google Play Services must be enabled in order to receive push notifications on your Android device. If Google Play Services are enabled and the general troubleshooting steps have not resolved the issue, it may be necessary to reset the app. To reset the app, go to Settings → Apps → PagerDuty and tap Clear Data.
For Android 6.0 and newer, make sure the app is set as a prioritized app in priority mode.
For Android 6.0 and newer, check to see if the app is being silenced by Doze mode.
Check the android version accordingly set the icon for > 6.0 and for other.For 6.0 version we need white background icon.
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
icon = R.mipmap.your_logo_for_Lolipop;
}else{
icon = R.drawable.your_logo_for_Kitkat ;
}

Emoji on Android Notification

I'm trying to display an emoji on notification bar.
Here is my string:
"\ue057 " + getString(R.string.notification_sent_hey)
I've already tried using the softbank, and every format possible: "U+1F601", "\xF0\x9F\x98\x81"
What should I do?
Thanks.
You can't do it like that. Notifications have a layout, and you are forced to use that layout.
A big icon, text, and a little icon. That's it. It's quite clearly stated here: http://developer.android.com/design/patterns/notifications.html
So, you can follow the example explained here: http://developer.android.com/training/wearables/notifications/creating.html
For example:
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_event)
.setContentTitle(eventTitle)
.setContentText(eventLocation)
.setLargeIcon(R.drawable.ic_event2);
.setContentIntent(viewPendingIntent);
// Get an instance of the NotificationManager service
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(this);
// Build the notification and issues it with notification manager.
notificationManager.notify(notificationId, notificationBuilder.build());

Android notification is not showing

I need a program that will add a notification on Android. And when someone clicks on the notification, it should lead them to my second activity.
I have established code. The notification should be working, but for some reason it is not working. The Notification isn't showing at all. I don't know what am I missing.
Code of those 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's selected
notificationManager.notify(0, n);
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());
Actually the answer by ƒernando Valle doesn't seem to be correct. Then again, your question is overly vague because you fail to mention what is wrong or isn't working.
Looking at your code I am assuming the Notification simply isn't showing.
Your notification is not showing, because you didn't provide an icon. Even though the SDK documentation doesn't mention it being required, it is in fact very much so and your Notification will not show without one.
addAction is only available since 4.1. Prior to that you would use the PendingIntent to launch an Activity. You seem to specify a PendingIntent, so your problem lies elsewhere. Logically, one must conclude it's the missing icon.
You were missing the small icon.
I did the same mistake and the above step resolved it.
As per the official documentation:
A Notification object must contain the following:
A small icon, set by setSmallIcon()
A title, set by setContentTitle()
Detail text, set by setContentText()
On Android 8.0 (API level 26) and higher, a valid notification channel ID, set by setChannelId() or provided in the NotificationCompat.Builder constructor when creating a channel.
See http://developer.android.com/guide/topics/ui/notifiers/notifications.html
This tripped me up today, but I realized it was because on Android 9.0 (Pie), Do Not Disturb by default also hides all notifications, rather than just silencing them like in Android 8.1 (Oreo) and before. This doesn't apply to notifications.
I like having DND on for my development device, so going into the DND settings and changing the setting to simply silence the notifications (but not hide them) fixed it for me.
Creation of notification channels are compulsory for Android versions after Android 8.1 (Oreo) for making notifications visible. If notifications are not visible in your app for Oreo+ Androids, you need to call the following function when your app 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);
}
}
You also need to change the build.gradle file, and add the used Android SDK version into it:
implementation 'com.android.support:appcompat-v7:28.0.0'
This worked like a charm in my case.
I think that you forget the
addAction(int icon, CharSequence title, PendingIntent intent)
Look here: Add Action
I had the same issue with my Android app. I was trying out notifications and found that notifications were showing on my Android emulator which ran a Android 7.0 (Nougat) system, whereas it wasn't running on my phone which had Android 8.1 (Oreo).
After reading the documentation, I found that Android had a feature called notification channel, without which notifications won't show up on Oreo devices. Below is the link to official Android documentation on notification channels.
Notifications Overview, Notification anatomy
Create and Manage Notification Channels
For me it was an issue with deviceToken. Please check if the receiver and sender device token is properly updated in your database or wherever you are accessing it to send notifications.
For instance, use the following to update the device token on app launch. Therefore it will be always 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) {
}
});
}
});
I encountered a similar problem to yours and while searching for a solution I found these answers but they weren't as direct as I hoped they would be but it gives an Idea; Your notifications may not be showing because for versions >=8 notifications are done relatively differently there is a NotificationChannel which aids in managing notifications this helped me. Happy coding.
void Note(){
//Creating a notification channel
NotificationChannel channel=new NotificationChannel("channel1",
"hello",
NotificationManager.IMPORTANCE_HIGH);
NotificationManager manager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.createNotificationChannel(channel);
//Creating the notification object
NotificationCompat.Builder notification=new NotificationCompat.Builder(this,"channel1");
//notification.setAutoCancel(true);
notification.setContentTitle("Hi this is a notification");
notification.setContentText("Hello you");
notification.setSmallIcon(R.drawable.ic_launcher_foreground);
//make the notification manager to issue a notification on the notification's channel
manager.notify(121,notification.build());
}
Make sure your notificationId is unique. I couldn't figure out why my test pushes weren't showing up, but it's because the notification ids were generated based on the push content, and since I was pushing the same notification over and over again, the notification id remained the same.
Notifications may not be shown if you show the notifications rapidly one after the other or cancel an existing one, then right away show it again (e.g. to trigger a heads-up-notification to notify the user about a change in an ongoing notification). In these cases the system may decide to just block the notification when it feels they might become too overwhelming/spammy for the user.
Please note, that at least on stock Android (tested with 10) from the outside this behavior looks a bit random: it just sometimes happens and sometimes it doesn't. My guess is, there is a very short time threshold during which you are not allowed to send too many notifications. Calling NotificationManager.cancel() and then NotificationManager.notify() might then sometimes cause this behavior.
If you have the option, when updating a notification don't cancel it before, but just call NotificationManager.notify() with the updated notification. This doesn't seem to trigger the aforementioned blocking by the system.
If you are on version >= Android 8.1 (Oreo) while using a Notification channel, set its importance to high:
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
val pendingIntent = PendingIntent.getActivity(applicationContext, 0, Intent(), 0)
var notification = NotificationCompat.Builder(applicationContext, CHANNEL_ID)
.setContentTitle("Title")
.setContentText("Text")
.setSmallIcon(R.drawable.icon)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentIntent(pendingIntent)
.build()
val mNotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
mNotificationManager.notify(sameId, notification)

Categories

Resources