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());
Related
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...
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.
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?
I want to auto cancel my notification when user clicks on notification. The following code works good in all the devices except Android lollipop device. In Lollipop device, notification goes only when user swipes it off.
#SuppressWarnings("deprecation")
public void sendNotification(int id){
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Test")
.setContentText("Jump to next screen")
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true);
mBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONLY_ALERT_ONCE;
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, NextActivity.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent resultPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0,
resultIntent, 0);
//PendingIntent.FLAG_UPDATE_CURRENT
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// id, if there is a need to update the notification later on.
mNotificationManager.notify(id, mBuilder.build());
Log.v(TAG, "Notification ID value " + id);
//mNotificationManager.cancel(id);
}
What is missing in this code?
Looks good to me. My auto cancel still works on 5.0.2. Let me give you a portion of my code:
public static void notif(int id, String title, String text, Context context, Class activity) {
// get an instance of the NotificationManager service
if (mNotifyMgr == null)
mNotifyMgr = (NotificationManager) context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(context, activity);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent notifIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.launcher)
.setContentTitle(title)
.setContentText(text)
.setContentIntent(notifIntent);
mNotifyMgr.notify(id, mBuilder.build());
}
modify following:
setAutoCancel(true) -> setAutoCancel(false);
then on action activity do the following
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancelAll();
if you want to set special condition you can set by
intent.putExtra("key", "value")
Below is my code for showing a Notification, and after clicking it I redirect to another activity using the PendingIntent
But when I click on this notification my application hangs;
On checking my LogCat I see continues GC invoke logs
Please correct where am i going wrong:
private void showNotification(int notifyId, String alertMsg, PendingIntent intent) {
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(getResources().getString(R.string.app_name))
.setStyle(new NotificationCompat.BigTextStyle().bigText(alertMsg))
.setSound(soundUri)
.setContentText(alertMsg);
mBuilder.setAutoCancel(true);
mBuilder.setContentIntent(intent);
mNotificationManager.notify(notifyId, mBuilder.build());
}
ALSO ABOVE CODE DOES PLAYS THE DEFAULT NOTIFICATION SOUND.
Please help.
Thanks in advance.
HERE IS MY PendingIntent Code:
Intent contentIntent = new Intent(this, AccountDetailsActivity.class);
contentIntent.putExtra(EXTRA_TYPE, extras.getString("cat"));
contentIntent.putExtra(EXTRA_ACCOUNT_ID, extras.getString("rem"));
PendingIntent pendingIntent = PendingIntent.getActivity(this,
NOTIFY_REQUEST_CODE, contentIntent, PendingIntent.FLAG_UPDATE_CURRENT);
showNotification(NOTIFICATION_TYPE_REMINDER, alertMsg, pendingIntent);
THANKS