I am developing an Android app. I my app, I am trying to show process notification to user. But what I want is I do not want notification opening an Activity or removed when it is get clicked. What I am doing is I am showing notification when a user click a button. Notification will saying "Transferring data". Notification is fired from receiver.
So when user click a button, receiver is called. But I do not want that notification removed or closed when user click it. I do not want to show any activity as well. I just want to close itself when data processing finish.
This is my receiver that show notification when button is clicked
public class GalleryImageUploadReceiver extends BroadcastReceiver {
private Context context;
private NotificationCompat.Builder notification;
@Override
public void onReceive(Context pContext, Intent intent) {
this.context = pContext;
showProcessNotification();
doAsyncTaskInBackground()
}
public void doAsyncTaskInBackground()
{
//I want to close it here after task is finished
}
public void showProcessNotification()
{
try{
notification = new NotificationCompat.Builder(context);
notification.setAutoCancel(true);
notification.setSmallIcon(R.mipmap.ic_launcher);
notification.setWhen(System.currentTimeMillis());
notification.setTicker("Transferring data...");
notification.setContentTitle(context.getResources().getString(R.string.app_name));
notification.setContentTitle("Transferring data...");
notification.setOngoing(true);
notification.setDefaults(Notification.FLAG_ONGOING_EVENT);
Intent i = new Intent(context.getApplicationContext(), MainActivity.class);//In this line, I do not want to open any activity
i.putExtra("start", OfficeMainActivity.FRAGMENT_NOTIFICATIONS);
PendingIntent pendingIntent = PendingIntent.getActivity(context.getApplicationContext(),0,i,PendingIntent.FLAG_UPDATE_CURRENT);
notification.setContentIntent(pendingIntent);
NotificationManager nm = (NotificationManager)context.getApplicationContext().getSystemService(context.getApplicationContext().NOTIFICATION_SERVICE);
nm.notify(1,notification.build());
}
catch (Exception e)
{
}
}
public void uploadImages(Context context,Intent intent)
{
onReceive(context,intent);
}
}
Receiver is called in button click event like this
GalleryImageUploadReceiver receiver = new GalleryImageUploadReceiver();
receiver.uploadImages(getBaseContext(),new Intent());
As you can see, I set ongoing to true because I want to make it undismissable. But the problem is it is closed because an activity is opened.
These are what I want:
But I do not want that notification removed or closed when user click it.
remove this
notification.setAutoCancel(true);
I do not want to open an activity when notification is clicked.
remove this
Intent i = new Intent(context.getApplicationContext(), MainActivity.class);//In this line, I do not want to open any activity
i.putExtra("start", OfficeMainActivity.FRAGMENT_NOTIFICATIONS);
PendingIntent pendingIntent = PendingIntent.getActivity(context.getApplicationContext(),0,i,PendingIntent.FLAG_UPDATE_CURRENT);
notification.setContentIntent(pendingIntent);
I suggest you read up on notification documentation. This is pretty straightforward stuff.
How can I close if self please?
you can "close" a notification manually with
nm.cancel(1); // if 1 is your notification id.
Instead can show option dialog with buttons?
Instead of doing that you should just present the buttons inside the notification.