I have an app that kicks off a notification on a Wear device (the notification is created and shown on the Wear device).
What I want to do is have the "first" notification page display some high level info (app name, scroll to see more, etc...), and then have a list of pages containing content after that.
That works fine. The issue is that I want to attach an Action to each page (to kick off a PendingIntent). However, no matter what I try, I can't get the page to perform the Action.
I've tried:
setContentIntent
addAction
addAction and extend(new Notification.WearableExtender().setContentAction(0))
Anyone have any ideas?
I'm using Notification, not NotificationCompat, but I don't think that should make a difference.
UPDATE: I'm creating the notification on the watch. Here is the code I use:
private void createNotifications(ArrayList<Thing> things) {
DataApi data = Wearable.DataApi;
int notificationId = 0;
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification.Builder mainBuilder = new Notification.Builder(this)
.setSmallIcon(R.drawable.default_icon)
.setContentTitle("Things")
.setContentText("Swipe to see your things")
.setPriority(Notification.PRIORITY_DEFAULT);
List<Notification> pages = new ArrayList<>(things.size());
for(Thing thing : things) {
pages.add(createNotificationPageForThing(data, thing, notificationId).build());
notificationId++;
}
Notification n = new Notification.WearableExtender().addPages(pages).extend(mainBuilder).build();
nm.notify(notificationId, n);
}
private Notification.Builder createNotificationPageForThing(DataApi data, Thing thing, int notificationId) {
Asset bitmapAsset = getBitmapAsset(data, contact);
Intent thingIntent = new Intent(this, WearDetailActivity.class);
thingIntent.putExtra(WearDetailActivity.DETAIL_EXTRA, thing);
PendingIntent thingPendingIntent = PendingIntent.getActivity(this, notificationId, thingIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification.Action action = new Notification.Action(R.drawable.ic_action_social_person, "More details", thingPendingIntent);
Notification.Builder builder = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_action_social_person)
.setContentTitle(thing.getDisplayName())
.setPriority(Notification.PRIORITY_DEFAULT)
.addAction(action)
.extend(new Notification.WearableExtender().setContentAction(0));
if(bitmapAsset != null) {
try {
Bitmap b = BitmapFactory.decodeStream(
data.getFdForAsset(connection.getClient(), bitmapAsset).await().getInputStream());
builder.setLargeIcon(b);
} catch (Throwable ignore) {}
}
return builder;
}
#Eliezer, reading at the following line I did not understand what exactly is the behaviour you're experiencing ... explaining it in details might be helpful to debug your problem.
However, no matter what I try, I can't get the page to perform the Action.
Using .addAction() should work in your case. You'd want to use WearableExtender if you were creating the notification from the device, not the watch itself. Just to confirm something obvious - you're passing the .addAction a PendingIntent right?
Here's a code snippet from one of my applications, which accomplishes exactly what you're aiming for - I have just "Swipe for actions" text in the first page, and the next 2 pages perform different actions.
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
Intent openIntent = new Intent(this, CommonWearIntentService.class);
openIntent.setAction(CommonWearIntentService.ACTION_OPEN_ON_PHONE);
PendingIntent openPendingIntent = PendingIntent.getService(this, 0, openIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent addTransactionIntent = new Intent(this, CommonWearIntentService.class);
addTransactionIntent.setAction(CommonWearIntentService.ACTION_ADD_TRANSACTION);
PendingIntent addTransactionPendingIntent = PendingIntent.getService(this, 0, addTransactionIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(getString(R.string.app_name))
.setContentText(getString(R.string.swipe_for_actions))
.addAction(R.drawable.add_transaction, getString(R.string.add_transaction), addTransactionPendingIntent)
.addAction(R.drawable.common_full_open_on_phone, getString(R.string.common_open_on_phone), openPendingIntent)
.setVibrate(new long[]{500, 500, 500, 1000})
.setLights(Color.BLUE, 3000, 3000)
.setOngoing(true);
notificationManager.notify(ONGOING_NOTIFICATION_ID, builder.build());
Hope this helps!
Per the documentation, you must use NotificationCompat. After you switch to that setContentIntent() looks to be the right way to set the PendingIntent.
Example from the linked docs:
// Create builder for the main notification
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.new_message)
.setContentTitle("Page 1")
.setContentText("Short message")
.setContentIntent(viewPendingIntent);
// Create a big text style for the second page
BigTextStyle secondPageStyle = new NotificationCompat.BigTextStyle();
secondPageStyle.setBigContentTitle("Page 2")
.bigText("A lot of text...");
// Create second page notification
Notification secondPageNotification =
new NotificationCompat.Builder(this)
.setStyle(secondPageStyle)
.build();
// Add second page with wearable extender and extend the main notification
Notification twoPageNotification =
new WearableExtender()
.addPage(secondPageNotification)
.extend(notificationBuilder)
.build();
// Issue the notification
notificationManager =
NotificationManagerCompat.from(this);
notificationManager.notify(notificationId, twoPageNotification);
Related
This is my code so far on understanding notification.
Intent mIntent = new Intent(this, typeof(MainActivity));
Intent gIntent = new Intent(this, typeof(GameMenu));
Intent sIntent = new Intent(this, typeof(SceneMenu));
PendingIntent mainIntent = PendingIntent.GetActivity(this, 0, mIntent, 0);
PendingIntent gameIntent = PendingIntent.GetActivity(this, 0, gIntent, 0);
PendingIntent sceneIntent = PendingIntent.GetActivity(this, 0, sIntent, 0);
var notifSound = RingtoneManager.GetDefaultUri(RingtoneType.Notification);
//long[] pattern = new long[] { 1000, 500, 1000, 2000, 3000, 500, 500, 1000};
Notification n = new Notification.Builder(this)
.SetContentTitle("RBOS Notification")
.SetContentText("You won $1million")
.SetColor(Color.LightGreen)
//.SetVibrate(pattern)
.SetPriority((int)NotificationPriority.Max)
.SetShowWhen(true)
.SetSound(notifSound)
//.SetTicker(DateTime.Now.ToString("hh:mmtt"))
.SetSmallIcon(Resource.Drawable.ic_fb)
.SetContentIntent(mainIntent)
.SetAutoCancel(false)
.SetLights(Color.Green, 1000, 1000)
.AddAction(Resource.Drawable.ic_main, "Main", mainIntent)
.AddAction(Resource.Drawable.ic_play, "Game", gameIntent)
.AddAction(Resource.Drawable.ic_scene, "Scene", sceneIntent).Build();
NotificationManager notificationManager =
(NotificationManager)GetSystemService(NotificationService);
notificationManager.Notify(0, n);
I am a bit confused on how to set the icon itself for notification rather than words. target api is jellybean to higher.
on my phone Asus with nougat OS, it only display words. In other phone Starmobile and OPPO, it display the icons but with only part of words. I want to display something like radio notif or vlc player notif with previous play/pause next icon buttons alone (without its words).
And for side question. What is the use of ticker? I can't see any changes when I put it there. Also in OPPO, why notification didn't appear instantly (like when battery is low) even priority set in max
Standard notifications will appear different on the various APIs (and even different among the same API level but different OEMs).
It seems you want to create a RemoteViews and use it as your notification's CustomContentView (like a media player in the notification drawer). That way you can override the default notification template using your own layout and content.
Example:
var remoteView = new RemoteViews(PackageName, Resource.Layout.Alarm);
remoteView.SetInt(Resource.Id.myButton, "setText", Resource.String.rocks);
var notification = new Notification.Builder(this)
.SetSmallIcon(Android.Resource.Drawable.IcDialogInfo)
.SetCustomContentView(remoteView)
.Build();
The Resource.Layout.Alarm layout contains just one Button, thus the notification is just one button across the notification drawer:
I want to show a simple notification on android wear then i want to give a action on tap on notification,i don't want to show any paging in notification.
I seen many examples but all are of paging, i just want to give a simple notification and want to handle on tap event.
You have to change my resources to yours. setContentAction(0) is what you generally need.
public void showNotification() {
Intent eventIntent = new Intent(context, GridCardsActivity.class);
PendingIntent openAppIntent = PendingIntent.getActivity(context, 1, eventIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification.Action openAction = new Notification.Action(R.drawable.logo_sm, "open", openAppIntent);
Notification notification = new Notification.Builder(context)
.setContentTitle(pulse.getName())
.setContentText(pulse.getQuestion())
.setSmallIcon(R.drawable.logo)
.extend(new Notification.WearableExtender()
.addAction(openAction)
.setContentAction(0)
.setHintHideIcon(true)
.setBackground(bm)
)
.build();
((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE))
.notify((int) System.currentTimeMillis(), notification);
}
I'm following Google's instructions here https://developer.android.com/training/wearables/notifications/creating.html
However, unsurprisingly, their code doesn't work. Specifically I'm trying to do this:
// Build the notification and add the action via WearableExtender
Notification notification =
new Notification.Builder(context)
.setSmallIcon(R.drawable.buzz_icon)
.setContentTitle("Buzz")
.setContentText("OpenBuzz")
.extend(new Notification.WearableExtender().addAction(action))
.build();
I want an action specific to the Wearable, so I have no choice but to use Notification.WearableExtender(). But it's addAction method only accepts an action as it's parameter. Here is my code for creating an action:
Notification.Action action =
Notification.Action.Builder(R.drawable.buzz_icon, actionIntent.toString(), pendingIntent);
Which doesn't work, as Android Studio says "Method call expected"
How can I successfully create a Notification.Action?
Or how else might I add a Wearable specific action to my notification?
You're on the right track.
You need to create a new NotificationCompat.Action.Builder and then call build() on it. Like this:
NotificationCompat.Action action =
new NotificationCompat.Action.Builder(android.R.drawable.ic_menu_call, "Only in wearable", pendingIntent)
.build();
Also, make sure that the action is defined as NotificationCompat.Action, not Notification.Action.
This example illustrates how to add a notification with an action (e.g. open the main activity).
NotificationCompat.Builde mBuilder = new NotificationCompat.Builder(this, null);
Intent myIntent = new Intent(this, MainActivity.class);
PendingIntent myPendingIntent = PendingIntent.getActivity(this, 0, myIntent, 0);
mBuilder.setContentTitle("Your_title")
.setContentText("Some_text")
.setSmallIcon(R.drawable.app_icon)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setOngoing(true)
.addAction(R.drawable.open_icon, "Open", myPendingIntent)
.setAutoCancel(false);
NotificationManager mNotifyManager = (NotificationManager) getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
mNotifyManager.notify(1, mBuilder.build());
I followed this Sample Code
In Big Text Notifications section, he said that need expand to see Big text notification form, as image im below :
I wonder that we can not set Expanded Notification as default in Big Text Notifications?
People who know it is can or not,
If can,
Please tell me how to do it,
Thanks,
The documentation states:
A notification's big view appears only when the notification is
expanded, which happens when the notification is at the top of the
notification drawer, or when the user expands the notification with a
gesture.
So my answer is no, you can't expand it by default.
There is however a trick to push the notification to the top of the list where it would be expanded. Simply set the Priority to Notification.PRIORITY_MAX and chances are that your app's notification will make it to the top.
Notification noti = new Notification.Builder()
... // The same notification properties as the others
.setStyle(new Notification.BigPictureStyle().bigPicture(mBitmap))
.build();
You change
.setStyle(new NotificationCompat.BigTextStyle().bigText(th_alert))
along with the announcement
notification = new NotificationCompat.Builder(context)
Here is an example:
You can set Code
Intent intent = new Intent(context, ReserveStatusActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
intent = new Intent(String.valueOf(PushActivity.class));
intent.putExtra("message", MESSAGE);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(PushActivity.class);
stackBuilder.addNextIntent(intent);
// PendingIntent pendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
// android.support.v4.app.NotificationCompat.BigTextStyle bigStyle = new NotificationCompat.BigTextStyle();
// bigStyle.bigText((CharSequence) context);
notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(th_title)
.setContentText(th_alert)
.setAutoCancel(true)
// .setStyle(new Notification.BigTextStyle().bigText(th_alert) ตัวเก่า
// .setStyle(new NotificationCompat.BigTextStyle().bigText(th_title))
.setStyle(new NotificationCompat.BigTextStyle().bigText(th_alert))
.setContentIntent(pendingIntent)
.setNumber(++numMessages)
.build();
notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notificationManager.notify(1000, notification);
notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText("Your Long Text here"))
simply setStyle of your notification builder.
No need of doing any kind of modification in the any file for showing of multiple lines of Notification while using Push Notification V5, remove the field "style", from the object you are sending. Automatically the Multiple lines of notification will be seen. For more information, upvote the answer, ask your query. I will help you out.
For reference, visit this question
From this notification code you have got images, and big text or more.
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mBuilder.setSmallIcon(R.drawable.small_logo);
mBuilder.setColor(Color.parseColor("#D74F4F"));
} else {
mBuilder.setSmallIcon(icon);
}
mBuilder.setTicker(title).setWhen(when);
mBuilder.setAutoCancel(true);
mBuilder.setContentTitle(title);
mBuilder.setContentIntent(intent);
mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
mBuilder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), icon));
mBuilder.setContentText(msg);
mBuilder.setPriority(Notification.PRIORITY_MAX);
if (Utils.validateString(banner_path)) {
mBuilder.setStyle(notiStyle);
} else {
mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(msg));
}
Notification noti = mBuilder.build();
notificationManager.notify(0, noti);
I used below code for displaying notification in notification Bar.
it worked fine.
But i need to display notification icon dynamically that will come from web service.
How i can do?
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification note = new Notification(R.drawable.image,"status message", System.currentTimeMillis());
Intent in = new Intent(Notify.this,CommentU.class);
PendingIntent pi = PendingIntent.getActivity(Notify.this, 0, in, 0);
note.setLatestEventInfo(Notify.this,"NotificationTitle", "You have a new Commentio", pi);
note.number = ++count;
note.vibrate = new long[] { 500l, 200l, 200l, 500l };
note.flags |= Notification.FLAG_AUTO_CANCEL;
nm.notify(NOTIFY_ME_ID, note);
Thanks in Advance.
I have one suggestion for you: you have to download that image which you want to show and then you can set that as bitmap: check below code. I have created one BITMAP.
its look link :
for this you have to add android-support-v4.jar
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My notification").setLargeIcon(BITMAP)
.setContentText("Hello World!");
Intent resultIntent = new Intent(this, test.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(test.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(NOTIFY_ME_ID, mBuilder.build());
for more detail chekc this link.
Removing notifications
Notifications remain visible until one of the following happens:
The user dismisses the notification either individually or by using "Clear All" (if the notification can be cleared).
The user clicks the notification, and you called setAutoCancel() when you created the notification.
You call cancel() for a specific notification ID. This method also deletes ongoing notifications.
You call cancelAll(), which removes all of the notifications you previously issued.
Edited: just replace this.
mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My notification").setLargeIcon(BITMAP)
.setAutoCancel(true)
.setContentText("Hello World!");
just add some icons in your resource folder and then
int myIcon = R.drawable.image;
your logic goes here ....
and change value of icon according to your logic like myIcon = R.drawable.somenewimage;
and then finally set your notification
Notification note = new Notification(myIcon,"status message", System.currentTimeMillis());