How to make notification clickable? - android

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.

Related

How to close app by clicking a button in the notification?

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...

local Notification on click open an activity

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());

How to set ongoing flags in builder notification?

How do I add flags in my notification created using the Notification.Builder builder? It was straightforward to add flags to the Notification noti = new NotificationCompat.Builder(this) notification declaration but not to the one I've referenced above. Here is what I've tried so far:
Notification.Builder builder = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Download [ROOT]")
//.setContentText("This is a test notification")
.setAutoCancel(false).setOngoing(true);
Intent notificationIntent = new Intent(this, NotificationAction.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
// Add as notification
NotificationManager manager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(111, builder.build());
for setOngoing() to Notification.Builder try this :
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentIntent(generatePendingIntent("Notification Type"))
.setAutoCancel(true)
.setOngoing(true/false); // this is where you have to set
you can use Notification.Builder too but with NotificationCompat.Builder and in Android 4.1 and later you able to set action buttons depend on expanded notifications.
And to handle how what's to happen when click on notification method :
private PendingIntent generatePendingIntent(String type) {
PendingIntent pendingIntent;
Intent notificationIntent;
if (type.equals("link")) {
notificationIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pendingIntent = PendingIntent.getActivity(context.getApplicationContext(), 0, notificationIntent, 0);
} else {
notificationIntent = new Intent(context, DesiredActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pendingIntent =
PendingIntent.getActivity(context.getApplicationContext(), 0, notificationIntent, 0);
}
return pendingIntent;
}

Activity is not being launched after click in a Notification

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());

Android AlarmManager putExtra to PendingIntent and NotificationCompat.Builder

I'm trying to putExtra to Activity which will be launched after clicking on notification, but instead of value that I set I'm getting default value. This is my code in AlarmReceiver:
Intent notifActiv = new Intent(context, NotificationActivity.class);
notifActiv.putExtra("ID", id);
PendingIntent pI = PendingIntent.getActivity(context, 0, notifActiv, 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
context).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(string).setTicker("You got meeting today!")
.setContentText("Click here for more details");
mBuilder.setContentIntent(pI);
mBuilder.setDefaults(NotificationCompat.DEFAULT_VIBRATE);
mBuilder.setAutoCancel(true);
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, mBuilder.build());
And this is my NotificationActivity where I'm trying to get Extras:
Intent intent = getIntent();
int id = intent.getIntExtra("ID", 0);
Could you please tell my where I'm doing something wrong?

Categories

Resources