error when building a notification - android

I am using this tutorial http://www.vogella.com/articles/AndroidNotifications/article.html , I want to build notification
// Prepare intent which is triggered if the
// notification is selected
Intent intent = new Intent(this, NotificationReceiver.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
// Build notification
// Actions are just fake
Notification noti = new Notification.Builder(this)
.setContentTitle("New mail from " + "test#gmail.com")
.setContentText("Subject")
.setSmallIcon(R.drawable.icon)
.setContentIntent(pIntent)
.addAction(R.drawable.icon, "Call", pIntent)
.addAction(R.drawable.icon, "More", pIntent)
.addAction(R.drawable.icon, "And more", pIntent).build();
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, noti);
I have error in the line addAction , eclipse says that the method addaction(ing, String pendingintent) is undefined for the type notification.builder

You need to set your sdkTargetVersion in the <uses-sdk> element of AndroidManifest.xml to be 16 or higher. The addAction(...) method doesn't exist for older versions of Android.

you have to use android-support-v4.jar file in yout project lib folder /lib/android-support-v4.jar and add it
your ptoject---->properites (Alt+)-->Build Path-->Library-->add jar-->select android-support-v4.jar from lib folder --> ok
your ptoject---->properites (Alt+)-->Build Path-->Order & Export--> select all--> ok.
using this your code run in <= 11 API LEVEL.
Edited:
you are getting error because below method require MIN 11 API level
.setContentTitle("My notification")
.setContentText("Hello World!");
and below method require MIN API LEVEL 16:
.addAction(R.drawable.ic_launcher, "call", pIntent)
.addAction(R.drawable.ic_launcher, "more", pIntent)
.addAction(R.drawable.ic_launcher, "add more", pIntent)
so your solution is below use this way:
Intent intent = new Intent(this, TestSettings.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.ic_launcher)
.addAction(R.drawable.ic_launcher, "call", pIntent)
.addAction(R.drawable.ic_launcher, "more", pIntent)
.addAction(R.drawable.ic_launcher, "add more", pIntent)
.setContentTitle("My notification")
.setContentText("Hello World!");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, TestSettings.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(TestSettings.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(100, mBuilder.build());
Make sure you have added android-support-v4.jar jar file

You are using a bad version of the support library. AddAction has been introduced in r13 (maybe earlier).

Related

Preview Notification on android

I'm using this code to show a notification to user from code :
Intent intent = new Intent(this, this.getClass());
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.icon)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.icon))
.setContentTitle("Notification!")
.setContentText("Hello word")
.setContentIntent(pi)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setPriority(NotificationCompat.PRIORITY_MAX);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
This is juste adding the notification in the notification screen (and also the icon of my appliaction in the status bar)
But how can I have a preview of this notification on top of screen ?

Android notification Big View - Intent on button not fired

I'm following the Android Dev tutorial to use Big View for notifications. I want an on going notification with some functionality when clicking on the body of the notification and other functionality when clicking the Button.
This is my code:
Intent bodyIntent = new Intent(context, MainActivity.class);
innerIntent.putExtra(NOTIFICATION_BODY, "Click on notification body");
PendingIntent bodyPendingIntent = PendingIntent.getActivity(context, 0, bodyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent buttonIntent = new Intent(context, MainActivity.class);
buttonIntent.setAction(NOTIFICATION_BUTTON);
PendingIntent buttonPendingIntent = PendingIntent.getService(context, 0, buttonIntent, 0);
android.support.v4.app.NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setCategory(Notification.CATEGORY_SERVICE)
.setPriority(Notification.PRIORITY_MAX)
.setVisibility(Notification.VISIBILITY_PRIVATE)
.setContentIntent(notificationPendingIntent)
.setAutoCancel(true)
.setOnlyAlertOnce(true)
.setLights(getResources().getColor(R.color.primary), 50, 10000)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setVibrate(new long[] {0, 50})
.setOngoing(true)
.setStyle(new NotificationCompat.BigTextStyle().bigText("Hello World BIG!"))
.addAction(R.mipmap.ic_image, "Button", buttonPendingIntent);
int mNotificationId = 001;
NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(mNotificationId, mBuilder.build());
Then in onResume:
Intent intent = getIntent();
Log.e("m", String.valueOf(intent.getIntExtra(NOTIFICATION_RESULT, 0)));
Log.e("m", String.valueOf(intent.getAction()));
Result:
When clicking on the body of the notification the bodyIntent fires and I get the correct log printing.
When clicking on the button: Nothing happens and MainActivity not even starting.
Thanks,
You should call PendingIntent.getActivity() method if you want to start an activity, but you are creating buttonPendingIntent by calling PendingIntent.getService().

Android notification, how to call custom function after click on button?

I have a simple notification with three buttons. How can i call custom function after button click. I red, that here should be used pending intent, but i did not find any useful example.
Here is my code:
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
// I WOULD LIKE TO CALL THIS INTENT OR CUSTOM FUNCTION
Intent phoneCallIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "123456789"));
// build notification
// the addAction re-use the same intent to keep the example short
Notification n = new Notification.Builder(context)
.setContentTitle("New mail from " + "test#gmail.com")
.setContentText("Subject")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent)
.setAutoCancel(true)
.addAction(R.drawable.ic_launcher, "Call", phoneCallIntent) //CUSTOM INTENT OR FUNCTION CALLING
.addAction(R.drawable.ic_launcher, "More", pIntent)
.addAction(R.drawable.ic_launcher, "And more", pIntent)
.build();
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, n);
How can i do it please?
Thanks for any help.
You have to use a PendingIntent. Here's how to create one from an Intent:
PendingIntent pendingPhoneCallIntent = PendingIntent.getActivity(context, REQUEST_CODE, phoneCallIntent, 0);
Then use pendingPhoneCallIntent instead of phoneCallIntent in addAction().
From the documentation of Notification.Builder setContentIntent(PendingIntent intent)
public Notification.Builder setContentIntent (PendingIntent intent) Supply a PendingIntent to be sent when the notification is clicked.
so before building the Notification you need to make a PendingIntent like this
PendingIntent pIntent = PendingIntent.getActivity(context, requestCode,phoneCallIntent,PendingIntent.FLAG_UPDATE_CURRENT);
//requestCode is Private request code for the sender like 0,1,2 etc
and then
Notification n = new Notification.Builder(context)
.setContentTitle("New mail from " + "test#gmail.com")
.setContentText("Subject")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent)
.setAutoCancel(true)
.build();
Now by doing this when you click on the notification the the pIntent PendingIntent will be sent so that phoneCallIntent Intent will performed.

