I use this code to show a notification:
public static void ShowCustomNotification(Context context,String notificationTitle, String notificationMessage)
{
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
// intent triggered, you can add other intent for other actions
Intent intent = new Intent(context, ResultActivity.class);
intent.putExtra("title", notificationTitle);
intent.putExtra("body", notificationMessage);
intent.setFlags(PendingIntent.FLAG_UPDATE_CURRENT
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);
Notification mNotification = new NotificationCompat.Builder(context)
.setContentTitle(notificationTitle)
.setSmallIcon(R.drawable.office_girl)
.setContentIntent(pIntent)
.setSound(soundUri)
.build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, mNotification);
}
When the user clicks the Notification the activity ResultActivity.class is invoked and everything is fine.
My question is how can i clear the notification on the status bar when a user click on a "Close" button from inside ResultActivity? in case the user click "Back" i dont want to clear the notification.
Thanks.
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(YOUR_NOTIFICATION_ID);
Related
I am trying to make Notification Reply like whatsapp.
here is my code
public void sendNotification(String msg, Intent i, ContactModel contact)
{
i.putExtra("message", msg);
i.putExtra("contact", new Gson().toJson(contact));
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
mNotificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
String replyLabel = "Reply";
RemoteInput remoteInput = new RemoteInput.Builder("KEY_REPLY")
.setLabel(replyLabel)
.build();
PendingIntent contentIntent = PendingIntent.getActivity(context, 100, i,
PendingIntent.FLAG_UPDATE_CURRENT);
Bundle bundle = new Bundle();
bundle.putBoolean("reply", true);
NotificationCompat.Action replyAction = new NotificationCompat.Action.Builder(
R.drawable.ic_phone, replyLabel, contentIntent)
.addRemoteInput(remoteInput)
.addExtras(bundle)
.setAllowGeneratedReplies(true)
.build();
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher))
.setContentTitle(contact.getName())
.setDefaults(Notification.DEFAULT_SOUND)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText("Content Hidden"))
.setContentText(msg)
.addAction(replyAction)
;
mBuilder.setContentIntent(contentIntent);
mNotificationManager.cancel(Constants.PUSH_ID);
mNotificationManager.notify(Constants.PUSH_ID, mBuilder.build());
}
Now my notification shows "Message" and a "Reply" Button.
The problem i am facing is, i can't differentiate wether user chose Reply button or tried to open the notification.
My main intent is
Intent myIntent = new Intent(this, NotificationActivity.class);
What i want is,
When a user click on "Reply" Button it opens
Intent myIntent = new Intent(this, NotificationActivity.class);
When a user click on notification itself it opens
Intent myIntent = new Intent(this, MainActivity.class);
Even if thats not possible , how can i figure out which action was performed.
Try to add
setContentIntent(new Intent(this, MainActivity.class))
Additional info here
You should create a pending intent for each case , one for reply button and another for your message notification.
addAction() define action button for your notificaion, and setContentIntent(pendingIntent) do the same for your core message.
.... your code
Intent messageIntent = new Intent(this, MainActivity.class);
PendingIntent messagePendingIntent = PendingIntent.getActivity(context, 200, messageIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher))
.setContentTitle(contact.getName())
.setDefaults(Notification.DEFAULT_SOUND)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText("Content Hidden"))
.setContentText(msg)
.setContentIntent(messagePendingIntent) // some change here
.addAction(replyAction)
Hope this helps.
Sorry for my english.
What I understand from your statement is: you are expecting two different intents for two different actions so, I'm not confirm that it will solve your problem, but this is what I tried.
public void createNotification() {
Intent intent = new Intent(this, NotificatioTest.class);
PendingIntent pIntent = PendingIntent.getActivity(this,(int) System.currentTimeMillis(), intent, 0);
Intent mIntent = new Intent(this, MainActivity.class);
PendingIntent mPIntent = PendingIntent.getActivity(this,(int)System.currentTimeMillis(),mIntent,0);
Notification noti = new Notification.Builder(this)
.setContentTitle("Your Title")
.setContentText("Your text content")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent)
.addAction(R.drawable.ic_launcher, "Reply", mPIntent).build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, noti);
}
The change is I have used two different PendingIntent as setContentIntent(pIntent) and addAction(R.drawable.ic_launcher,"Reply",mPIentent).
Output:
https://youtu.be/vVKVMboHEOA
I'm trying to create Notification using AppCompat inside Fragment.
I create see nothing.
Code:
private void createNotification()
{
Context context = getActivity().getApplicationContext();
Intent notificationIntent = new Intent(context, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.ymk_ya_logo)
.setAutoCancel(false)
.setContentTitle("notify")
.setContentText("test");
Notification notification = builder.build();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(777, notification);
}
Try changing your last bit to this instead:
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
Hi I want to show alert after click on my notification. Here is code:
#SuppressWarnings("deprecation")
private void Notify(String notificationTitle, String notificationMessage) {
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon_stat, "Powiadomionko", System.currentTimeMillis());
notification.ledARGB = Color.CYAN;//0xFFff0000;
notification.ledOnMS = 800;
notification.ledOffMS = 2400;
notification.vibrate = new long[]{100, 120, 100, 120};
Intent notificationIntent = new Intent(this, TerminarzActivity.class);
notification.flags = Notification.FLAG_SHOW_LIGHTS | Notification.FLAG_AUTO_CANCEL | Notification.DEFAULT_VIBRATE;
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(Serwis_updateTERM.this, notificationTitle, notificationMessage, pendingIntent);
notificationManager.notify(1, notification);
}
And I want to show Alert on anything not on my Activity after click. Anyone know how to do this ?
I know i must add
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
but nothing else..
Check out the creation of this notification (taken from Geofence sample). This code creates a notification and if you touch it it launches your MainActivity.
// Create an explicit content Intent that starts the main Activity
Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);
// Construct a task stack
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the main Activity to the task stack as the parent
stackBuilder.addParentStack(MainActivity.class);
// Push the content Intent onto the stack
stackBuilder.addNextIntent(notificationIntent);
// Get a PendingIntent containing the entire back stack
PendingIntent notificationPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
// Get a notification builder that's compatible with platform versions
// >= 4
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
// Set the notification contents
builder.setSmallIcon(R.drawable.ic_folder)
.setContentTitle(getString(R.string.geofence_transition_notification_title, transitionType, ids))
.setContentText(getString(R.string.geofence_transition_notification_text))
.setContentIntent(notificationPendingIntent);
// Get an instance of the Notification manager
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Issue the notification
mNotificationManager.notify(0, builder.build());
This code might help you, but still it depends what do you mean by "alert".
I am building a notification for my Music Player, so it displays what music is playing.
But I want to add a function where, when I touch it. It opens my layout player.xml. Which is deployed through the MainActivity.class.
I have researched on Notifications on developer.android.com and found a way to deploy and activity upon clicking the notification. But it did't work.
Here is my current code -
Notification.Builder builder = new Notification.Builder(getApplicationContext());
builder.setContentTitle(songsList.get(songIndex).get("songTitle"));
builder.setContentText("NexPlay™");
builder.setTicker(songsList.get(songIndex).get("songTitle"));
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setSmallIcon(R.drawable.play_default);
builder.setAutoCancel(true);
builder.setPriority(0);
builder.setOngoing(true);
Notification notification = builder.build();
NotificationManager notificationManger =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManger.notify(01, notification);
Thanks to everyone for answering!
I added
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, NotificationToPlayer.class), 0);
builder.setContentIntent(pendingIntent);
to my notifications and it worked great !
Try this
private void showNotification() {
// In this sample, we'll use the same text for the ticker and the expanded notification
CharSequence text = getText(R.string.service_started);
// Set the icon, scrolling text and timestamp
Notification notification = new Notification(R.drawable.android, text,
System.currentTimeMillis());
// The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, ServiceLauncher.class), 0);
// Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(this, getText(R.string.service_label),
text, contentIntent);
notification.defaults |= Notification.DEFAULT_SOUND;
// Send the notification.
// We use a layout id because it is a unique number. We use it later to cancel.
nm.notify(R.string.service_started, notification);
}
Hope this will solve your problem
You need to add intent on ur code:-
int icon = R.drawable.app_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message.split(":::")[1], when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = null;
notificationIntent = new Intent(context, MessageDetail.class);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, title, message.split(":::")[1], intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
You'll also want to add an intent. Something like:
Intent actIntent = new Intent(this, do MyActivity.class);
Then in your builder:
.setContentIntent(PendingIntent.getActivity(MyActivity, 131314, actIntent,
PendingIntent.FLAG_UPDATE_CURRENT))
I would like to have a notification open the activity from which it is created. I am using ActionBarSherlock and my code looks like this:
public class CurrentActivity extends SherlockFragmentActivity {
private void remindMeAlert() {
Intent intent = new Intent(this, CurrentActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification noti = new NotificationCompat.Builder(this)
.setContentTitle("title")
.setContentText("text")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent).build();
noti.flags |= Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, noti);
}
}
This does not make any action when pressing the notification. However if I open any other activity like:
Intent intent = new Intent(this, DifferentActivity.class);
It works fine.