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));
Related
Im testing the push notification with my app.
when App in the foreground:
Step 1. Received the notification (in system tray).
2. now, I'm in some other screen than the home screen.
3. Actual Problem: On tap on the notification, it is going to the home screen.
4. Expected: If the app is in the foreground, just I need to cancel on tap of the notification. (No need to swipe.)
when App in background/killed: (Works well)
Step 1. Received the notification (in the system tray)
2. On tap, open the home screen of the app.
Tried with setting launch mode flags in intent. Not helped. Below is my code. Please suggest the solution guys.
Intent resultIntent = new Intent(this, MainActivity.class);
//resultIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// resultIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this);
mBuilder.setContentIntent(resultPendingIntent);
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
mBuilder.setContentTitle(title);
mBuilder.setContentText(body);
mBuilder.setAutoCancel(true);
mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(body));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mBuilder.setChannelId(TestUtils.creatChanel(this).getId());
}
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(642, mBuilder.build());
Not sure about dismiss notification on tap, but since your concern is wrong navigation.
We can check app is in foreground or not and prevent new activity to be opened from notification click, if app is in foreground.
//If app is in foreground setting pending intent to null
PendingIntent pendingIntent;
Intent notificationIntent = new Intent(getApplicationContext(), Main2Activity.class);
if(isAppInForeground()){
Log.e("--^","inForeground");
pendingIntent = null;
}else{
Log.e("--^","inBackground");
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
}
Add this function (SOURCE: link)
private boolean isAppInForeground() {
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> services = activityManager.getRunningAppProcesses();
boolean isActivityFound = false;
if (services.get(0).processName
.equalsIgnoreCase(getPackageName()) && services.get(0).importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
isActivityFound = true;
}
return isActivityFound;
}
In this case if notification came when app is in foreground, it will do nothing if clicked. So user has only one option left to swipe it to remove.
You can create Custom Notification with close button to close notification using RemoteViews
// create Notification with RemoteViews:
RemoteViews remoteViews= new RemoteViews(getApplicationContext().getPackageName(), R.layout.your_custom_notification);
Intent closeIntent = new Intent(context, CloseNotificationService.class);
hangUpIntent.setAction("close");
PendingIntent pendingCloseIntent = PendingIntent.getBroadcast(this, 0, closeNotification, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.cancel_notification, pendingCloseIntent);
// create notification here..
Notification customNotification = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setStyle(new NotificationCompat.DecoratedCustomViewStyle())
.setCustomContentView(remoteViews)
.build();
OnClick of close button it will redirect to service class:
public class CloseNotificationService extends IntentService {
/**
* Creates an IntentService. Invoked by your subclass's constructor.
*/
public CloseNotificationService() {
super("notificationIntentService");
}
#Override
protected void onHandleIntent(#Nullable Intent intent) {
switch (intent.getAction()) {
case "close":
Handler hangUpHandler = new Handler(Looper.getMainLooper());
hangUpHandler.post(new Runnable() {
#Override
public void run() {
NotificationManager notifManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notifManager.cancel(notificationId); // get notification id from intent.
}
});
break;
}
}
}
For more information of RemoteViews you can refer official google developer website https://developer.android.com/training/notify-user/custom-notification
Instead of this:
Intent resultIntent = new Intent(this, MainActivity.class);
do this:
Intent resultIntent = getPackageManager().getLaunchIntentForPackage("your.package.name");
and put that in your Notification. This will launch the app if it is not already running, otherwise it will just bring the app's task to the foreground in whatever state it was when the user last used it. If the user is already in the app (ie: on another screen), this will do nothing when the user clicks the Notification.
Should be exactly what you are looking for.
Inside your launcher activity have you tried notification manager class cancelAll() method??
In this way if there is already a notification on launch then it will cancelled automatically
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))
On my radio streaming app i want to stop the player from playing.
How to add a button in the notification to stop or kill the player?
I have this so far:
private void initNotification() {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent intent = PendingIntent.getActivity(StreamService.this, 0, new Intent(this, Radio.class), 0);
builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_ricky)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon))
.setContentTitle("Radio")
builder.setContentIntent(intent);
builder.setOngoing(true);
notificationManager.notify(NOTIFICATION_ID, builder.build());
}
Add a close button to the notification layout. Create some intent like this:
final ComponentName srvComp = new ComponentName(getContext(), service.getClass());
Intent close = new Intent(YourRadioService.ACTION_PLAYER_CLOSE);
close.setComponent(srvComp);
mPiClose = PendingIntent.getService(getContext(), 4, close, PendingIntent.FLAG_UPDATE_CURRENT);
Inflate your RemoteView somehow, for example:
RemoteViews smallNotification = new RemoteViews(getContext().getPackageName(), R.layout.notification_small);
smallNotification.setOnClickPendingIntent(R.id.close, mPiClose);
Set the content view on the notification
notification.contentView = smallNotification;
After that, use the notification manager to provide the notification
notificationManager.notify(NOTIFICATION_ID, notification);
Don't forget to handle the Intent. If you need more help look up how some open source music players are doing it.
I'm new to Android and at the moment I'm developing a MediaPlayer for online streaming. The mediaplayer runs fine and everything, but I'm having a problem with the Notification. When I press "play" the Notification appears in the notification bar and when I click on the notification in the notification bar it start a new activity instead of displaying the current one.
I don't know if I'm using the wrong FLAG for the PendingIntent for this Application.
My Code Looks like This:
public void Notification_Inicialization()
{
Intent intent = new Intent(this,radio_app_3.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
res = getApplicationContext().getResources();
app_notification = new Notification.Builder(this).setTicker(tickerTitle).setContentTitle(title)
.setSmallIcon(R.drawable.android_icon).setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.ic_launcher))
.setContentIntent(pendingIntent).addAction(R.drawable.android_icon,"Play",pendingIntent)
.getNotification();
notification_layout = new RemoteViews(getPackageName(),R.layout.activity_radio_app_3);
notification_layout.setTextViewText(R.id.button_play,getString(R.string.app_name));
notification_layout.setOnClickPendingIntent(R.id.button_play,pendingIntent);
app_notification.flags = Notification.FLAG_ONGOING_EVENT;
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(uniqueID,app_notification);
}
Hope someone can give me a help with this problem
Thanks in advance
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