Get response after Android Wear Action - android

I'm trying to develop with Android Wear. I tried all the tutorial provided in the documentation, but now I want to try to do something smarter. I'm trying to get back the text that user says (with emulator written by computer keyboard), so I made it with this code:
protected void voiceNotification() {
// Crete intent for the response action
Intent replyIntent = new Intent(this, ReplyActivity.class);
// Adding intent to pending intent
PendingIntent replyPendingIntent = PendingIntent.getActivity(this, 0,
replyIntent, 0);
// Build the notification
NotificationCompat.Builder replyNotificationBuilder = new NotificationCompat.Builder(
this);
replyNotificationBuilder
.setSmallIcon(android.R.drawable.ic_btn_speak_now);
replyNotificationBuilder.setContentTitle("Messaggio");
replyNotificationBuilder.setContentText("Testo del messaggio");
replyNotificationBuilder.setContentIntent(replyPendingIntent);
replyNotificationBuilder.setNumber(++numMessages);
replyNotificationBuilder.setAutoCancel(true);
replyNotificationBuilder.setSound(RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
replyNotificationBuilder.setVibrate(new long[] { 1000, 1000 });
replyNotificationBuilder.setTicker("Hai una nuova notifica!");
// Create remote input
RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY)
.setLabel(getResources().getString(R.string.reply_label))
.build();
// Create the wearable notification
Notification replyNotification = new WearableNotifications.Builder(replyNotificationBuilder)
.addRemoteInputForContentIntent(remoteInput)
.build();
// Get the instance of NotificationManagerCompat and send my notification
NotificationManagerCompat.from(this).notify(0, replyNotification);
}
With this code on the emulator I'm getting 2 views: one with the text of my notification and a second one in which I can answer to notification with voice (keyboard with emulator). It's working all good, but I want to know if it's possible to get the text I said (wrote with emulator) to do something in my application (I saw on the emulator display that after I said/wrote somethings it appears 2 button "Edit" and "Send", so I think that with button "Send" I can get the text in my application to do something). I try to find out something in the documentation, but I don't find nothing. I hope you can help me to get this text.

You'll need to implement a Broadcast-receiver that listens to the pendingIntent you defined - the reply from the user will be passed in an extra string you defined in the RemoteInput - in your case this would be EXTRA_VOICE_REPLY.
You might want to have a look at these two files someone posted on GitHub in order to understand what is going on.
http://git.io/emKcrw
http://git.io/_PRW_w

Related

How to implement Android Bubbles notifications on Android 11(Api 30)+

I am trying to implement the Android Bubbles notifications API but it's not working for me, it's displaying as an ordinary notification. I am testing on emulator API 30(Android 11). I got the people-example working on the device, and I am following the Conversation Notifications guidelines.
The notification uses MessagingStyle.
(Only if the app targets Android 11 or higher) The notification is associated with a valid long-lived dynamic or cached sharing shortcut.
The notification can set this association by calling setShortcutId()
or setShortcutInfo(). If the app targets Android 10 or lower, the
notification doesn't have to be associated with a shortcut, as
discussed in the fallback options section.
The user hasn't demoted the conversation from the conversation section via notification channel settings, at the time of posting.
Please tell me what did I missed?
Also, I got a few optional questions about the design of Bubbles.
At what point of the app should I create the shortcuts and when to update it?
How the Person object needs to be cached?
This is what I got so far
Recipient recipient = ...; // Sender data
Message message = ...; // Message data
Intent intent = new Intent(context, ChatActivity.class);
intent.putExtra(ChatActivity.CONVERSATION_ID, message.conversationId);
PendingIntent bubbleIntent =
PendingIntent.getActivity(context, 0, intent, 0);
IconCompat icon = loadIcon(recipient);
Person person = loadPerson(recipient, icon);
NotificationCompat.MessagingStyle style = getMessagingStyle(person);
createOrVerifyChannel();
Notification notification = new NotificationCompat.Builder(context, CHANNEL_ID)
.setContentTitle(getNewMessagesCount(message) + " new messages with " + person.getName())
.setCategory(Notification.CATEGORY_MESSAGE)
.setContentText(message.text)
.setBubbleMetadata(
new NotificationCompat.BubbleMetadata.Builder()
.setDesiredHeight(600)
.setIntent(bubbleIntent)
.setAutoExpandBubble(true)
.setSuppressNotification(true)
.setIcon(icon)
.build()
)
.addPerson(person)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setWhen(message.date)
.setStyle(style)
.setShortcutInfo(
new ShortcutInfoCompat.Builder(context, message.conversationId + "")
.setActivity(new ComponentName(context, ChatActivity.class))
.setCategories(new HashSet<>(Collections.singletonList(Notification.CATEGORY_MESSAGE)))
.setIcon(icon)
.setPerson(person)
.setRank(0)
.setShortLabel(person.getName())
.setIntent(intent)
.build()
)
.build();
NotificationManagerCompat.from(context).notify(message.id + "," + message.type,
message.id, notification);
Manifest
<activity
android:name=".screens.chat.ChatActivity"
android:allowEmbedded="true"
android:resizeableActivity="true"
tools:targetApi="n" />
Gradle
targetSDKVersion 30
implementation 'androidx.appcompat:appcompat:1.3.0-alpha02'
The one thing that I missed was adding .setLongLived(true) for the ShortcutInfoCompat. It solves the problem.
I learned that it best to manage the ShortcutInfo on the app level since you can have at most 5 at a time, so there is no harm caching those in memory, it includes the Person object as well.
Also, you should add LocusId for the NotificationCompat, this id is shared among shortcuts, notifications, and views. To add it to the views you need to put in some extra work, as described in ContentCaptureManager

