Notification code disappears automatically - android

When I receive the notification I would like it to automatically disappear and have to upload manually I, Thank you in advance.
Here I attach the code, in which I create the notification. What I want to happen is that when I receive the notification, I get off, I can see it on the screen, and after a few seconds I go up alone without having to interact with it.
public void notificacion(String label, String autor,String destino) {
NotificationCompat.Builder notifica = new
NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon((((BitmapDrawable) getResources()
.getDrawable(R.drawable.ic_launcher)).getBitmap()))
.setContentTitle("tittle")
.setContentText(label)
.setAutoCancel(true);
Intent intnot = new Intent(this, MainActivity.class);
intnot.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intnot.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intnot.putExtra("Destino", destino);
PendingIntent intnotpend = PendingIntent.getActivity(this, 0, intnot, PendingIntent.FLAG_UPDATE_CURRENT);
notifica.setContentIntent(intnotpend);
notifica.setFullScreenIntent(intnotpend, true);
notifica.setContentIntent(intnotpend);
}
NotificationManager notyman = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
notyman.notify(10, notifica.build());
}

Related

Update notifcation text

I have currently setup a small application which sets an alarm on the device and stores relevant information inside of an SQL database e.g. Alarm name and time. When the alarm is activated my application sends a notification to the user. Currently each notification is static which means every message is the same. I would like to now allow my application to grab the name of the alarm which has been activated and display it in the notification. Now I know that when setting multiple alarms you require multiple ID's which I have stored in side of my SQL and I'm guess the same when it comes to sending a notification. Is there a way of matching my Alarms ID with one on a notification so that it knows what message to send e.g. alarm name?
Current code for setting my Alarm
pendingIntent = PendingIntent.getBroadcast(view.getContext(), Integer.valueOf(alarm_request_code), receiver, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, myCalendar.getTimeInMillis(), pendingIntent);
intentArrayList.add(pendingIntent);
My Broadcast receiver...
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
Intent moveActivity = new Intent(context, AlarmActivity.class);
moveActivity.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, moveActivity, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentIntent(pendingIntent)
.setContentTitle("Standard text")
.setContentText("Standard text")
.setSmallIcon(R.mipmap.ic_launcher)
.setAutoCancel(true);
notificationManager.notify(0, builder.build());
}
//////////////
Update on my situation which is slowly driving me crazy
I can now set the text from my SQL to my notification but its only the first from my database each time. Now I've come up with two possible solutions which are only theory's.
Is there a way in which each time the database is called to the notification it will move onto the next result?
As I mentioned above I'm setting an alarm and when the alarm goes off it then calls the notification. Now In my pending intent for the alarm I'm giving it a id which you can see as alarm_request_code is there a way of giving it to my notification ID and then setting up a statement where if the notification ID is equal to or in the list of my alarm ID's stored on my SQL then it will search for the correct text to input.
MyDatabaseHandler myDatabaseHandler = new MyDatabaseHandler(context, null, null, 1);
Cursor cursor = myDatabaseHandler.getAllProducts();
// Information we are trying to acquire but can only get first result
String name = cursor.getString(cursor.getColumnIndexOrThrow("alarm_name"));
String date = cursor.getString(cursor.getColumnIndexOrThrow("alarm_date"));
// Alarm ID FROM SQL which we want to match with the NOTIFICATION ID.....
String alarm_request_code = cursor.getString(cursor.getColumnIndexOrThrow("alarm_code"));
////
Log.i("The reciever is working", "perfect");
//create an intent to service
Intent service_Intent = new Intent(context, MessageService.class);
context.startService(service_Intent);
//Notification
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
Intent moveActivity = new Intent(context, AlarmActivity.class);
moveActivity.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Works with moveActivity to move the user back to main application.
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, moveActivity, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentIntent(pendingIntent)
.setContentTitle(name)
.setContentText(date)
.setSmallIcon(R.mipmap.ic_launcher)
.setAutoCancel(true);
notificationManager.notify(Integer.valueOf(alarm_request_code), builder.build());
}
}
To update text store mBuilder variable
and update later in this way:
mBuilder.setContentTitle(head);
mBuilder.setContentText(body);
mNotificationManager.notify(SIMPLE_ID, mBuilder.build());
EDIT
private void simple() {
Intent intent = new Intent(context, AlarmActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
mBuilder = new NotificationCompat.Builder(this);
mBuilder.setContentTitle(getResources().getString(R.string.app_name))
.setContentText(getResources().getString(R.string.app_name))
.setContentIntent(pIntent);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
mBuilder.setSmallIcon(R.drawable.ic_done_24dp);
} else {
mBuilder.setSmallIcon(someiconLollipop);
mBuilder.setColor(somecolorLollipop]);
}
noti = mBuilder.build();
//use flags if you need:
noti.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
mNotificationManager.notify(SIMPLE_ID, mBuilder.build());
}
to update the text:
private void showText() {
String head = yournewhead;
String body = yournewbody;
mBuilder.setContentTitle(head);
mBuilder.setContentText(body);
mNotificationManager.notify(SIMPLE_ID, mBuilder.build());
}
Per the docs you can do the following:
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());
The trick here is your notify id, which must be the same in order to behave appropriately, in your case 0.
So as long it has not been dismissed it should work accordingly. I will suggest having a unique id as a constant for your custom notification such as 123.
notificationManager.notify(0, builder.build());

How to display notifications on Android application?

