I try to close my app after button click.
I know that i need to use pending intent and intent to do that, but i don't know how can i do it and which flags or actions can I use.
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "")
.setSmallIcon(R.mipmap.ic_launcher_notification)
.setContentTitle("Close your app")
.setContentText("Your app is still running...")
.addAction(R.drawable.ic_exit,"Close App",
PendingIntent.getBroadcast(this, 0, intent, 0))
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, builder.build());
This is not working...
Related
I have a local notification which is set in one of my receivers. Onclick of notification I want to open a specific activity. Below is my code.
NotificationCompat.Builder builder =
(NotificationCompat.Builder) new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Its Your day!!")
.setContentText(message)
.setAutoCancel(true);
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
// Add as notification
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(times, builder.build());
But this is not working. On Click of notification it just removes notification from status bar. Am I missing anything ?
try this :
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
instead of this :
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
use this codes before your notification builder code.
now this is the full version :
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder builder =
(NotificationCompat.Builder) new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Its Your day!!")
.setContentText(message)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(times, notificationBuilder.build());
try to use this. i hope you will get you desire answer.
UPDATE
here is the sample project : https://github.com/ImtiazDipto01/SimpleNotification
Add Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP as Flag on your `Intent. Like -
notificationIntent .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Also provide unique id for notification. like -
int uniqueNotificationId = (int) (System.currentTimeMillis() & 0xfffffff);
PendingIntent contentIntent = PendingIntent.getActivity(context, uniqueNotificationId , notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Also on -
manager.notify(uniqueNotificationId , builder.build());
My notification can show but I would also want it to be clickable so that when it's clicked it would open the same activity it came from.
public void acceptNotification(){
NotificationCompat.Builder builder = new NotificationCompat.Builder(RequestConfirm.this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle("PEOPLE HELPER");
builder.setContentText("Your request has been accepted");
Intent intent = new Intent(RequestConfirm.this, BroadcastFragment.class); //creates an explicit intent
TaskStackBuilder stackBuilder = TaskStackBuilder.create(RequestConfirm.this);
stackBuilder.addParentStack(RequestConfirm.this); //adds the intent
stackBuilder.addNextIntent(intent); //put the intent to the top of the stack
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); //(id, flag) //creates a pending intent
builder.setContentIntent(pendingIntent); //adds the PendingIntent to the builder
NotificationManager notificationManager = (NotificationManager) RequestConfirm.this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
}
like this :
Intent gotoIntent = new Intent();
gotoIntent.setClassName(getApplicationContext(), "FULL CLASS NAME");
PendingIntent contentIntent = null
contentIntent = PendingIntent.getActivity(getApplicationContext(),
(int) (Math.random() * 100), gotoIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Now set in notification Builder:
.setContentIntent(contentIntent)
To start the activity, you have to use the flag Intent.FLAG_ACTIVITY_NEW_TASK:
Intent intent = new Intent(this, YourActivityClass.class);
PendingIntent pendingIntent = PendingIntent.getActivity(
this,
0,
intent,
Intent.FLAG_ACTIVITY_NEW_TASK);
Call notificationManager.notify(0, builder.build()); when you want to show the notification. By clicking that pending intent will be started.
try this
Intent intent = new Intent(this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
notificationBuilder.setContentTitle(title);
notificationBuilder.setContentText(msg);
notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
notificationBuilder.setVibrate(vibrationPattern);
notificationBuilder.setAutoCancel(true);
notificationBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0,notificationBuilder.build());
Try this , hope this will helpful for you.
NotificationManager notif = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(CurrentActivity.this);
Notification notify;
PendingIntent pending = PendingIntent.getActivity(CurrentActivity.this, 0,
new Intent(CurrentActivity.this, NextActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
notify = builder.setContentIntent(pending)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message1))
.setSmallIcon(R.drawable.logop).setTicker(excep).setWhen(System.currentTimeMillis())
.setAutoCancel(true).setContentTitle("your Notification title")
.setContentText(message1).build();
CurrentActivity refers to Activty/class that belong to your service Activity/class and nextActivity refers to Activity/class where you want to move.
It's able to show the notification but opens the app in background.
PendingIntent pendingIntent;
Intent intent = new Intent(this, ExpertActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.sendmessageicon);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setLargeIcon(largeIcon)
.setSmallIcon(R.drawable.sendmessageicon)
.setContentTitle("Dummy")
.setContentText(messageBody)
.setAutoCancel(true)
.setDefaults(-1)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 , notificationBuilder.build());
Please try this
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, ExpertActivity.class), 0);
Try this one
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,PendingIntent.FLAG_ONE_SHOT);
I want the Main Activity to be launched when the Notification is clicked. This is even when the user is not in the app. How do I do so?
NotificationCompat.Builder mBuilder =new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.diamond)
.setContentTitle("Crystallise")
.setContentText("making your thoughts crystal clear");
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(notifyID, mBuilder.build());
First of all you need PendingIntent :
Intent intent=new Intent(mContext, Activity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent notificationIntent= PendingIntent.getActivity(mContext,requestCode, intent,PendingIntent.FLAG_UPDATE_CURRENT);
Then in your NotificationCompat.Builder add this: .setContentIntent(notificationIntent)
For unique request code use this : int requestCode=(int)System.currentTimeMillis();
You can use this in Non Activity classes too, e.g: Service, BroadcastReceiver . Then your app will be launched even it is in closed state.
//add intent and PendingIntent for open activity
Intent attendanceIntent = new Intent(getApplicationContext(), Details.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0,
attendanceIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.diamond)
.setContentTitle("Crystallise")
.setContentIntent(pendingIntent)
.setContentText("making your thoughts crystal clear");
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(notifyID, mBuilder.build());
a have tried 8 versions of the following code, but the Home activity is not being launched when I click the notification.
Here is the code (it's inside of a Service)
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());
builder.setSmallIcon(R.mipmap.alert);
builder.setContentTitle("");
builder.setContentText("Running in Background");
builder.setOngoing(true);
IntenIntenticationIntent = new Intent(getApplicationContext(), Home.class);
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0,notificationIntent, 0);
notificationManager = (NotificationManager) getApplicationContext().getSystemService(getApplicationContext().NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, builder.build());
Could you please the code below?
Intent notificationIntent = new Intent(this, Home.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.mipmap.alert);
builder.setContentTitle("");
builder.setContentText("Running in Background");
builder.setOngoing(true);
builder.setContentIntent(contentIntent);
notificationManager = (NotificationManager) this.getSystemService(Service.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, builder.build());