How to prevent android quick reply notification from being stuck in loading?

My app uses the Android N's new quick-reply feature to quickly take notes from a fixed notification, without having the user to open an activity.
However, I would like the notification to be reset to its initial state after the user sends the quick reply message, without having to dismiss the notification.
Here is my code:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ? createChannel() : "").setSmallIcon(android.R.mipmap.sym_def_app_icon).setContentTitle("My Awesome App").setContentText("Doing some work...").setContentIntent(pendingIntent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
android.support.v4.app.RemoteInput remoteInput = new RemoteInput.Builder(BookmarkCreatorReceiver.TXT_REPLY).setLabel("Reply").build();
Intent replyIntent = new Intent(this, BookmarkCreatorReceiver.class);
PendingIntent replyPendingIntent = PendingIntent.getBroadcast(this, 0, replyIntent, 0);
NotificationCompat.Action action = new NotificationCompat.Action.Builder(android.R.drawable.ic_dialog_email, "Bookmark", replyPendingIntent).addRemoteInput(remoteInput).build();
NotificationCompat.Action actionQuick = new NotificationCompat.Action.Builder(android.R.drawable.ic_dialog_email, "Quick", replyPendingIntent).build();
builder.addAction(action);
builder.addAction(actionQuick);
}
startForeground(NOTIFICATION_ID, builder.build());
The problem is that once the user sends a message and the Broadcast receiver is called, the notification stays in the loading state like this:
How can I remove the loading message, without having to discard the notification?
Per the Notifications in Android N blog post:
After you’ve processed the text, you must update the notification by calling notify() with the same id and tag (if used). This is the trigger which hides the Direct Reply UI and should be used as a technique to confirm to the user that their reply was received and processed correctly.
Also note:
For most templates, this should involve using the new setRemoteInputHistory() method which appends the reply to the bottom of the notification.
This ensures that users see the text they entered was properly handled.

android system notification limit per app