v4 support lib Notification not working

i found a weird behavior with android support library v4. Notification will work only if we set the set small icon for the notification else no notification will posted to on status bar. sample code is code is posted bellow please take look. Can anyone explain why this weird behavior.
// below code will not post any notification
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
Notification n = new Builder(context.getApplicationContext())
.setContentTitle("simple notification title")
.setContentText("simple message")
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.addAction(R.drawable.ic_launcher, "Call", pIntent)
.addAction(R.drawable.ic_launcher, "More", pIntent)
.addAction(R.drawable.ic_launcher, "And more",pIntent).build();
NotificationManager notificationManager =(NotificationManager) context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, n);
// below code will post notification
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
Notification n = new Builder(context.getApplicationContext())
.setContentTitle("simple notification title")
.setContentText("simple message")
.setContentIntent(pendingIntent)
.setAutoCancel(true)
//this below one line piece of code is making difference
.setSmallIcon(R.drawable.ic_launcher)
.addAction(R.drawable.ic_launcher, "Call", pIntent)
.addAction(R.drawable.ic_launcher, "More", pIntent)
.addAction(R.drawable.ic_launcher, "And more",pIntent).build();
NotificationManager notificationManager =(NotificationManager) context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, n);
The documentation at http://developer.android.com/guide/topics/ui/notifiers/notifications.html clearly states which notification content is required and which is optional.
a small icon is required.

Notification for android giving error for Notification class

I am creating a small app for android for notification.
But it gives an Error in Notification class error (API level supported for 11 or 16).
Then I tried using NotificationCompat class but it shows resources can not be resolved to a type while I import package import android.support.v4.app.NotificationCompat.Builder;
In other words, if I use Notification class then it give API level Error, and if I use NotificationCompat then it gives that resource error. How can I resolve both these errors?
public void createNotification(View view) {
// Prepare intent which is triggered if the
// notification is selected
Intent intent = new Intent(this, NotificationReceiverActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
// Context context=new Context();
// Build notification
// Actions are just fake
NotificationCompat.Builder noti = new NotificationCompat.Builder(this)
.setContentTitle("New mail from " + "star.ankit90#gmail.com")
.setContentText("Subject").setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent)
.addAction(R.drawable.ic_launcher, "Call", pIntent)
.addAction(R.drawable.ic_launcher, "More", pIntent)
.addAction(R.drawable.ic_launcher, "And more", pIntent).build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, noti);
}
I've had compilation errors using your code.
This fixed code compiles successfully:
// Prepare intent which is triggered if the
// notification is selected
Intent intent = new Intent(this, NotificationReceiverActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
// Build notification
// Actions are just fake
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle("New mail from " + "star.ankit90#gmail.com")
.setContentText("Subject").setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent)
.addAction(R.drawable.ic_launcher, "Call", pIntent)
.addAction(R.drawable.ic_launcher, "More", pIntent)
.addAction(R.drawable.ic_launcher, "And more", pIntent).build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Hide the notification after its selected
Notification notification = builder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
Maybe it will work for you. if not a stack trace can help.

Categories

Resources