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);
}
Related
I researched and the only answers I could find either told:
How to disable the notification bar from being pulled down.
How to cancel a notification using:
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(0);
Both of these aren't what I require as the notification gets cancelled fine. I've set two actions in my notification: 'Dismiss' and 'Open Activity'. As the name implies, upon clicking both, the above code executes and clears the notification but doesn't cause the notification bar from going back up. This is needed, specially when the second Notification Action causes an Activity launch.
I tried on Android Lollipop and Nougat and the notification bar didn't go back up in either. So if someone could kindly tell me if it is even possible and how.
Thanks.
The code for building the notification:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder
.setSmallIcon(icon).setTicker(message).setWhen(when)
.setAutoCancel(true).setContentTitle("Kindly record your Voice")
.setColor(Color.RED);
notificationIntent = new Intent(this,ReminderReceiver.class);
notificationIntent.setAction("Record");
PendingIntent pendIntent1 = PendingIntent.getBroadcast(getApplicationContext(), 1, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.addAction(R.drawable.common_google_signin_btn_icon_dark, "Record", pendIntent1);
notificationIntent2 = new Intent(this, ReminderReceiver.class);
notificationIntent2.setAction("Dismissed");
PendingIntent pendIntent2 = PendingIntent.getBroadcast(getApplicationContext(), 1, notificationIntent2, PendingIntent.FLAG_UPDATE_CURRENT);
builder.addAction(R.drawable.starticon, "Dismiss", pendIntent2);
notificationIntent3 = new Intent(this, CancelReceiver.class);
PendingIntent pendIntent3 = PendingIntent.getBroadcast(getApplicationContext(), 1, notificationIntent3, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setDeleteIntent(pendIntent3);
notification = builder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(1, notification);
Might seem weird, but the notification bar doesn't go up when there are other notifications present. If yours is the only notification, the bar slides up automatically upon clicking.
I need to achieve notification in android wear like in whatsapp where each conversation is a list and sliding to the right allows the user to reply to the respective conversation. I tried stacking from the android developers example but it only displays messages as is. How could I set more than 1 message and respective action like in whatsapp?
Edit:
NotificationCompat.WearableExtender wearOptions =
new NotificationCompat.WearableExtender()
.setHintHideIcon(true);
String replyLabel = mXmppConnectionService.getResources().getString(R.string.wear_reply);
RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY)
.setLabel(replyLabel)
.build();
Intent replyIntent = new Intent(mXmppConnectionService, XmppConnectionService.class);
PendingIntent replyPendingIntent =
PendingIntent.getActivity(mXmppConnectionService, 0, replyIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Action action =
new NotificationCompat.Action.Builder(R.mipmap.ic_launcher,
"reply to", replyPendingIntent)
.addRemoteInput(remoteInput)
.build();
final Builder mBuilder;
mBuilder.setDefaults(0);
mBuilder.setSmallIcon(R.drawable.ic_notification);
mBuilder.setPriority(getPriority());
mBuilder.setDeleteIntent(createDeleteIntent());
mBuilder.setLights(0xff00FF00, 2000, 3000)
.extend(wearOptions)
.extend(new NotificationCompat.WearableExtender().addAction(action));
final Notification notification = mBuilder.build();
notificationManager.notify(NOTIFICATION_ID, notification);
you are missing essentially three different things:
you are not calling setGroup("GROP_NAME") on your NotificationBuilder
Notifications that belong to the same group must have different ids. If you notify always with the same id, NOTIFICATION_ID in your case, ste stacking will not work
you want a different replyPendingIntent for each notification, otherwise your pending intent will refer to the last notification notified. Instead of hard-coding 0, pass a different value for each notification.
The rest looks good,
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'm following Google's instructions here https://developer.android.com/training/wearables/notifications/creating.html
However, unsurprisingly, their code doesn't work. Specifically I'm trying to do this:
// Build the notification and add the action via WearableExtender
Notification notification =
new Notification.Builder(context)
.setSmallIcon(R.drawable.buzz_icon)
.setContentTitle("Buzz")
.setContentText("OpenBuzz")
.extend(new Notification.WearableExtender().addAction(action))
.build();
I want an action specific to the Wearable, so I have no choice but to use Notification.WearableExtender(). But it's addAction method only accepts an action as it's parameter. Here is my code for creating an action:
Notification.Action action =
Notification.Action.Builder(R.drawable.buzz_icon, actionIntent.toString(), pendingIntent);
Which doesn't work, as Android Studio says "Method call expected"
How can I successfully create a Notification.Action?
Or how else might I add a Wearable specific action to my notification?
You're on the right track.
You need to create a new NotificationCompat.Action.Builder and then call build() on it. Like this:
NotificationCompat.Action action =
new NotificationCompat.Action.Builder(android.R.drawable.ic_menu_call, "Only in wearable", pendingIntent)
.build();
Also, make sure that the action is defined as NotificationCompat.Action, not Notification.Action.
This example illustrates how to add a notification with an action (e.g. open the main activity).
NotificationCompat.Builde mBuilder = new NotificationCompat.Builder(this, null);
Intent myIntent = new Intent(this, MainActivity.class);
PendingIntent myPendingIntent = PendingIntent.getActivity(this, 0, myIntent, 0);
mBuilder.setContentTitle("Your_title")
.setContentText("Some_text")
.setSmallIcon(R.drawable.app_icon)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setOngoing(true)
.addAction(R.drawable.open_icon, "Open", myPendingIntent)
.setAutoCancel(false);
NotificationManager mNotifyManager = (NotificationManager) getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
mNotifyManager.notify(1, mBuilder.build());
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);