I need display notifications when my server return specific values. I have a service running on android and taskschedule running every time sending request to server, when the server return positive value i need display message on celular display, similar receive message of whatsapp (display icon and notification on display). Anyone have a sample?
I trying this:
PendingIntent resultPendingIntent = PendingIntent.getActivity(
this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
But my application is running as a service.
Please use Using Service to run background and create notification
private void processStartNotification() {
// Do something. For example, fetch fresh data from backend to create a rich notification?
final NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle("Scheduled Notification")
.setAutoCancel(true)
.setColor(getResources().getColor(R.color.colorAccent))
.setContentText("This notification has been triggered by Notification Service")
.setSmallIcon(R.drawable.notification_icon);
PendingIntent pendingIntent = PendingIntent.getActivity(this,
NOTIFICATION_ID,
new Intent(this, NotificationActivity.class),
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
builder.setDeleteIntent(NotificationEventReceiver.getDeleteIntent(this));
final NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(NOTIFICATION_ID, builder.build());
}

NotificationCompat and setFullScreenIntent()

I was wondering if anyone has some experience with with this type of Notification.
In my use case I want to trigger a notification from my Service and than it should open a fullscreen video. The method setFullScreenIntent look just the right thing for this problem because in the documentation it writes:
An intent to launch instead of posting the notification to the status bar. Only for use with extremely high-priority notifications demanding the user's immediate attention, such as an incoming phone call or alarm clock that the user has explicitly set to a particular time.
So it says it works like an incoming phone call. That means even if my phone is asleep I should get to see the notification in full screen.
But in my case I just get the heads-up notification and if I click on it it opens the activity. Even thought in the docs they mention something about this behavior ...
On some platforms, the system UI may choose to display a heads-up notification, instead of launching this intent, while the user is using the device.
... I want to know how to automatically open the activity when the notification is triggered. Just like the incoming phone call screen.
This is my code from the service:
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Intent notificationIntent = new Intent("android.intent.category.LAUNCHER");
intent.setClassName("com.example.test",
"com.example.test.VideoActivity");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(contentIntent)
.setContentTitle("Video")
.setContentText("Play video")
.setFullScreenIntent(contentIntent, true);
NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
return Service.START_STICKY;
}
try to notify notification with setFullScreenIntent at onCreate
#Override
public int onCreate() {
Intent notificationIntent = new Intent("android.intent.category.LAUNCHER");
intent.setClassName("com.example.test",
"com.example.test.VideoActivity");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(contentIntent)
.setContentTitle("Video")
.setContentText("Play video")
.setFullScreenIntent(contentIntent, true);
NotificationManager mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
}
Try removing
.setContentIntent(contentIntent)

Notification does not work properly on some devices

In my app I use a service for background work(send data to server) when my app is in the background.So I create a notification which tells to the user that app is running in the background.I want when the user taps on the notification my app to come to the foreground so I use an Intent like this that android use to launch my app
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.final_driver_notification_icon)
.setContentTitle("...")
.setContentText("...");
final Intent notintent = new Intent(getApplicationContext(),EntryScreen.class);
notintent.setAction(Intent.ACTION_MAIN);
notintent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent pendint = PendingIntent.getActivity(this, 0, notintent, 0);
mBuilder.setContentIntent(pendint);
int notID = 001;
startForeground(notID, mBuilder.build());
In the emulator and some physical devices works perfect.But in some other it launches the app from the start(It puts Entry screen in the top).
Any idea why is this happening?
You should start your service with startForeground(). this is the way to start a service in foreground and report by a notification. Android - implementing startForeground for a service?
or try something like this:
public void initForeground() {
Intent intentForeground = new Intent(this, Activity.class);
intentForeground.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intentForeground,0);
pendingIntent.cancel();
pendingIntent = PendingIntent.getActivity(this, 0, intentForeground,0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.icon)
.setContentIntent(pendingIntent)
.setContentTitle(getString(R.string.app_name))
.setContentText(getString(R.string.something))
.setTicker(getString(R.string.something))
.setAutoCancel(false);
Notification notification = mBuilder.build();
startForeground(myID, notification);
}
Hope it helps you!

How to add a Dynamic image instead of notification icon in android?

I used below code for displaying notification in notification Bar.
it worked fine.
But i need to display notification icon dynamically that will come from web service.
How i can do?
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification note = new Notification(R.drawable.image,"status message", System.currentTimeMillis());
Intent in = new Intent(Notify.this,CommentU.class);
PendingIntent pi = PendingIntent.getActivity(Notify.this, 0, in, 0);
note.setLatestEventInfo(Notify.this,"NotificationTitle", "You have a new Commentio", pi);
note.number = ++count;
note.vibrate = new long[] { 500l, 200l, 200l, 500l };
note.flags |= Notification.FLAG_AUTO_CANCEL;
nm.notify(NOTIFY_ME_ID, note);
Thanks in Advance.
I have one suggestion for you: you have to download that image which you want to show and then you can set that as bitmap: check below code. I have created one BITMAP.
its look link :
for this you have to add android-support-v4.jar
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My notification").setLargeIcon(BITMAP)
.setContentText("Hello World!");
Intent resultIntent = new Intent(this, test.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(test.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(NOTIFY_ME_ID, mBuilder.build());
for more detail chekc this link.
Removing notifications
Notifications remain visible until one of the following happens:
The user dismisses the notification either individually or by using "Clear All" (if the notification can be cleared).
The user clicks the notification, and you called setAutoCancel() when you created the notification.
You call cancel() for a specific notification ID. This method also deletes ongoing notifications.
You call cancelAll(), which removes all of the notifications you previously issued.
Edited: just replace this.
mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My notification").setLargeIcon(BITMAP)
.setAutoCancel(true)
.setContentText("Hello World!");
just add some icons in your resource folder and then
int myIcon = R.drawable.image;
your logic goes here ....
and change value of icon according to your logic like myIcon = R.drawable.somenewimage;
and then finally set your notification
Notification note = new Notification(myIcon,"status message", System.currentTimeMillis());

Categories

Resources