How to add button directly on Notification on Android Wear - android

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.

Related

Customizing full screen intent notifications android

Project Target API 30 Android 10, Min API 19 KitKat
I am creating a parental control app where parents can restrict certain apps.
I have a foreground service where I would I ideally trigger an activity-like notification from the service that would envelop the user's entire screen or take them to the home screen.
I have learned that starting activities, including going to the home screen, is no longer possible under normal circumstances as of API 29. https://developer.android.com/guide/components/activities/background-starts
After reading the documentation it seems creating a full screen intent notification is the most highly recommended workaround to the activity restriction.
I am currently working with the following code for my full screen intent notification:
Intent blockedIntent = new Intent(App.getContext(), BlockedItemReceiver.class);
blockedIntent.putExtra("currentApp", restrictedApp.name);
PendingIntent pendingBlockedIntent = PendingIntent.getActivity(App.getContext(), 50, blockedIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder blockedNotificationBuilder = new NotificationCompat.Builder(App.getContext(), CHANNEL_BLOCKED_FROM_ITEM)
.setSmallIcon(R.drawable.ic_baseline_lock_24)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentTitle(restrictedApp.packageName + " was Blocked!")
.setContentText(restrictedApp.name + " will be available again DAY at TIME")
.setCategory(NotificationCompat.CATEGORY_CALL)
.setFullScreenIntent(pendingBlockedIntent, true);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(5, blockedNotificationBuilder.build());
I looked at some documentation for customizing notifications, but the information I found does not have a full screen intent notification example, and when I attempt to add the methods in the documentation example for NotificationCompat.Builder such as setCustomContentView() with custom layouts, my notification fails to appear without an error message.
https://developer.android.com/training/notify-user/custom-notification
Besides, I do not want a collapsed and expanded version of my notification as the example in the link has, just one full screen view.
TLDR; I want a notification that always engulfs the screen until the user presses a button on the notification to dismiss it. How can I further customize my full screen intent notification to truly take up the full screen? Ideally with a layout. If I must have a collapsed version of my notification, I don't want the user to ever see it, because I always want my notification to engulf the screen while it's showing.
There are existing apps such as AppBlock that have found a workaround to launching an activity-like thing from a foreground service that takes up the full screen, so what I am trying to do is possible even if the specific question I'm asking won't lead me to that result. Please suggest another way of accomplishing what I am after if what I am asking to do in my question is "impossible". What I generally want to do is certainly possible.

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.

Android Wear custom notification layout when minimized

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!

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.

Categories

Resources