I have the following code, that's suppose to initialize a new activity during the notification, this is located in a service class
Intent push = new Intent();
push.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
push.setClass( context, MyActivity.class );
PendingIntent pi = PendingIntent.getActivity( context, 0, push, PendingIntent.FLAG_ONE_SHOT );
long[] vibraPattern = {0, 500, 250, 500 };
Notification noti = new Notification.Builder( getApplicationContext() )
.setVibrate( vibraPattern )
.setDefaults( Notification.DEFAULT_SOUND )
.setFullScreenIntent( pi , true )
.setContentIntent( pi )
.getNotification();
notifMng.notify( 0 , noti );
The sound and the vibration go nicely, so the noti is successfully notified, however MyActivity is never created, even while it is the FullScreenIntent of this notification.
Notifications are tricky with the API changing so much between different versions. Your solution is targeting API level 11+ (3.0.x) and does not work on any 2.x devices. The getNotification() method is also deprecated in 4.1...
Your notification is missing content to show. It will actually launch the activity when the notification is received but does not display notification because it does not have any content to display.
Add .setFullScreenIntent( pi , true ) if you want to launch an activity immediately upon receiving the push.
I have modified your code so that it works:
Intent push = new Intent();
push.setClass(context, MyActivity.class);
PendingIntent pi = PendingIntent.getActivity(context, 0, push,
PendingIntent.FLAG_ONE_SHOT);
long[] vibraPattern = { 0, 500, 250, 500 };
Notification noti = new Notification.Builder(getApplicationContext())
.setVibrate(vibraPattern)
.setDefaults(Notification.DEFAULT_SOUND)
.setContentIntent(pi)
.setContentTitle("Title")
.setContentText("content text")
.setSmallIcon(R.drawable.ic_launcher)
.getNotification();
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, noti);
Notification.Builder requires API 11+. Use NotificationCompat.Builder instead, which is supported only requires API 4+, and allows for the implementation of new features (i.e. new notification styles available in Jelly Bean) on supported devices, while older devices just ignore the new features. It makes everything much smoother.
See this link for more details: http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html
you need following three variables to pass:
the color
if turning on the led is on
if turning off the led is on
if you enable 2 and 3 your led will be blinking, if you disable 2 and 3 the led will be turned off
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context );
mBuilder.setLights(Color.RED, 1, 1); // will blink
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 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);
I'm creating notification like this:
NotificationManager notifyManager = (NotificationManager) App
.getContext().getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(
App.getContext());
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setContentTitle(task.getFileNode().getName());
Intent cancelIntent = CancelDownloadReceiver
.getCancelDownloadIntent(ticketUUID);
PendingIntent intent = PendingIntent.getBroadcast(
App.getContext(), 0, cancelIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.addAction(
R.drawable.ic_stat_cancel,
App.getContext().getString(
R.string.notification_download_cancel),
intent);
builder.setProgress(0, 0, true);
Notification notification = builder.build();
notification.flags = Notification.FLAG_ONGOING_EVENT;
notifyManager.notify(ticketUUID, 1, notification);
The issue is that on Motorola XOOM with Android 4.0.3 action is missing. Why? As you can see I'm using classes from support library.
Even though this is in the support library, it won't work on Android 4.0.x. See documentation: "Action buttons won't appear on platforms prior to Android 4.1".
I want to display action buttons in notification bar. Notification works but I cannot see any button . I have read Android Developer document and different examples on the Internet but I could not find any big difference with my code. I have done like following:
public void showNotification(Context context) {
NotificationManager mNotifyMgr = (NotificationManager);
context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder = new NotificationCompat.Builder(context);
Intent prevIntent = new Intent(mContext, PlayAudioService.class);
prevIntent.setAction(ACTION_PREV);
PendingIntent piPrev = PendingIntent.getService(mContext, 0, prevIntent, 0);
Intent playIntent = new Intent(mContext, PlayAudioService.class);
playIntent.setAction(ACTION_PLAY);
PendingIntent piPlay = PendingIntent.getService(mContext, 0, playIntent, 0);
Intent nextIntet = new Intent(mContext, PlayAudioService.class);
nextIntet.setAction(ACTION_NEXT);
PendingIntent piNext = PendingIntent.getService(mContext, 0, nextIntet, 0);
mBuilder.setSmallIcon(smallIcon)
.setContentTitle("Title")
.setContentText("Text")
.setTicker("Ticker")
.setWhen(0)
//.setStyle(new NotificationCompat.BigTextStyle().bigText(title))
.addAction (R.drawable.ic_previous, "Prev", piPrev)
.addAction (R.drawable.ic_play, "Play", piPlay)
.addAction (R.drawable.ic_next, "Next", piNext);
Intent notifyIntent = new Intent(context, MainActivity.class);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent piNotify =
PendingIntent.getActivity(
mContext,
0,
notifyIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(piNotify);
mNotifyMgr.notify(mNotificationId, mBuilder.build());
}
In activity:
showNotification(this);
My Android target SDK version 15 and I am using latest version of support library v4. What am I missing and did not understand properly?
Your answer would be appreciated.
Notification Actions were introduced in API 16.
This means that you should be setting your target SDK to 16 or above.
However, only Jelly Bean and above can take advantage of the Notification Actions - make sure to test this feature on those platforms.
NotificationCompat.Builder takes care of generating a Notification compatible with whatever API it is running on, so keep using it if you are going to be supporting Ice Cream Sandwich and older. If not, simply using Notification.Builder will suffice (after changing the target SDK of course).
I have tested the new BigTextStyle Notification which display correctly. However when I add action to the notification, it never get cancel and the pull-down notification area is never close when click on the action. What did I do wrong?
Drawable d = getResources().getDrawable(android.R.drawable.ic_menu_add);
Bitmap b = ((BitmapDrawable)d).getBitmap();
Notification noti = null;
Intent notificationIntent = new Intent(MainActivity.this, MainActivity.class);
String s = "Name: John \n"
+ "Surname: Doe\n"
+ "Vehicle: Honda Jazz TA-1234\n"
+ "Note: Good drifter";
Builder builder = new Notification.Builder(MainActivity.this)
.setContentTitle("New challenger arrived!")
.setContentText(s)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(b)
.setTicker("New challenger arrived!")
.setPriority(Notification.PRIORITY_HIGH)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setNumber(5)
.addAction(
android.R.drawable.ic_menu_add,
"Accept",
PendingIntent.getActivity(getApplicationContext(), 0,
notificationIntent, 0, null))
.addAction(
android.R.drawable.ic_menu_delete,
"Refuse",
PendingIntent.getActivity(getApplicationContext(), 0,
notificationIntent, 0, null))
.addAction(
android.R.drawable.ic_dialog_map,
"Map",
PendingIntent.getActivity(getApplicationContext(), 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT, null));
noti = new Notification.BigTextStyle(builder)
.setBigContentTitle("New challenger arrived!")
.bigText(s)
.setSummaryText("You will never win against me").build();
NotificationManager nm = (NotificationManager) MainActivity.this
.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(0, noti);
I think you have to manually cancel the notifications if you take an action. This can be done by calling
notifMgr.cancel(0);
Clicking the notification itself should dismiss it, but for some reason actions don't seem to. The reason you pass 0 as the argument is becasue that's the ID you gave when you called:
notifMgr.notify(0, noti);
That's my updated answer, however, I had previously answered this:
It looks like you're trying to launch the same activity (from your PendingIntent) that you currently have displayed (I'm assuming this is a 1-activity app, with MainActivity containing the code above). Create another activity, build your PendingIntent so it launches that activity, then it should do as you wish. Alternatively, run your activity, notification appears, navigate to homescreen, or different app, then act on your Activity, your desired behavior should manifest.