I am new to android and developing an application with a notification.
I need to show a notification with an extra button; when I click on it, I need to go to some activity in my application. Say "SettingsActivity"
I have the following code. It works fine, but the notification doesn't cancel automatically when press on that addAction icons
Any idea?
My code looks like
Intent intent = new Intent(context, SettingsActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Notifications Example")
.setContentText("This is a test notification");
builder.setDefaults(Notification.DEFAULT_ALL);
builder.setLights(Color.parseColor("#fec317"), 300, 1000);
Intent notificationIntent = new Intent(context, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
builder.addAction(R.drawable.delete, "Call", pIntent);
builder.setAutoCancel(true);
NotificationManager manager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, builder.build());
As a workaround: you may remove it manually using mNotificationManager.cancel(id);
where id
is the ID you've passed when you created this notification
You can make a broadcast receiver which listens for two events, one for starting notification and another for stopping notification. Below is one example that I am currently using in one of my project. Feel free to ask what you don't get.
public class sendMessageBroadcast extends BroadcastReceiver {
RemoteViews remoteViews;
private static Thread thread;
NotificationManager notificationManager;
Notification notification;
@Override
public void onReceive(Context context, Intent intent) {
Log.d("sendMessageBroadcast", "received");
notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
remoteViews = new RemoteViews(context.getPackageName(), R.layout.customnotification);
int SITUATION_ID = intent.getIntExtra("SITUATION_ID",-1);
Intent remoteViewsIntent = new Intent("com.itcse.Situation.cancelNotification");
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
0, remoteViewsIntent, PendingIntent.FLAG_UPDATE_CURRENT);//getActivity(getBaseContext(),0,intent, PendingIntent.FLAG_UPDATE_CURRENT);
if(intent.getAction().equals("com.itcse.Situation.cancelNotification")) {
thread.interrupt();
}
else {
Log.d("Cancel Messge",""+ SITUATION_ID);
if(Build.VERSION.SDK_INT <= 10 ){
remoteViews.setViewVisibility(R.id.bCancel, View.GONE);
remoteViews.setViewVisibility(R.id.tvCancelNotification, View.VISIBLE);
notification = new Notification(R.drawable.situation,"Sending Situation",System.currentTimeMillis());
notification.contentIntent = pendingIntent;
notification.contentView = remoteViews;
notification.flags = Notification.FLAG_AUTO_CANCEL;
updateProgressBar(notification);
}
else {
remoteViews.setOnClickPendingIntent(R.id.bCancel, pendingIntent);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setAutoCancel(true).setContent(remoteViews).setTicker("Sending Situation Message")
.setWhen(System.currentTimeMillis()).setSmallIcon(R.drawable.situation);
if (Build.VERSION.SDK_INT >= 11 && Build.VERSION.SDK_INT <15){
updateProgressBar(builder.getNotification());
}
else if(Build.VERSION.SDK_INT >=15){
updateProgressBar(builder.build());
}
}
}
}
public void updateProgressBar(final Notification notification){
thread = new Thread(new Runnable() {
@Override
public void run() {
//unregisterReceiver(broadcastReceiver);
int incr;
boolean send=true;
for(incr = 0;incr<=100;incr+=10){
remoteViews.setProgressBar(R.id.progressBar, 100, incr, false);
notificationManager.notify(1,notification);
if(Thread.currentThread().isInterrupted()){
Log.d("Thread", "Interrupted");
send=false;
break;
}
try{
Thread.sleep(1000);
}
catch (InterruptedException e){
Log.d("Thread", "Thread interrupted while sleeping");
send = false;
break;
}
}
Log.d("Thread","Value of incr "+incr);
remoteViews.setViewVisibility(R.id.bCancel, View.GONE);
remoteViews.setViewVisibility(R.id.tvCancelNotification, View.GONE);
remoteViews.setViewVisibility(R.id.progressBar, View.GONE);
if(send) {
sendMessage();
remoteViews.setTextViewText(R.id.tvNotificationHeading, "Message send");
}
else {
remoteViews.setTextViewText(R.id.tvNotificationHeading, "Message not send");
notification.tickerText = "Message not send";
}
notificationManager.notify(1, notification);
}
});
thread.start();
}
private void sendMessage() {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("5556",null,"3G",null,null);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.