i need to implement buttons below my push notification
and i have 2 device Samsung S5 and Meizu MX5
notification Meizu looks like this
all is ok
but notification Samsung looks worse
here is my code
public void With_buttons(View view) {
Bitmap icon = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_android_black_24dp);
Intent resultIntent = new Intent(this, ResultActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(ResultActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification.Builder(this)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setSmallIcon(R.drawable.ic_android_black_24dp)
.setContentTitle("Wonderful music")
.setContentText("My Awesome Band")
.setLargeIcon(icon)
.addAction(R.drawable.ic_android_black_24dp, "Previous", resultPendingIntent) // #0
.addAction(R.drawable.ic_android_black_24dp, "Pause", resultPendingIntent) // #1
.addAction(R.drawable.ic_android_black_24dp, "Next", resultPendingIntent) // #2
.build();
mNotificationManager.notify((int) System.currentTimeMillis(), notification);
}
what is the case of this bug? i expect to see buttons the same like on Meizu device.
What i am doing wrong?
Related
I'm trying to create a simple notification for a music player that has 3 buttons, Pause, Previous and Next. My code only shows one of them which is Next. Any suggestions where I did a mistake?
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
Intent playNextIntent = new Intent(this, BroadcastReceiver.class);
playNextIntent.setAction("NEXT_ACTION");
PendingIntent nextPendingIntent = PendingIntent.getBroadcast(this, 1, playNextIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent pauseIntent = new Intent(this, BroadcastReceiver.class);
pauseIntent.setAction("PAUSE_ACTION");
PendingIntent pausePendingIntent = PendingIntent.getBroadcast(this, 2, pauseIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent playPreviousIntent = new Intent(this, BroadcastReceiver.class);
playPreviousIntent.setAction("PREVIOUS_ACTION");
PendingIntent prevPendingIntent = PendingIntent.getBroadcast(this, 3, playPreviousIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, "D")
.setSmallIcon(R.drawable.ic_launcher_background) //notification icon
.setContentTitle("Simple Music Player")
.setContentText("Currently playing: " + mediaPlayerHolder.getSongsList().get(songIterator).getSongName())
.setPriority(NotificationCompat.PRIORITY_MAX)
.setWhen(0)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setOngoing(true) //user can't remove notification
.addAction(R.drawable.ic_previous, "Previous", prevPendingIntent) // #0
.addAction(R.drawable.ic_pause, "Pause", pausePendingIntent) // #1
.addAction(R.drawable.ic_next, "Next", nextPendingIntent) // #2
.setStyle(new NotificationCompat.BigTextStyle())
.setContentIntent(pendingIntent); //on click go to app intent
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
// notificationId is a unique int for each notification that you must define
notificationManager.notify(1, mBuilder.build()); //show notification
There's nothing wrong with your code. If you run your code in an emulator on stock android the actions show up as expected.
On Android displaying notifications is done by the launcher. Like any other app launchers can contain bugs. In this case there is a bug in the launcher you're using to display multiple actions in a notification.
I recommend you to try a google sample Universal Music player Here is a GitHub link . It includes all of you want in notifications and it has a latest notification Android Oreo features just try out it .
I have two problem with my app:
1. I don't know how to retrieve the color of the small icon of notification when a notification arrives?
2. Can I edit app name that displayed at top of the notification by programmatically?
I'm using android Nougat .
You can use the below code to set smallIcon :(Use Small icon same color as Title Color)
Change color by this site
.setSmallIcon(R.drawable.ic_launcher_icon)
Reference Code :
private void sendNotification(String messageBody,String message2, Bitmap image, String TrueOrFalse) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("AnotherActivity", TrueOrFalse);
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)
// .setLargeIcon(image)/*Notification icon image*/
.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_launcher_icon))
.setSmallIcon(R.drawable.ic_launcher_icon)
.setContentTitle(messageBody)
.setContentText(message2)
.setColor(getResources().getColor(R.color.colorAccent))
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(image))/*Notification with Image*/
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
This API seems to allow custom title and icon: https://developer.android.com/reference/android/app/Notification.Builder.html
I created a notification using this code:
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
final int notificationID = NotificationID.getID();
PendingIntent pendingIntent = PendingIntent.getActivity(this, notificationID, intent, PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notif_icon)
.setContentTitle(messageTitle)
.setStyle(new NotificationCompat.BigTextStyle().bigText(messageTitle + "\n" +messageBody))
.setContentText(messageTitle + " " +messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notificationID, notificationBuilder.build());
This works well on many devices, however on a Samsung Galaxy S6 android 7 the notification is cancelled immediately.
I tried putting the ongoing flag and the notification remains visible, however this is not a solution because the user won't be able to cancel it be swiping the notification.
Any idea what might cause the problem?
Remove setAutoCancel(true) from your code.
setAutoCancel()
Notification.Builder setAutoCancel (boolean autoCancel) Make this
notification automatically dismissed when the user touches it.
Reference: https://developer.android.com/reference/android/app/Notification.Builder.html#setAutoCancel(boolean)
I want to display a notification for the users of my app every day at a specific time. Maybe at 1pm. So how do I do this? I know, with an AlarmManager or Calender. But how?
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("TEST")
.setContentText("Benachrichtigung");
Intent resultIntent = new Intent(this, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(123, mBuilder.build());
Thanks!
I've a sample app about the Alarm Manager. You can take a look it from here. What you need to do basically is, push notification whenever you receive an alarm in alarm receiver Broadcast Receiver.
I tried to create a notification that starts a activity by clicking on it and which you can't swipe away.
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("DroidSchool")
.setContentText("DroidSchool l\u00E4uft im Hintergrund...");
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, Intent.FLAG_FROM_BACKGROUND);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int mId = 1234567890;
mNotificationManager.notify(mId, mBuilder.build());
with the code above the notification gets displayed, but nothing happens when I click on it and you can swipe it away.
To keep the Notification, in that way the user can't click it away in any way, add this flag
Notification mNotification = mBuilder.build();
notification.flags = Notification.FLAG_NO_CLEAR;
mNotificationManager.notify(mId, notification);
To start an Activity you have to use this flag for your Intent
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
Do like this:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("DroidSchool")
.setContentText("DroidSchool l\u00E4uft im Hintergrund...");
Intent intent = new Intent(YourActivity.this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification noti = mBuilder.build();
noti.flags |= mBuilder.build().FLAG_NO_CLEAR | mBuilder.build().FLAG_ONGOING_EVENT;
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, noti);
You want a foreground service as detailed here
It's worth noting that only android 4.3 added the persistent notification you seem to be after and it can be overriden by the users settings.
try using like this
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, Intent.FLAG_UPDATE_CURRENT);
in MainActivity.java
Dont not consume your notification; ie instead of calling below 2 line code in oncreate() call under onDestro()
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.cancel(mId);
You can use NotificationCompat.Builder's .setOngoing(true), use it like this:
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setAutoCancel(true)
.setContentTitle("Downloading Something")
.setContentText("Downloading")
.setSound(null)
.setDefaults(0)
.setOngoing(true);
// Makes it so the notification is clickable but not destroyable by swiping-away or hitting "clear all"