This may be off topic , but I couldn't found anything for it.
Is there any limit on the number of notifications android app can display?I am facing issue after 100 notifications. There is no documentation which states this clearly.
Note: This is not really a good idea to show 100 notifications but It is required for certain reasons.
According to #Nirel's answer.
1) I tried to run the code in 3 different devices.
Surprisingly notifications beyond 50 are not showing in notification area.
It gives following error.
W/NotificationManagerďą• notify: id corrupted: sent 51, got back 0
The same error comes for subsequent calls.
I saw the source of NotificationManager , it gives this error if incoming and out id is not same. See below code.
https://github.com/android/platform_frameworks_base/blob/master/core/java/android/app/NotificationManager.java#L233
2) After I tried to notify on intervals of 100 milliseconds.
It also Gives the same error. What I tried is removed 1 notification when code is executed.
Surprisingly , notification number 153 came in status bar.
So the conclusion is that , at most 50 notifications can be there. This may be default behaviour and may can change by manufacturer as said by #Sharp Edge.
Thnx.
In API23
package com.android.server.notification;
NotificationManagerService.java
static final int MAX_PACKAGE_NOTIFICATIONS = 50;
The limit for notifications and toasts is per app 50
this post has really helped me to do research on this topic. I have written an article on this like how can you modify your logic and keep posting notifications even if you have reached the maximum limit by compromising on the oldest notifications. https://medium.com/mindorks/the-notification-limit-per-app-in-android-94af69a6862c
The notification limit dropped from 50 to 24 per appin the Android 10 notification drawer.
Read more about it here.
run this:
// prepare intent which is triggered if the
// notification is selected
Intent intent = new Intent(this, NotificationReceiver.class);
// use System.currentTimeMillis() to have a unique ID for the pending intent
PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);
// build notification
// the addAction re-use the same intent to keep the example short
Notification n = new Notification.Builder(this)
.setContentTitle("New mail from " + "test#gmail.com")
.setContentText("Subject")
.setSmallIcon(R.drawable.icon)
.setContentIntent(pIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
for(int i = 0;i<1000;i++)
{
Log.d("Tag", "notification number" + i "just published")
notificationManager.notify(i, n);
}
when the application will crash you will see how much notification you have..

Android push notification opening a custom page

my question for you is the following: I have a web app written in HTML5, wrapped as a native Android app in order to use Google Push Notifications. Because my app is using many notifications for different reasons, I want to be able to say each time a notification is received, which page to be open, like adding a 'href' in the notification intent. Is this possible?
If I wasn't clear enough please let me know.
Thanks
You can define your own notification message content. The Message builder from Google supports key value pairs to be set by the sender of the notification.
See http://developer.android.com/reference/com/google/android/gcm/server/Message.html
Example:
Message message = new Message.Builder()
.addData("link1", "http://mypage1.com")
.addData("link2", "http://mypage2.com")
.build();
When you create the notification, use setContentIntent() to attach an Intent that has been constructed to visit the right webpage:
// assuming <this> is an Activity or other Context
Intent urlIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(yourUrl));
PendingIntent urlPendingIntent = PendingIntent.getActivity(this, 0 urlIntent, 0);
Notification.Builder b = new Notification.Builder(this)
.setSmallIcon(...).setContentTitle(...).setContentText(...) // etc.
.setContentIntent(urlPendingIntent);
NotificationManager noMan
= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
noMan.notify(ID, b.build());
If you expect to have more than one of these in the notification panel at a time:
Reconsider. It's spammy to post more than one notification.
If you must, you'll need a separate ID (or separate tag) for each.

how to send push Notifications to other device in android?

Using this code, i am able to send a notification to my own device.
Intent intent = new Intent(getApplicationContext(), ContactDonor.class);
PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
//display text
String body = "Please Click on this to accept!";
String title = bloodgroup+" Required";
Notification n = new Notification(R.drawable.ic_launcher, body , System.currentTimeMillis());
n.setLatestEventInfo(getApplicationContext(), title, body, pi);
n.defaults = Notification.DEFAULT_ALL;
nm.notify(uniqueID, n);
finish();
But now i have a screen where a person's details are displayed like:
Name: ...
email: ...
, and there is a message box and a Request Button , on the click of that button, he should receive a notification with that particular message. How can this particular thing be done?
It cannot be implemented by using PUSH notifications. PUSH notification is useful when there is server-client comm is implemented where server notifies the client about the event that has occurred on server side.
What you are trying to implement indirectly is server-client architecture, where your device will act as server. If you mould your current architecture to server-client you will be able to send notification to other device. In this case also, you don't need PUSH notifications, it will be a simple server-client communication.
For more info on PUSH please see: http://www.vogella.com/articles/AndroidCloudToDeviceMessaging/article.html#c2dm_sendmessage
You can also send SMS, but that will not solve your problem. According to me, there is no other solution that you can apply to send notification.

Categories

Resources