Im trying to make my custom notification similar to this one
I found a bunch of similar questions here on SO and examples on the internet but no one helped me.
Here is my code:
RemoteViews bigView = new RemoteViews(mContext.getApplicationContext().getPackageName(), R.layout.notification_playback);
Intent i = new Intent(MainActivity.ACTION_PLAYER, null, mContext, MainActivity.class);
i.putExtra("CURRENT_TRACK", t);
i.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pi = PendingIntent.getActivity(mContext, 0, i, 0);
Notification n = new NotificationCompat.Builder(mContext)
.setContentTitle(t.getName())
.setContentText(t.getArtistName())
.setContentIntent(pi)
.setSmallIcon(R.drawable.ic_notification)
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.ic_launcher))
.setOngoing(true)
.setContent(bigView)
.setWhen(System.currentTimeMillis())
.build();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
n.bigContentView = bigView;
NotificationManager notifyMgr = (NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notifyMgr.notify(0, n);
It appears in default size and become a size I expect only after long tap on it.
Is it possible to make it a big size immediately without long tap ? And how can I do it if so ?
Thanks!
So, what i figured out.
Notification appears expanded if it is it the to of notifications. Otherwise it appears wit regular height an user can drag it down to expand.
In order to make it appear in the top of notifications you can use .setPriority(Notification.PRIORITY_MAX) in Builder.
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!
I'm currently working on an app with notification which are shown on a wear device. The notification contains an action binded onto the notification card (.setContentAction(0)).
Everything is working fine, except that it shows a confirmation message everytime someone clicks on the card.
Since the card gets updated as soon as someone clicks on it, it's not necessary to show a confirmation.
I already check the official documentation (https://developer.android.com/training/wearables/ui/confirm.html#show-confirmation) if there is a way to stop the confirmation, unfortunately I didn't find a solution so far.
Edit 09.07.2015
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setGroup("GROUP")
.setGroupSummary(false)
.setAutoCancel(false)
.setPriority(Notification.PRIORITY_HIGH)
.setSmallIcon(R.drawable.ic_timer_white_48dp);
ArrayList<NotificationCompat.Action> actions = new ArrayList<>();
NotificationCompat.Action control = new NotificationCompat.Action.Builder(icon, null, pendingTimeIntent).build();
actions.add(control);
builder.extend(new NotificationCompat.WearableExtender().addActions(actions).setContentAction(0).setBackground(background));
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(context);
notificationManager.notify(Constants.NOTIFICATION_ID_WEAR, builder.build());
you could try modify your constructor like:
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setGroup("GROUP")
.setGroupSummary(false)
.setAutoCancel(false)
.setPriority(Notification.PRIORITY_HIGH)
.setShowWhen(true)
.setSmallIcon(R.drawable.ic_timer_white_48dp);
Note the line:
.setShowWhen(true);
And you can modify the flag of the Intent to false:
Intent intent = new Intent(this, ConfirmationActivity.class);
intent.putExtra(ConfirmationActivity.EXTRA_ANIMATION_TYPE,
ConfirmationActivity.SUCCESS_ANIMATION);
intent.putExtra(ConfirmationActivity.EXTRA_MESSAGE,
getString(R.string.msg_sent));
intent.putExtra(ConfirmationActivity.EXTRA_SHOW_WHEN, false);
startActivity(intent);
I don't know if it works properly, but I hope it gives you a clue.
I want to show a simple notification on android wear then i want to give a action on tap on notification,i don't want to show any paging in notification.
I seen many examples but all are of paging, i just want to give a simple notification and want to handle on tap event.
You have to change my resources to yours. setContentAction(0) is what you generally need.
public void showNotification() {
Intent eventIntent = new Intent(context, GridCardsActivity.class);
PendingIntent openAppIntent = PendingIntent.getActivity(context, 1, eventIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification.Action openAction = new Notification.Action(R.drawable.logo_sm, "open", openAppIntent);
Notification notification = new Notification.Builder(context)
.setContentTitle(pulse.getName())
.setContentText(pulse.getQuestion())
.setSmallIcon(R.drawable.logo)
.extend(new Notification.WearableExtender()
.addAction(openAction)
.setContentAction(0)
.setHintHideIcon(true)
.setBackground(bm)
)
.build();
((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE))
.notify((int) System.currentTimeMillis(), notification);
}
I have an app that kicks off a notification on a Wear device (the notification is created and shown on the Wear device).
What I want to do is have the "first" notification page display some high level info (app name, scroll to see more, etc...), and then have a list of pages containing content after that.
That works fine. The issue is that I want to attach an Action to each page (to kick off a PendingIntent). However, no matter what I try, I can't get the page to perform the Action.
I've tried:
setContentIntent
addAction
addAction and extend(new Notification.WearableExtender().setContentAction(0))
Anyone have any ideas?
I'm using Notification, not NotificationCompat, but I don't think that should make a difference.
UPDATE: I'm creating the notification on the watch. Here is the code I use:
private void createNotifications(ArrayList<Thing> things) {
DataApi data = Wearable.DataApi;
int notificationId = 0;
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification.Builder mainBuilder = new Notification.Builder(this)
.setSmallIcon(R.drawable.default_icon)
.setContentTitle("Things")
.setContentText("Swipe to see your things")
.setPriority(Notification.PRIORITY_DEFAULT);
List<Notification> pages = new ArrayList<>(things.size());
for(Thing thing : things) {
pages.add(createNotificationPageForThing(data, thing, notificationId).build());
notificationId++;
}
Notification n = new Notification.WearableExtender().addPages(pages).extend(mainBuilder).build();
nm.notify(notificationId, n);
}
private Notification.Builder createNotificationPageForThing(DataApi data, Thing thing, int notificationId) {
Asset bitmapAsset = getBitmapAsset(data, contact);
Intent thingIntent = new Intent(this, WearDetailActivity.class);
thingIntent.putExtra(WearDetailActivity.DETAIL_EXTRA, thing);
PendingIntent thingPendingIntent = PendingIntent.getActivity(this, notificationId, thingIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification.Action action = new Notification.Action(R.drawable.ic_action_social_person, "More details", thingPendingIntent);
Notification.Builder builder = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_action_social_person)
.setContentTitle(thing.getDisplayName())
.setPriority(Notification.PRIORITY_DEFAULT)
.addAction(action)
.extend(new Notification.WearableExtender().setContentAction(0));
if(bitmapAsset != null) {
try {
Bitmap b = BitmapFactory.decodeStream(
data.getFdForAsset(connection.getClient(), bitmapAsset).await().getInputStream());
builder.setLargeIcon(b);
} catch (Throwable ignore) {}
}
return builder;
}
#Eliezer, reading at the following line I did not understand what exactly is the behaviour you're experiencing ... explaining it in details might be helpful to debug your problem.
However, no matter what I try, I can't get the page to perform the Action.
Using .addAction() should work in your case. You'd want to use WearableExtender if you were creating the notification from the device, not the watch itself. Just to confirm something obvious - you're passing the .addAction a PendingIntent right?
Here's a code snippet from one of my applications, which accomplishes exactly what you're aiming for - I have just "Swipe for actions" text in the first page, and the next 2 pages perform different actions.
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
Intent openIntent = new Intent(this, CommonWearIntentService.class);
openIntent.setAction(CommonWearIntentService.ACTION_OPEN_ON_PHONE);
PendingIntent openPendingIntent = PendingIntent.getService(this, 0, openIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent addTransactionIntent = new Intent(this, CommonWearIntentService.class);
addTransactionIntent.setAction(CommonWearIntentService.ACTION_ADD_TRANSACTION);
PendingIntent addTransactionPendingIntent = PendingIntent.getService(this, 0, addTransactionIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(getString(R.string.app_name))
.setContentText(getString(R.string.swipe_for_actions))
.addAction(R.drawable.add_transaction, getString(R.string.add_transaction), addTransactionPendingIntent)
.addAction(R.drawable.common_full_open_on_phone, getString(R.string.common_open_on_phone), openPendingIntent)
.setVibrate(new long[]{500, 500, 500, 1000})
.setLights(Color.BLUE, 3000, 3000)
.setOngoing(true);
notificationManager.notify(ONGOING_NOTIFICATION_ID, builder.build());
Hope this helps!
Per the documentation, you must use NotificationCompat. After you switch to that setContentIntent() looks to be the right way to set the PendingIntent.
Example from the linked docs:
// Create builder for the main notification
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.new_message)
.setContentTitle("Page 1")
.setContentText("Short message")
.setContentIntent(viewPendingIntent);
// Create a big text style for the second page
BigTextStyle secondPageStyle = new NotificationCompat.BigTextStyle();
secondPageStyle.setBigContentTitle("Page 2")
.bigText("A lot of text...");
// Create second page notification
Notification secondPageNotification =
new NotificationCompat.Builder(this)
.setStyle(secondPageStyle)
.build();
// Add second page with wearable extender and extend the main notification
Notification twoPageNotification =
new WearableExtender()
.addPage(secondPageNotification)
.extend(notificationBuilder)
.build();
// Issue the notification
notificationManager =
NotificationManagerCompat.from(this);
notificationManager.notify(notificationId, twoPageNotification);
I followed this Sample Code
In Big Text Notifications section, he said that need expand to see Big text notification form, as image im below :
I wonder that we can not set Expanded Notification as default in Big Text Notifications?
People who know it is can or not,
If can,
Please tell me how to do it,
Thanks,
The documentation states:
A notification's big view appears only when the notification is
expanded, which happens when the notification is at the top of the
notification drawer, or when the user expands the notification with a
gesture.
So my answer is no, you can't expand it by default.
There is however a trick to push the notification to the top of the list where it would be expanded. Simply set the Priority to Notification.PRIORITY_MAX and chances are that your app's notification will make it to the top.
Notification noti = new Notification.Builder()
... // The same notification properties as the others
.setStyle(new Notification.BigPictureStyle().bigPicture(mBitmap))
.build();
You change
.setStyle(new NotificationCompat.BigTextStyle().bigText(th_alert))
along with the announcement
notification = new NotificationCompat.Builder(context)
Here is an example:
You can set Code
Intent intent = new Intent(context, ReserveStatusActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
intent = new Intent(String.valueOf(PushActivity.class));
intent.putExtra("message", MESSAGE);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(PushActivity.class);
stackBuilder.addNextIntent(intent);
// PendingIntent pendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
// android.support.v4.app.NotificationCompat.BigTextStyle bigStyle = new NotificationCompat.BigTextStyle();
// bigStyle.bigText((CharSequence) context);
notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(th_title)
.setContentText(th_alert)
.setAutoCancel(true)
// .setStyle(new Notification.BigTextStyle().bigText(th_alert) ตัวเก่า
// .setStyle(new NotificationCompat.BigTextStyle().bigText(th_title))
.setStyle(new NotificationCompat.BigTextStyle().bigText(th_alert))
.setContentIntent(pendingIntent)
.setNumber(++numMessages)
.build();
notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notificationManager.notify(1000, notification);
notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText("Your Long Text here"))
simply setStyle of your notification builder.
No need of doing any kind of modification in the any file for showing of multiple lines of Notification while using Push Notification V5, remove the field "style", from the object you are sending. Automatically the Multiple lines of notification will be seen. For more information, upvote the answer, ask your query. I will help you out.
For reference, visit this question
From this notification code you have got images, and big text or more.
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mBuilder.setSmallIcon(R.drawable.small_logo);
mBuilder.setColor(Color.parseColor("#D74F4F"));
} else {
mBuilder.setSmallIcon(icon);
}
mBuilder.setTicker(title).setWhen(when);
mBuilder.setAutoCancel(true);
mBuilder.setContentTitle(title);
mBuilder.setContentIntent(intent);
mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
mBuilder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), icon));
mBuilder.setContentText(msg);
mBuilder.setPriority(Notification.PRIORITY_MAX);
if (Utils.validateString(banner_path)) {
mBuilder.setStyle(notiStyle);
} else {
mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(msg));
}
Notification noti = mBuilder.build();
notificationManager.notify(0, noti);