I am not talking about the white icon shown which is the common problem asked in the case of Lollipop.
I am getting no small icon itself when my service is running and in the widget it is not showing my image instead it is showing the ic_launcher image of the andorid.
I have created a music service so I have Play, Pause and Next action which is also not shown in my notification widget. Although it is showing setContentTitle and setContextText correctly.
My code :
Notification part :
Bitmap icon = BitmapFactory.decodeResource(getResources(),R.drawable.notification_image);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("Music Player")
.setTicker("Playing Music")
.setContentText("My Song")
.setSmallIcon(R.drawable.notification_image)
.setLargeIcon(Bitmap.createScaledBitmap(icon,128,128,false))
.setContentIntent(pendingIntent)
.setOngoing(true)
.addAction(android.R.drawable.ic_media_previous,"Previous",ppreviousIntent)
.addAction(android.R.drawable.ic_media_play,"Play",pplayIntent)
.addAction(android.R.drawable.ic_media_next,"Next",pnextIntent)
.build();
startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE,notification);
I have created a list of action constants in my Constants class that is being referred to here.
Full Function :
private void showNotification(){
Intent notificationIntent = new Intent(this,MainActivity.class);
notificationIntent.setAction(Constants.ACTION.MAIN_ACTION);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,notificationIntent,0); //Allow the action to happen as though the main user (MainActivity) is calling.
Intent previousIntent = new Intent(this,PlayerService.class);
previousIntent.setAction(Constants.ACTION.PREV_ACTION);
PendingIntent ppreviousIntent = PendingIntent.getActivity(this,0,previousIntent,0);
Intent playIntent = new Intent(this,PlayerService.class);
playIntent.setAction(Constants.ACTION.PLAY_ACTION);
PendingIntent pplayIntent = PendingIntent.getActivity(this,0,playIntent,0);
Intent nextIntent = new Intent(this,PlayerService.class);
nextIntent.setAction(Constants.ACTION.NEXT_ACTION);
PendingIntent pnextIntent = PendingIntent.getActivity(this,0,nextIntent,0);
Bitmap icon = BitmapFactory.decodeResource(getResources(),R.drawable.notification_image);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("Music Player")
.setTicker("Playing Music")
.setContentText("My Song")
.setSmallIcon(R.drawable.notification_image)
.setLargeIcon(Bitmap.createScaledBitmap(icon,128,128,false))
.setContentIntent(pendingIntent)
.setOngoing(true)
.addAction(android.R.drawable.ic_media_previous,"Previous",ppreviousIntent)
.addAction(android.R.drawable.ic_media_play,"Play",pplayIntent)
.addAction(android.R.drawable.ic_media_next,"Next",pnextIntent)
.build();
startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE,notification);
}
I need this :
But I am just getting the Title and ContextText and in the image I am getting the launcher icon and no Play, Previous, Next Button
Edit : I tested the same code in Nexus 5X API 19 and it worked properly. My phone is Lollipop 5.1.1 it is not working.
Also whatever I set as my app launcher icon that is what is shown as the notification icon. And the Action buttons area completely invisible in the notification widget.
The full code here : https://github.com/torrtuga/Music-App
Please try this #Hack
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable. notification_image))
Related
My android app tracks the user's location and compares it to the locations in a list, and if it finds a match, it creates a heads-up notification that remains visible at the top of the screen (the green band in this image).
Then if the user taps the notification, it starts another activity (DisplayActivity) using a pendingIntent. This process may be repeated many times in a journey, with a new notification for each one.
Here is my code:
void makeNotification() {
if (SDK_INT >= Build.VERSION_CODES.O) {
int soundName;
String NOTIFICATION_CHANNEL_ID = "AMJnotifychannel";
Intent goShow = new Intent(this, DisplayActivity.class);
goShow.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
goShow.putExtra("placeno", placeNumber);
goShow.putExtra("plname", placeName);
goShow.putExtra("justquiz", quizOnly);
goShow.putExtra("addedscore", scoreAdded);
goShow.putExtra("killlquiz", false);
PendingIntent pendingIntent = PendingIntent.getActivity(this, notificationNumber, goShow, PendingIntent.FLAG_MUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews expandedView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.expandednotify);
expandedView.setTextViewText(R.id.placename, placeName);
NotificationManager notificationManager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
soundName = R.raw.alert;
Uri sound = Uri.parse("android.resource://" + getPackageName() + "/" + soundName);
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "amjchannel", NotificationManager.IMPORTANCE_HIGH);
channel.setSound(sound, audioAttributes);
channel.shouldVibrate();
channel.enableLights(true);
channel.enableVibration(true);
channel.setLockscreenVisibility(NotificationCompat.VISIBILITY_PUBLIC);
notificationManager.createNotificationChannel(channel);
builder.setSound(sound);
builder.setSmallIcon(R.mipmap.amjnotify);
builder.setContentIntent(pendingIntent);
builder.setCustomContentView(expandedView);
builder.setCustomBigContentView(expandedView);
builder.setAutoCancel(true);
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
builder.setPriority(NotificationCompat.PRIORITY_HIGH);
builder.setFullScreenIntent(pendingIntent, true);
builder.setOngoing(true);
builder.setChannelId(NOTIFICATION_CHANNEL_ID);
notificationManager.notify(717, builder.build());
}
}
As long as the user does tap the notification each time and starts DisplayActivity, this works fine - new notifications continue to appear as a heads-up notifications at the top of the screen and the user doesn't have to open the notification drawer to see them.
However, if at any time the user dismisses a notification by swiping it upwards and off the screen, the next time a notification occurs it behaves differently. Swiping a notification away into the drawer just once means that, after that, all future notifications no longer appear at the top of the screen automatically - they go straight into the drawer and the user then has to open the drawer to see them.
I have tried this on a number of Android builds using emulators - API28, API29, API30 and API33 - always with the same result.
These notifications are essential to my app, which is useless without them. Can anyone tell me how to make sure that new notifications appear on the screen every time, as in the picture, even after the user has swiped one away into the notification drawer.
And just to make the puzzle more complicated..........
I have just tried deleting the old notification using:
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(717);
before creating a new one. As before, everything is ok if the user taps the notification and opens the new activity each time. However, if he swips the notification away one time, then whenever there is another notification after that it goes straight to the displayActivity without waiting for the user to tap the intent.
I'm totally confused!
This is my code so far on understanding notification.
Intent mIntent = new Intent(this, typeof(MainActivity));
Intent gIntent = new Intent(this, typeof(GameMenu));
Intent sIntent = new Intent(this, typeof(SceneMenu));
PendingIntent mainIntent = PendingIntent.GetActivity(this, 0, mIntent, 0);
PendingIntent gameIntent = PendingIntent.GetActivity(this, 0, gIntent, 0);
PendingIntent sceneIntent = PendingIntent.GetActivity(this, 0, sIntent, 0);
var notifSound = RingtoneManager.GetDefaultUri(RingtoneType.Notification);
//long[] pattern = new long[] { 1000, 500, 1000, 2000, 3000, 500, 500, 1000};
Notification n = new Notification.Builder(this)
.SetContentTitle("RBOS Notification")
.SetContentText("You won $1million")
.SetColor(Color.LightGreen)
//.SetVibrate(pattern)
.SetPriority((int)NotificationPriority.Max)
.SetShowWhen(true)
.SetSound(notifSound)
//.SetTicker(DateTime.Now.ToString("hh:mmtt"))
.SetSmallIcon(Resource.Drawable.ic_fb)
.SetContentIntent(mainIntent)
.SetAutoCancel(false)
.SetLights(Color.Green, 1000, 1000)
.AddAction(Resource.Drawable.ic_main, "Main", mainIntent)
.AddAction(Resource.Drawable.ic_play, "Game", gameIntent)
.AddAction(Resource.Drawable.ic_scene, "Scene", sceneIntent).Build();
NotificationManager notificationManager =
(NotificationManager)GetSystemService(NotificationService);
notificationManager.Notify(0, n);
I am a bit confused on how to set the icon itself for notification rather than words. target api is jellybean to higher.
on my phone Asus with nougat OS, it only display words. In other phone Starmobile and OPPO, it display the icons but with only part of words. I want to display something like radio notif or vlc player notif with previous play/pause next icon buttons alone (without its words).
And for side question. What is the use of ticker? I can't see any changes when I put it there. Also in OPPO, why notification didn't appear instantly (like when battery is low) even priority set in max
Standard notifications will appear different on the various APIs (and even different among the same API level but different OEMs).
It seems you want to create a RemoteViews and use it as your notification's CustomContentView (like a media player in the notification drawer). That way you can override the default notification template using your own layout and content.
Example:
var remoteView = new RemoteViews(PackageName, Resource.Layout.Alarm);
remoteView.SetInt(Resource.Id.myButton, "setText", Resource.String.rocks);
var notification = new Notification.Builder(this)
.SetSmallIcon(Android.Resource.Drawable.IcDialogInfo)
.SetCustomContentView(remoteView)
.Build();
The Resource.Layout.Alarm layout contains just one Button, thus the notification is just one button across the notification drawer:
I am having issues with the 5.0 MediaSession controls. My app streams live music so I do not use a pause button, only a stop button. I cannot figure out how to only display a stop button on my wear device.....the stop button shows up and works fine from the Notification on my phone....but on Moto360 only displays the standard pause/play toggle. I can get the correct behavior from my watch when I override onPause(), but I want to use onStop() in the MediaSession callbacks. I do not want to confuse my users by showing the pause button.
private void buildNotification(Notification.Action action)
{
Notification.MediaStyle style = new Notification.MediaStyle();
style.setMediaSession(mSession.getSessionToken());
style.setShowActionsInCompactView(0);
Intent intent = new Intent(getApplicationContext(), Play.class);
intent.setAction(ACTION_STOP);
if ((selectedUrl.equals(MouseWorldRadioActivity.radioFiveIP)) || (selectedUrl.equals(MouseWorldRadioActivity.scrIP)))
{
PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0,
new Intent(getApplicationContext(), nowPlaying.class),0);
Notification.Builder builder = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_stat_now_playing)
.addAction(action)
.setContentTitle("Mouseworld Radio")
.setContentText(metaData)
.setContentIntent(pi)
.setOngoing(true)
.setStyle(style);
lNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
lNotificationManager.notify(NOTIFICATION_ID, builder.build());
//build the notification with stop action---this is called in a different block
buildNotification(generateAction(R.drawable.ic_av_stop, "Stop", ACTION_STOP));
I'm trying to build an Android application that sends a notification to an Android Wear device.
The notification needs to have a content action set, so that the user can directly activate the action by clicking on the button displayed in the notification.
However, using the below code, the action appears on the next page, just like a regular action, and not on the notification:
Context context = getApplicationContext();
// Create an intent for the reply action
Intent actionIntent = new Intent(this, getClass());
PendingIntent actionPendingIntent =
PendingIntent.getActivity(this, 0, actionIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
// Create the action
NotificationCompat.Action action =
new NotificationCompat.Action.Builder(R.drawable.common_signin_btn_icon_dark, "ActionTitle", actionPendingIntent).build();
NotificationCompat.Builder builder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.common_signin_btn_icon_dark)
.setContentTitle("Title")
.setContentText("Context Text")
.addAction(action)
.extend(new NotificationCompat.WearableExtender()
.setContentAction(0));
// Get an instance of the NotificationManager service
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(this);
// Build the notification and issues it with notification manager.
notificationManager.notify(0, builder.build());
This is how it looks:
After swiping:
It is supposed to all be on a single page, with the Action button imbedded into the notification, like this:
What am I doing wrong?
Please try the possible solutions here:
Android Wear - Notification - setContentAction() not working
Can't create "Single-action control" notification android wear
I want notification which will stick on notification bar until music stop playing it.
currently i have written some code in which i can show notification but when i press clear notification button or swap it than it disappear from notification center.
I want notification like spotify which stays on bar until you stop playing music.
Here is my code for notification
int pendingRequestCode = 0;
// final Resources res = getResources();
notificationManager = (NotificationManager) getSystemService(
NOTIFICATION_SERVICE);
Intent i = new Intent(getApplicationContext(),Mainactivity.class);
Notification.Builder builder = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_action_search)
.setAutoCancel(true)
.setTicker("test ckick")
.setContentIntent(PendingIntent.getActivity(getApplicationContext(), NOTIFICATION_DEFAULT, i,0));
// Sets a custom content view for the notification, including an image button.
layout = new RemoteViews(getPackageName(), R.layout.notification);
layout.setTextViewText(R.id.notification_title, getString(R.string.app_name));
Intent clickIntent = new Intent();
clickIntent.setAction(ACTION_DIALOG);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), pendingRequestCode, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
layout.setOnClickPendingIntent(R.id.notification_button,pendingIntent);
builder.setContent(layout);
// Notifications in Android 3.0 now have a standard mechanism for displaying large
// bitmaps such as contact avatars. Here, we load an example image and resize it to the
// appropriate size for large bitmaps in notifications.
layout.setImageViewResource(R.id.notification_button, R.drawable.pause);
notificationManager.notify(NOTIFICATION_DEFAULT, builder.getNotification());
waiting for reply
Use setOngoing(true) to indicate that the event is ongoing. You may also wish to remove setAutoCancel(true), as that clears the Notification when the user taps upon it.
notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL