Android Wear custom notification layout when minimized - android

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!

Related

Android bundled notifications behaviour

Android N introduced Bundled notifications:
posting 4 or more notifications without a group will cause those
notifications to be automatically bundled.
I'm creating four very similar notifications with different tag and id but with the same icon. After notifying NotificationManagerCompat and creating 4rd notification, all notifications are bundled into one with my application name and grey icon. And this leads to strange behaviour because it looks like that icon is greyed out shape of my applications launcher icon instead of smallIcon from NotificationCompat.Builder
I created sample app and tried to simulate the same environment, and created 4 notifications using same builder methods as in first application. Instead grey shape, I can see original smallIcon from builder which is correct behaviour.
On the screen, first notification is from my sample app, the second one is from my main app.
Creating notifications in both apps looks the same:
NotificationCompat.Builder notifBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.alert_octagon_white)
.setContentTitle("contentTitle 2")
.setTicker("ticker 2")
.setContentText("contentText 2")
.setStyle(new NotificationCompat.BigTextStyle().bigText("bigText 2"));
NotificationManagerCompat.from(this).notify("tag2", 2, notifBuilder.build());
Where can I look for the differences? How can I set bundled notification icon?
I am not sure of the difference in behavior you are seeing but i think something in Android Nougat default Notification bundling is screwed up.
To avoid that specifically call setGroup() and setGroupSummary() on your Builder object.That would solve the issue.

How to find that notification view is expanded or not

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.

Android Wear notifications: full screen

I want to create such kind of a notification (like here, in Breaking out of the card (with custom layouts) section). That is, it shoud contain a full screen page (in fact, with a map).
Is it (on a picture) an extended notification or a wear app? How to implement this full screen view?
I have tried doing as described here, but my activity is not starting from a notification.
You can embed an Activity into a Wear Notification like so:
Notification myFullScreenNotification = new Notification.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentText(notificationText)
.extend(new Notification.WearableExtender()
.setCustomSizePreset(Notification.WearableExtender.SIZE_FULL_SCREEN)
.setDisplayIntent(PendingIntent.getActivity(context, 0, new Intent(context, MyActivity.class), 0)))
.build();
Make sure you give the right permissions in your AndroidManifest.xml:
<activity
android:name=".MyActivity"
android:exported="true"
android:allowEmbedded="true"
android:taskAffinity="" />
The notification shows normally until the user swipes upwards, then it will turn into fullscreen and run the activity on the page.
If you want to use the Activity as a secondary page of the Notification, then insert the Notification above as a page of another notification like so:
Notification myWearNotification = new Notification.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentText(notificationText)
.extend(new Notification.WearableExtender()
.addPage(myFullScreenNotification)
.build();
An Android Wear app is required to fully customise the layout of a notification.
I tried coding a custom library which ships a layout xml from the phone to wear and inflate but that's not possible because of limitations relating to LayoutInflator.
Here's the relevant bit for reference: For performance reasons, view inflation relies heavily on pre-processing of XML files that is done at build time. Therefore, it is not currently possible to use LayoutInflater with an XmlPullParser over a plain XML file at runtime; it only works with an XmlPullParser returned from a compiled resource (R.something file.)

Remove ContentIntent from Android Wear but keep it on Handheld

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.

How to add button directly on Notification on Android Wear

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.

Categories

Resources