guys.
I've been debugging this for days, but I can't find an answer for this.
As the title says, I am using BroadcastReceiver to make my notification.
Everything was fine until I add .addAction.
It needs PendingIntent for default and I made a PendingIntent.
But the problem is this; when I add .addAction, It says this
Incompatiable types.
Required : android.app.Notification
Found : android.app.Notification.Builder
the code works perfectly fine with out the .addAction and the PendingIntent.
What could be the problem?
Here is my code,
BroadcastReceiver, WifiService.java
Intent intent = new Intent(mcontext, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pIntent = PendingIntent.getActivity(mcontext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification noti = new Notification.Builder(mcontext)
.setContentTitle("Welcome home!")
.setContentText("You have a to-do; " + hometodo)
.setSmallIcon(R.drawable.home_light)
.setLargeIcon(icon)
.setLights(0xFFFFD800, 5000, 0)
.addAction(R.drawable.archive_notification, "Mark as done", pIntent);
//.addAction gets the error.
.build();
NotificationManager notificationManager = (NotificationManager) mcontext.getSystemService(mcontext.NOTIFICATION_SERVICE);
notificationManager.notify(0, noti);
used NotificationCompat for setting .addAction(...).
Check official docs
action buttons won't appear on platforms prior to Android 4.1. Action
buttons depend on expanded notifications, which are only available in
Android 4.1 and later.
Related
I am starting stickynotification from my service using startForeground. Notification does show up however my settings, like title or intent to show up when clicked does not take effect.
#Override
public void onCreate()
{
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
Notification notification = new NotificationCompat.Builder(this)
//.setSmallIcon(R.mipmap.app_icon)
.setContentTitle("My Awesome App")
.setContentText("Doing some work...")
.setContentInfo("Server is running")
.setContentIntent(pendingIntent).build();
startForeground(1337, notification);
super.onCreate();
}
Notification shows app name and touch for more info or to stop....
When clicked it takes me to app info where i can forestop or uninstall it.
If anybody out there in this universe having same problem i suggest do not overclock your brain else you will endup missing small things like requesting permissions.
Setting up icon fixed my issue.
The problem is: in my code im sending 4 notification. But only one top notifacation opens my Activity another 3 do nothing on click.
Here is my code to send Notificaiton. I do it 4 times in my program.
The Notification ID is always diferrent.
Intent notificationIntent = new Intent(context, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context,
0, notificationIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.element)
.setContentTitle(getResources().getString(R.string.app_name))
.setContentText(messages.get(messageCount).toString());
Notification notification = builder.build();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(messageCount, notification);
Please Help . Thanks!
The problem is that android "eat" the same intents from pending intent.
You need to make them different using different extra and/or request codes
I'm trying to create a notification, without using the NotificationCompat.Builder. The motivation is not using the Support Library if its not really needed (and also for educational purposes)
I assume that its possible, as there are "native" Notification and NotificationManager classes that are exposed to the users.
My try goes as follow:
Intent i = new Intent();
i.setClassName("com.test", "com.test.SomeActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
PendingIntent pi = PendingIntent.getService(this, 0, i, 0);
notification.setLatestEventInfo(this, title, content,pi);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0 , notification);
This code just run, but no notification appears, and notificationManager.notify() in general returns void.
I'm trying to use the code google implemented but it seems I'm missing something. Here's the code
int notificationId = 001;
// Build intent for notification content
Intent viewIntent = new Intent(this, ViewEventActivity.class);
viewIntent.putExtra(EXTRA_EVENT_ID, eventId);
PendingIntent viewPendingIntent =
PendingIntent.getActivity(this, 0, viewIntent, 0);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_event)
.setContentTitle(eventTitle)
.setContentText(eventLocation)
.setContentIntent(viewPendingIntent);
// Get an instance of the NotificationManager service
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(this);
// Build the notification and issues it with notification manager.
notificationManager.notify(notificationId, notificationBuilder.build());
When I run this, i see that my device obtains the notification, but my ViewEventActivity class is not activated when i click to the notification. I tried to add FLAG_ACTIVITY_NEW_TASK flag to my viewIntent (it was a tip in the definition of getActivity() method) but no luck there. Any help would be appreciated.
My problem was that i missed the declaration of my activity ViewEventActivity in manifest.
<activity
android:name=".ViewEventActivity" />
It's odd that application did not crash when the reference ViewEventActivity.class was not found.
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).