I'm programming an android app, which shows a notification. I know how to show it but how can my app learn whether it is removed by being swiped right or left? Is there any way to know this?
This is the way I created the notification:
public void showNotification(){
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
mBuilder.setContentTitle("My notification");
mBuilder.setContentText("Hello World!");
Notification mNotification = mBuilder.build();
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(R.id.note, mNotification);
}
Related
I'm using the following to code to send a notification:
NotificationCompat.Builder builder =
new NotificationCompat.Builder(getBaseContext())
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("Title")
.setContentText("Message");
int NOTIFICATION_ID = 12345;
Intent targetIntent = new Intent(getBaseContext(), MainActivity4.class);
PendingIntent contentIntent = PendingIntent.getActivity(getBaseContext(), 0, targetIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
NotificationManager nManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nManager.notify(NOTIFICATION_ID, builder.build());
The code works as I expected except for one little thing, once I go to the notification bar, expand it and choose the notification, it keeps staying on the notification bar, opposite to the behavior of most notifications, than once you tap them they disappear from being there.
Guess it must be some option in the configuration of the notification but as much as I search I cannot find it.
Hope you can help.
Are you looking for this?
.setAutoCancel(true)
so you would have
NotificationCompat.Builder builder =
new NotificationCompat.Builder(getBaseContext())
.setSmallIcon(R.drawable.notification_icon)
.setAutoCancel(true)
.setContentTitle("Title")
.setContentText("Message");
I am working on an app in android in which i wish to notify specific users about specific events. These notifications are made by using the the Firebase Cloud Messaging data-messages. When I send a data-message to the users client, the onMessageReceived-method is called and I can handle these messages as I wish.
Now I want to make the device vibrate in the very moment the messages arrives. Therefore, I tried to build a notification and set the vibration on it, however it does not vibrate at all..
I included the VIBRATE-permission in my applications Manifest as well.
Here is my Code to build the notification:
NotificationCompat.Builder mBuilder =
(NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_icon)
.setContentTitle("Content Title")
.setContentText("Content Text")
.setVibrate(new long[] {500,500,500,500,500,500,500,500,500});
Notification note = mBuilder.build();
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, note);
Have I missed anything?
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic)
.setContentTitle(context.getString(R.string.text))
.setContentText(context.getString(R.string.app_name))
.setContentIntent(contentIntent);
mBuilder.setVibrate(new long[]{500, 500});
mBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
NotificationManager mNotifyMgr =
(NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
mNotifyMgr.notify(001, mBuilder.build());
This works for me.
I am building and easy android app.
I am having trouble with notification. When the app receive the notification it shows it properly as heads up with all the picture and the text, while on the lock screen, if the phone is locked, does not show the text. To see the text I have to swipe the notification down.
This is the code:
NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();
bigText.bigText(body);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_stat_luceterna_notifica_90)
.setContentTitle("Luceterna")
.setLargeIcon((((BitmapDrawable) this.getResources().getDrawable(R.mipmap.ic_launcher_lux)).getBitmap()))
.setAutoCancel(true)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
mBuilder.setStyle(bigText);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mBuilder.setSound(alarmSound);
NotificationManager mNotificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Can please anyone help me? I cannot find any answer to the problem.
Thanks in advance.
I know it's been a while since you asked this question, but I encountered same problem today, and found a sollution.
To solve this problem, you have to override NotificationCompat setContentTitle and
setContentDescritption
With BigTextStyle ones: setBigContentTitle and bigText
You have to use them both, with same texts inside. So, your code would look like:
String title = "Title";
String description = "Description";
NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
bigTextStyle.setBigContentTitle(title);
bigTextStyle.bigText(description);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setContentTitle(title)
.setContentText(description)
.setSmallIcon(R.drawable.ic_launcher)
.setStyle(bigTextStyle);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher) // notification icon
.setContentTitle("Notification!") // title for notification
.setContentText("Hello word") // message for notification
.setAutoCancel(true); // clear notification after click
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(this,0,intent,Intent.FLAG_ACTIVITY_NEW_TASK);
mBuilder.setContentIntent(pi);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
An example to help you
I have got this simple code that issues a Notification.
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this)
.setSmallIcon(
R.drawable.ic_launcher)
.setContentTitle("Error deactivating Tracker")
.setContentText("Unable to send deactivation SMS");
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(0, mBuilder.build());
The notifications shows correctly on a 4.4.2 device, but it does not show on a 2.3.6 device.
I am using the NotificationCompat so I suppose it should show.
What am I doing wrong?
Solved.
Apparently it wants a PendingIntent in any case,
so I added it:
PendingIntent pi = PendingIntent.getBroadcast(this, 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this)
.setSmallIcon(
R.drawable.ic_launcher)
.setContentTitle("Error deactivating Tracker")
.setContentText("Unable to send deactivation SMS")
.setContentIntent(pi).setTicker("Error deactivating Tracker");
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(0, mBuilder.build());
Some question about push notifications. How do I dissmis all the notifications of my app when I press one of them? Also ... how do I dissmiss the noifications if I start the app (not from the noification)?
Here is how I send a notification from my GcmIntentService class (if it is helpfull).
private void sendNotification(Bundle extras) {
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
NOTIFICATION_ID = Integer.parseInt(extras.getString("id"));
Intent in = new Intent(this, PushActivity.class);
in.putExtras(extras);
in.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
String msg = extras.getString("msj");
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
in, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("MY TITLE")
.setAutoCancel(true)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
I'm not sure if the notifications you are putting out are for the same event, if so you can do what is called "stacking notifications" like Gmail does when you get a second or third email. It's all in the one notification and therefore will go away as one. This is only compatible above 4.1.
This is the code Google gives along with the stacking. This any help?
mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Sets an ID for the notification, so it can be updated
int notifyID = 1;
mNotifyBuilder = new NotificationCompat.Builder(this)
.setContentTitle("New Message")
.setContentText("You've received new messages.")
.setSmallIcon(R.drawable.ic_notify_status)
numMessages = 0;
// Start of a loop that processes data and then notifies the user
...
mNotifyBuilder.setContentText(currentText)
.setNumber(++numMessages);
// Because the ID remains unchanged, the existing notification is
// updated.
mNotificationManager.notify(
notifyID,
mNotifyBuilder.build());
try android.app.NotificationManager.cancelAll()