I'd like to remove the ContentIntent from my android wear notification (I have a separate action i'd like to use when opening app from watch), but still keep it on the handheld. The following code sets the content intent and then ends up removing it later. Does anyone know how to do this?
mainNotification.setContentIntent(contentIntent)
// wearable extender to add 2nd page and extend the main notification
dualNotification =
new NotificationCompat.WearableExtender()
.extend(mainNotification)
.extend(wearableExtendedNotification)
.setContentIntent(null) // thisremoves content intent that was set before
;
I also tried using setContentAction() which would work fine but it squishes the first notification card down on the wearable so the text isn't readable.
Related
I've been trying for a while to replicate some of the demo notifications from the Android Wear app with custom layouts, but there is one thing I can't get working: showing the custom layout when the notification is minimized. I'm using the setDisplayIntent() method to embed an activity into the notification with custom a layout.
The thing is, when the notification is expanded the activity's layout is displayed properly. But then the notification minimizes the activity is hidden and the default notification data is displayed (contentTitle, contentText).
I'm displaying the notification with setDisplayIntent() from the wear application as shown here:
Intent page1Intent = new Intent(context, Page1Activity.class);
Notification page1notif = new Notification.Builder(context)
.setContentTitle("Notification title")
.setSmallIcon(R.drawable.logo_renfe)
.extend(new Notification.WearableExtender()
.setBackground(BitmapFactory.decodeResource(context.getResources(), R.drawable.train_background))
.setDisplayIntent(PendingIntent.getActivity(context, 0, page1Intent, PendingIntent.FLAG_UPDATE_CURRENT))
.setCustomSizePreset(Notification.WearableExtender.SIZE_LARGE))
.build();
Here's what I'm trying to achieve:
And here's what I've got:
What kind of sorcery is Google doing to show their demo cards? Is there something from the API that I've missed or are they using a different technique?
Thanks!
I just succesfully created a bunch of pages notifications on my Wear device.
The only problem is that the PendingIntent does not seems to start an Activity
(which is of course declared in Manifest).
Here is my code:
List extras = new ArrayList();
Intent viewIntent = new Intent(getApplicationContext(), DetailActivity.class);
viewIntent.putExtra("KEY", "TEST123");
//Note: I also tried: Intent viewIntent = new Intent(getApplicationContext(), DetailActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent viewPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, viewIntent, 0);
for (Route aRoute : myRoutes) {
Notification aNotif = new NotificationCompat.Builder(getApplicationContext())
.setContentTitle("BUS " + aRoute.route_short_name)
.setContentText(aRoute.directions.get(0).trip_headsign)
.setLargeIcon(bitmap)
.setContentIntent(viewPendingIntent)
.setSmallIcon(R.mipmap.ic_launcher).build();
extras.add(aNotif);
}
NotificationCompat.Builder builder1 = new NotificationCompat.Builder(this)
.setContentTitle(title)
.setContentText(desc)
.setContentIntent(viewPendingIntent)//Just in case
.setSmallIcon(R.mipmap.ic_launcher);
Notification notification = builder1
.extend(new NotificationCompat.WearableExtender()
.addPages(extras))
.setContentIntent(viewPendingIntent)//Just in case
.build();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(0, notification);
When I press on a Notification, I expect the intent to start, but nothing happens..
Any suggestion is welcome.
EDIT:
This code works, just after the notification, so, the second activity can easily be launched withour bug:
startActivity(viewIntent);
EDIT2:
There is now an "open" button at the end that works fine, but still nothing happens on individual notifications (every pages)
Pages are not clickable - on Android Wear, only actions are clickable. For phone generated notifications, those only appear after all pages
If you have a content intent on your phone generated notification, that will always appear as an 'Open on phone' action. There is no way to disable this unless you remove your content intent (making the notification unclickable on phones).
I say 'phone generated' as you can also create a Wear app. By using the data layer to push messages to your Wear app, the Wear app can then build custom notifications. These notifications allow you to use setDisplayIntent() and display activities inline (either as the main page or as separate pages). These activities can, of course, contain any View you want, including actions to perform any action (such as send a message back to the phone to start a particular activity).
Note that because pages are not clickable by default, styling of a custom notification should make it very obvious that the items are clickable. Rather than using a custom notification activity, you may consider using setContentAction() to display the action icon inline with the rest of the layout - this removes the action as a separate element past the action and places it directly on the notification/page.
I'm developping a music player application for android and I'm stuck with a problem in the notification layout. It seems like I can't add any complicated element in it or it throws android.app.RemoteServiceException: Bad notification.
Problem is I wrote a player controller that is a Fragment. I can't find how to add the fragment by hand in code so I tried directly in XML layout but without success. I also had the problem with a custom ImageView that I wrote.
Am I really obliged to duplicate code ?
It does not look like you can add a fragment. You could see if MediaStyle fits your needs. In your notification builder you would add .setStyle(NotificationCompat.MediaStyle). Otherwise it looks like you would have to subclass Notification.Style or NotificationCompat.Style to create a custom layout. It also looks like for some options you can intercept the notification as it's being created. Check this out for more details.
Edit:
Given your time frame, and if you're willing to flex some on your layout, then I would just add buttons to the notification. Create a pending intent for each action you want to be able to do from your notification (play, pause, skip). By way of a code sample I've included an abbreviated version of how I put a dismiss button in my notifications.
Intent resultIntent = new Intent(context, AlarmScreen.class);
resultIntent.putExtra("Id",reminder.getId());
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
context,
reminder.getId()*2,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
Notification.Builder mBuilder =
new Notification.Builder(context)
.setStyle(new Notification.BigTextStyle()
.bigText(reminder.getDescription()))
.addAction(R.drawable.ic_stat_content_clear, "Dismiss", dismissPendingIntent)
.build();
In your case you should be able to replace the R.drawable.ic_stat_content_clear with an appropriate icon and maybe skip the text. You can just repeat .addAction() for each button you want. Notice also where I have reminder.getId()*2 in my pending intent declaration? I found that if I had the same number there for both of my buttons I got strange results, so one of my buttons has id*2 and the other has id*2+1.
As for how you handle the intents sent by the buttons, you'll have to create a BroadcastReceiver to receive them, and figure out where to go from there based on how you're implementing the rest of your logic.
That is not possible. Notification only able to use with RemoteView. RemoteView support some views only, and it do not support custom views or fragment.
I am using the following code showing the notification, it works properly. I am look for the listener from which i can come to know whether the notification is expanded or not.
I want to open the app if the notification is expanded or else open the particular activity of the app.
Intent intent= new Intent(context, Activity1.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, getNextRandom(), intent, 0);
Builder newBuilder newBuilder = new Notification.Builder(context);
newBuilder.setContentTitle(title)
.setContentText(subtitle)
.setAutoCancel(true)
.setSmallIcon(R.drawable.icon)
.setTicker(notificationMessage)
.setContentIntent(pendingIntent);
Notificationnotification = new Notification.BigTextStyle(newBuilder)
.setBigContentTitle(notificationTitle)
.bigText(text)
.build();
Let me know if there is way to acheive this?
There is no way to know if the notification is open... What you can do is add buttons to the expanded notification that pressing on them will act differently than pressing on the notification itself.
There maybe no direct way, but maybe the following hack can help!!
You can have a custom notification layout and use RemoteView, Set a setOnClickPendingIntent to launch a service for the entire view so that you get notified when the notification is clicked.
When the user clicks the notification, service is started and show the new expanded custom layout from the service (you replace the old notification with the new one)
Maybe show your initial notification also from the same service using startforeground, so that your app is not killed and the view click can be received faster since service is already running and response time for changing the view is lower.
see this for remoteview and this for launching service from notification click.
At the Google I/O Bytes video How We Customized Google Apps for Android Wear (https://www.youtube.com/watch?v=o5cne6vK-eo), I saw that for the Wearable-customized Camera App, they add a button directly on the notification (not at the back of the notification as a new page which would happen if addAction or setContentAction is used).
Does any one know which API I need to use in order to do that? I don't think there are using a custom Activity for the first view cos it just looks like the first screen of Android Wear when there is at least one Notification. I've tried to find for it in the documentations but couldn't get it. I've tried setDisplayIntent which is suggested by others but it doesn't seems to be the same one.
Use WearableExtender.setContentAction(int) to add an action directly to a notification card. The int parameter refers to the index of the action you have added to the notification (using NotificationCompat.Builder.addAction(NotificationCompat.Action)). See Creating a Notification for more info on how to create notification for wearables.
The sample code you can download using the SDK manager contains a sample project Notifications (located under /samples/android-20/wearable/Notifications) that shows how to create various types of notifications. Here is an edited snippet from that sample that shows how to create a notification with an embedded action:
NotificationCompat.Action action = new NotificationCompat.Action.Builder(
R.drawable.ic_result_open, null, NotificationUtil.getExamplePendingIntent(
context, R.string.example_content_action_clicked)).build();
NotificationCompat.Builder builder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Title")
.setContentText("Context Text")
.addAction(action)
.extend(new NotificationCompat.WearableExtender()
.setContentAction(0));
The video walks you though a few steps that are needed, but the main thing (and what you're asking for) is the Wearable Data Layer API. The first view (the card) is a notification, but that notification launches an Activity running on the wear device. That Activity is what displays the button and sends (through the Data Layer API) a message to the camera to take the picture.