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.
Related
I just started working with notifications and now I'm trying to remove the notification and launch the app once the notification has been tapped in the notificationcenter.
I tried to work with the following code:
import android.app.NotificationManager;
public class ExpandNotification {
private int NOTIFICATION = 546;
private NotificationManager mNM;
public void onCreate() {
mNM.cancel(NOTIFICATION);
setContentView(R.layout.activity_on);
//Toast.makeText(this, "stopped service", Toast.LENGTH_SHORT).show();
}
I think this code executes the other class when tapped?
PendingIntent contentIntent = PendingIntent.getActivity(this, REQUEST_CODE, new Intent(this, ExpandNotification.class), 0);
However the notification doesn't go away, nor does the application launch.
But I'm able to swipe it to left or right to remove it but that's not what I want..
To get the same effect using Notification.Builder or NotificationCompat.Builder call setAutoCancel(true) on the Builder instance.
Use the flag Notification.FLAG_AUTO_CANCEL
Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText, pendingIntent);
// Cancel the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;
and to launch the app:
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// Create a new intent which will be fired if you click on the notification
Intent intent = new Intent(context, App.class);
// Attach the intent to a pending intent
PendingIntent pendingIntent = PendingIntent.getActivity(context, intent_id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
This answer is too much late but specially i write following solution because notification constructor become deprecated so use notification using builder , like following :
**.setAutoCancel(true)** is used to remove notification on click
and entire notification is like follwoing :
private void makeNotification(String title,String msg){
Intent resultIntent = new Intent(this, MasterActivity.class);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setContentIntent(resultPendingIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(title)
.setAutoCancel(true)
.setContentText(msg);
int mNotificationId = 001;
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(mNotificationId, mBuilder.build());
}
Calling this method with title and message you get perfect notification.
You can directly add .setAutoCancel(true) this line into your code in order to remove notification on click.
This must be added in your builder variable.
Example:
mBuilder = new NotificationCompat.Builder(mContext);
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
mBuilder.setContentTitle("Notification")
.setContentText("Hello")
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
Best & simple way is set builder.setAutoCancel(true) it's cancel your notification after clicking on notification. I hope this code help you.
Builder for notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(android.R.drawable.btn_star);
builder.setContentTitle("This is title of notification");
builder.setContentText("This is a notification Text");
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
For open an activity on clicking notification
Intent intent = new Intent(Broadcastdemo.this, ThreadDemo.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 113,intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
Show bulder in notification
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(114, builder.build());
Complete code for show a notification with icon, image, title, description, auto cancel and on click open an activity
public void ShowIntentNotification(View v)
{
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(android.R.drawable.btn_star);
builder.setContentTitle("This is title of notification");
builder.setContentText("This is a notification Text");
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
Intent intent = new Intent(Broadcastdemo.this, ThreadDemo.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 113,intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(114, builder.build());
}
For my it was that
.setPriority(Notification.PRIORITY_HIGH);
that was causing the notification to not clear after click... make sure you use:
.setPriority(Notification.PRIORITY_DEFAULT);
And .setAutoCancel(true) should work.
The below code is working fine with me in terms of creating the notification, with its voice and actions.
but looks the .setGroup(GROUP_KEY_Fonix) is not working, as the notifications are not grouped together, am I missing something here!
public int notificationID = 0;
final static String GROUP_KEY_Fonix = "fonix_notification";
private void Notify(String notificationTitle, String notificationMessage){
notificationID++;
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
// intent triggered, you can add other intent for other actions
Intent intent = new Intent(MainActivity.this, NotificationView.class);
PendingIntent pIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
// Build the notification, setting the group appropriately
Notification notif = new NotificationCompat.Builder(this)
.setGroup(GROUP_KEY_Fonix)
.setContentTitle("New note: " + notificationTitle)
.setContentText(notificationMessage)
.setSmallIcon(R.drawable.ic_stat_new_message)
.setContentIntent(pIntent)
.setSound(soundUri)
.addAction(R.drawable.ic_stat_new_message, "View", pIntent)
.addAction(0, "Remind", pIntent)
.build();
// Issue the notification
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(this);
// hide the notification after its selected
notif.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(notificationID, notif);
}
Also, you should create group summary notification. For this just use this code:
Notification notificationSummary = new NotificationCompat.Builder(this)
//Set content
.setGroup(GROUP_KEY_Fonix)
.setGroupSummary(true)
.build();
notificationManager.notify(notificationSummaryId, notificationSummary);
Learn more here.
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.
I've been searching for almost a day for this problem, I followed the example about Android Notification here: http://www.vogella.com/articles/AndroidNotifications/article.html. It worked well and I can send the Notification. The problem is, when I click on the notification, it does NOT disappear even though I have set:
setAutoCancel(true) AND notification.flags |= Notification.FLAG_AUTO_CANCEL
Update: Here is my code
Intent intent = new Intent(MyNotification.this, NotificationReceiver.class);
PendingIntent pIntent = PendingIntent.getActivity(
MyNotification.this, 0, intent, 0);
NotificationCompat.Builder builder =
new NotificationCompat.Builder(MyNotification.this)
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true)
.addAction(R.drawable.ic_launcher, "Call", pIntent)
.addAction(R.drawable.ic_launcher, "More", pIntent)
.addAction(R.drawable.ic_launcher, "And more", pIntent)
.setContentTitle("New mail from " + "test#gmail.com")
.setContentText("Subject");
Notification noti = builder.build();
noti.flags |= Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, noti);
Any ideas where the problem come from? Thanks for your helps.
use this..
notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL
Android notification doesn't disappear after clicking the notifcation
Try removing:
noti.flags |= Notification.FLAG_AUTO_CANCEL;
It should work without noti.flags.
The new way of using flags in NotificationCompat is to use the defined methods, like setAutoCancel(true).
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).