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.
Related
We have code similar to the following in our app
val pendingIntent = PendingIntent.getActivity(ctx, id.toInt(), intent, PendingIntent.FLAG_CANCEL_CURRENT)
val builder = NotificationCompat.Builder(ctx, Channel.TEST_CHANNEL.channelId)
builder.setTicker(tickerText)
.setContentTitle(contentTitle)
.setContentText(contentText)
.setVibrate(vibrate)
.setSmallIcon(icon)
.setAutoCancel(true)
.setLights(-0xff0100, 300, 1000)
.setSound(uri)
.setContentIntent(pendingIntent)
.setStyle(NotificationCompat.BigTextStyle().bigText(contentText))
.addAction(R.drawable.ic_notification, ctx.getString(R.string.notification), piAction)
val notification = builder.build()
val nf = ctx.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
nf.notify(NOTIFICATION_TAG, id.toInt(), notification)
}
Starting recently we noticed that notifications on some device running Android 8+ started disappearing briefly after being shown, without user's interaction. Setting auto-cancel to false helps, but the user experience degrades.
The id is a unique item id from the database. This may be important thing to note - technically we can have a notification with such id be shown, removed/canceleld by user, and later some time used again for a similar notification with the same id. Can this be the reason?
We've updated the support libs and tried the following method on builder for luck:
builder.setTicker(tickerText)
...
.setTimeoutAfter(-1)
...
Setting this param to a positive value delayed the notification disappearing by that amount of time (so it did affect). Thus we tried a negative number, the notifications seem to stay there now.
I couldn't find any reasonable documentation explaining this, so this answer is not 100%, but keeping it here for now for others to try and see if it helps them.
Disable your application from auto optimize from battery optimization setting in android OREO. Notification will stay as long as you want
Only thing I found uncertain is NotificationCompat.Builder
Android oreo now uses Notification.Builder instead of NotificationCompat.Builder.
Might be you have to check android version like:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//Use Notification.Builder
} else {
// Use NotificationCompat.Builder.
}
I don't think unique id will be an issue for disappearing notification.
Google has created open source sample for this new changes. Please refer to it for more info.
https://github.com/googlesamples/android-NotificationChannels
.setAutoCancel(false)
May be it will work for you.
I'm working on some Android code, and I wan't to build a MediaStyle Notification. I'm already using AppCompat for most of m mediaplayer and mediasession, and what I don't already use I'm planning on switching over just so I can keep 4.x compatibility.
Issue? Well, I'm trying to make my MediaStyle notification, and give it a MediaSession Token. My support.v4.media.session.MediaSession.Token doesn't seem to be compatable with media.session.MediaSession.Token
I've tried casting, and just leaving it raw. I'm honestly confused because the docs say they're compatible.
If you want the rest of the code, the code can be found here
Or you can look at the relevant code here.
Intent nIntent = new Intent(context, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, nIntent, 0);
n.setLatestEventInfo(context, notifTitle, notifMessage, pIntent);
notificationManager.notify(notifId, n);
ComponentName c = new ComponentName("com.thefan.android", "BackgroundService");
ms = new MediaSessionCompat(this, "TheFan", c, pIntent);
ms.setMetadata(new MediaMetadataCompat.Builder()
.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, artwork)
.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, "Pink Floyd")
.putString(MediaMetadataCompat.METADATA_KEY_ALBUM, "Dark Side of the Moon")
.putString(MediaMetadataCompat.METADATA_KEY_TITLE, "The Great Gig in the Sky")
.build());
// Indicate you're ready to receive media commands
ms.setActive(true);
// Attach a new Callback to receive MediaSession updates
ms.setCallback(new MediaSessionCompat.Callback() {
// Implement your callbacks
});
// Indicate you want to receive transport controls via your Callback
ms.setFlags(MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);
// Create a new Notification
final Notification noti = new Notification.Builder(this)
// Hide the timestamp
.setShowWhen(false)
// Set the Notification style
.setStyle(new Notification.MediaStyle()
// Attach our MediaSession token
.setMediaSession(ms.getSessionToken())
// Show our playback controls in the compat view
.setShowActionsInCompactView(0, 1, 2))
// Set the Notification color
.setColor(0xFFDB4437)
// Set the large and small icons
.setLargeIcon(artwork)
.setSmallIcon(R.drawable.your_small_icon)
// Set Notification content information
.setContentText("Pink Floyd")
.setContentInfo("Dark Side of the Moon")
.setContentTitle("The Great Gig in the Sky")
// Add some playback controls
.addAction(R.drawable.your_prev_icon, "prev", retreivePlaybackAction(3))
.addAction(R.drawable.your_pause_icon, "pause", retreivePlaybackAction(1))
.addAction(R.drawable.your_next_icon, "next", retreivePlaybackAction(2))
.build();
Magical. There's a Token.getToken(); You need to use that.
Then again, MediaStyle Notifications are only API 21 compatible, so good luck.
For whom this may be helpful.
Firstly you need import v4 MediaSessionCompat and comment the general MediaSession like this:
//import android.media.session.MediaSession;
import android.support.v4.media.session.MediaSessionCompat;
In your code, you need use MediaSessonCompat like this:
MediaSessionCompat mediaSession = new MediaSessionCompat(getApplicationContext(), "session tag");
MediaSessionCompat.Token token = mediaSession.getSessionToken();
mediaStyle.setMediaSession(token);
As others have pointed out, the underlying MediaSession.Token can be obtained through MediaSessionCompat.getToken(), and its type can safely be cast. In Kotlin:
val mediaStyle = Notification.MediaStyle()
.setMediaSession(mediaSessionCompat.sessionToken.token as MediaSession.Token?)
It is posible.
Check your imports, maybe you are importing bad versions.
MediaStyle should android.support.v7.app.NotificationCompat.MediaStyle
NotificationBuilder should be android.support.v7.app.NotificationCompat.Builder
Notification should be compat android.support.v4.app.NotificationCompat
If you are going to support older version than 21 you need to use Compat classes (ALL compat classes instead of "normal" ones).
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
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.
Before Notification.Builder came into existence the way to update a notification that was already in the notification tray was to call setLatestEventInfo() and then send the notification back through the NotificationManager.notify() call with an ID that matches the first notify() call you made.
Now setLatestEventInfo() is deprecated with the message: Use Notification.Builder instead. But I cannot find any documentation about how to properly update a notification using Notification.Builder.
Are you just suppose to recreate a new Notification instance every time you need to update the notification? Then simply pass that to NotificationManager.notify() with the ID you used before?
It seems to work but I wanted to see if anyone had any official verification that this is the new "way to do this"?
There real reason I am asking this is because in Android 4.1.1 Jelly Bean, the notification now flashes everytime notify() is called. When updating a progress bar with setProgress() this looks really bad and makes it hard to tap on the notification. This was not the case in 4.1 or previous versions. So I want to make sure I am doing this correctly before I file a bug.
I resolved this issue by calling setWhen(0) on my Notification.Builder. It seems Android's default value for this argument doesn't suit updating bits of the notification view without the entire notification fading out / in.
Notification.Builder builder = new Notification.Builder(c)
.setContentTitle("Notification Title")
.setSmallIcon(R.drawable.ic_notification_icon)
.setProgress(max_progress,current_progress,false)
.setWhen(0);
notification = builder.getNotification();
mNotificationManager.notify(NOTIFICATION_ID, notification);
Update:
As WolframRittmeyer stated, using when=0 is not an elegant way. I formed a solution like following:
if(mNotif == null) {
//either setting mNotif first time
//or was lost when app went to background/low memory
mNotif = createNewNotification();
}
else {
long oldWhen = mNotif.when;
mNotif = createNewNotification();
mNotif.when = oldWhen;
}
mNotificationManager.notify(NOTIFICATION_ID, mNotif);
What you are doing is correct, you're just missing the flags you can set. I don't know your particular notification implementation but you might consider using:
setOngoing(boolean ongoing)
or
setOnlyAlertOnce(boolean onlyAlertOnce)
I'm guessing (since I had the same trouble just now) that you are using a RemoteView in your notification. I managed to update the notification without it flashing like this:
RemoteViews views;
if( this.mNotification == null) {
views = new RemoteViews(getPackageName(), R.layout.notification);
this.mNotification = new Notification.Builder(this)
.setContent(views)
.setSmallIcon(R.drawable.status_icon)
.setContentIntent(mNotificationAction)
.setOngoing(true)
.setOnlyAlertOnce(true)
.getNotification();
} else {
views = this.mNotification.contentView;
}
Thanks to #seanmonstar for answering Refresh progress bar in notification bar.
The solution described here works well: Updating an ongoing notification quietly
The key is to use to reuse the builder and setOnlyAlertOnce(true):
if (firstTime) {
mBuilder.setSmallIcon(R.drawable.icon)
.setContentTitle("My Notification")
.setOnlyAlertOnce(true);
firstTime = false;
}
mBuilder.setContentText(message)
.setProgress(100, progress, true);
mNotificationManager.notify(mNotificationId, mBuilder.build());