Notification do not dismiss till it clicked by the user - android

I am using the below code to generate the notification in my application
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = MainActivity.this;
notifManager = (NotificationManager) mContext.getSystemService(mContext.NOTIFICATION_SERVICE);
mNotification = new NotificationCompat2.Builder(mContext).setSmallIcon(android.R.drawable.sym_def_app_icon)
.setTicker("Launch download").setContentTitle("Downloader").setContentText(content)
.setContentIntent(getPendingIntent());
mNotification.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
mNotification.setAutoCancel(true);
notifManager.notify(UPDATE_PROGRESS, mNotification.build());
}
private PendingIntent getPendingIntent() {
Intent i = new Intent(mContext, NotificationReceiver.class);
//i.setFlags(FLAG_ACTIVITY_CLEAR_TOP);
return PendingIntent.getActivity(mContext, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
}
Note:- I am using NotificationCompat2 by Jake Wharton.
Now this code works fine except when a new a Notification arrives it dismisses the old notification even if it is not read by the user.
My Question
How to show all the notification in the status slidingdrawer till it is not read by the user?

A notification will be replaced by another if they have the same id. Change the id in
notifManager.notify(id, mNotification.build());
to display several notifications.

Related

Notification "setContentInfo" not show up

I'm using Android Studio. I wanna add message with number of notifications. Why when I run app there is no content info on the screen?
>
private int i = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void notyfikacja(View view) {
Intent intent = new Intent(this, SecondActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder
.setSmallIcon(android.R.drawable.arrow_up_float)
.setContentInfo("You have"+ ++i + "messages" )
.setContentText("ContentText")
.setContentTitle("ContentTitle")
.setAutoCancel(true)
.setContentIntent(pendingIntent);
Notification notification = builder.build();
NotificationManagerCompat.from(this).notify(0, notification);
}
I add the Notification Screeshot
I am not sure about why the setContentInfo doesn't work. Probably cause you are trying in Nougat ? Anyways, according to the documentation, it is recommended to use setSubText(CharSequence) instead of setContentInfo(CharSequence info)
From Doc:
This method was deprecated in API level 24. use setSubText(CharSequence) instead to set a text in the header. For legacy apps targeting a version below N this field will still show up, but the subtext will take precedence.

Multiple notification handling

I'm having trouble in figuring out the Intent, PendingIntent Filters and Flags for notifications.
Notification are working and are getting generated as they should, but the problem is only the last notification created keeps the Bundle Data.
I want all notification to keep the Bundle Data of each notification in it until a user click on them.
Consider an application for a moment when different user send you a message a new notification gets created and when you click on any notification the app launches and takes you to some specific Activity. I want the same thing but when there are more than one notification the last notification keeps the Data where as previous notification loose their Bundle Data and Intent.
There is another thing which Filters to use to restrict the app from launching a new instance of MainActivity everytime a notification is clicked at.
The Notification_ID are different for each notification.
public class AlarmSchedulingService extends IntentService {
private NotificationManager mNotificationManager;
public AlarmSchedulingService() {
super("SchedulingService");
}
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
sendNotification(extras.getInt(KEY_EXTRAS_NOTIFICATION_ID));
}
public void sendNotification(int NOTIFICATION_ID) {
mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra(keyName, extraData);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
// use the right class it should be called from the where alarms are set
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this)
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(titleString)
.setStyle(
new NotificationCompat.BigTextStyle()
.bigText(messageString))
.setDefaults(
Notification.DEFAULT_SOUND
| Notification.DEFAULT_LIGHTS)
.setContentText(messageString);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
}
This is Showing that :
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
You are giving request code 0 to all notifications.
0 should be replaced by each unique number, otherwise, each new notification will override the old one.

Switch navigation tabs manually in Navigation drawer in Android

I am using latest Lollipop style Navigation drawer in my app.Please refer to this example for more info on that.I use Fragments to show different navigation tabs. Now, I need to open, let's say the 5th item in drawer when I click a certain notification from the notification bar in android device. I am stuck in as to how to switch directly to that Fragment by clicking notification. I am very much aware as how this can be done using Activity. Can anyone please suggest me any solution regarding this ?
Thanks in Advance.
Resolved:
I have resolved this issue by following Ziem's answer. I have just added following lines to open it as a new screen and clear older activity stack:
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
You can add PendingIntent to notification's click:
PendingIntent resultPendingIntent;
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
...
.setContentIntent(resultPendingIntent);
Next you need to handle notification's Intent inside your activity.
Example:
// How to create notification with Intent:
Intent resultIntent = new Intent(this, MainActivity.class);
resultIntent.putExtra("open", 1);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setContentIntent(resultPendingIntent);
int mNotificationId = 33;
NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(mNotificationId, mBuilder.build());
//How to handle notification's Intent:
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (getIntent() != null && getIntent().hasExtra("open")) {
int fragmentIndexToOpen = getIntent().getIntExtra("open", -1)
// show your fragment
}
}
}

How can I modify a textview from a non-activity class? Android

I want to modify a TextView from an alarmmanager class's onReceive method this class does not extend activity. The alarm sends an intent that creates a notification and I want to make it so clicking the notification will open a textview with a particular string that I get in this onReceive method.
RemoteViews contentView = new RemoteViews(context.getPackageName(),R.layout.notify_layout);
contentView.setTextViewText(R.id.notetext,my_message);
notif.contentView = contentView;
I tried to do this using the RemoteView above with the notification intent but it doesn't seem to work.
I know I can't access this using findViewById, but what approach should I take here?
This is from the code that handles the intent
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.notify_layout);
NotificationManager nm = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE); nm.cancel(getIntent().getExtras().getInt("com.myapp.NotificationDisp"));
Then I have this whole notification thing here. I know that its deprecated and I should be using builder but I need this to be compatible back to gingerbread which it seems builder is not.
Intent i = new Intent("com.MyApp.NotificationDisp");
i.putExtra("com.MyApp.NotificationDisp", 1);
nm = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
#SuppressWarnings("deprecation")
Notification notif = new Notification(R.drawable.ic_launcher,
"Myapp", System.currentTimeMillis());
//
RemoteViews contentView = new RemoteViews(context.getPackageName(),
R.layout.notify_layout);
contentView.setTextViewText(R.id.notetext,
my_message);
notif.contentView = contentView;
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
i, 0);
notif.contentIntent = contentIntent;
//
notif.setLatestEventInfo(context, "Myapp", my_message, contentIntent);
notif.vibrate = new long[] { 100, 250, 100, 500};
nm.notify(1, notif);
So this brings up a notification with the correct text in my status bar but when I click on it it only shows a blank textview. The xml notify_layout should be setup fine.

Custom status bar notification for background download

I am new to android development and I try to create a background download feature for my app. I followed this http://developer.android.com/guide/topics/ui/notifiers/notifications.html#CustomExpandedView to create my custom notification.
The downloading is performed, I checked the downloaded file in the sdcard. Also,the status bar icon and title are changed properly.
The problem is that the custom layout I provide for the notification does not appear (expand under the bar). Here is the related code parts inside private AsyncTask class:
#Override
protected void onPreExecute() {
// create and configure the notification
notification = new Notification(R.drawable.download, "Downloading map..", System.currentTimeMillis());
notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
//create a custom layout for the notification
myContentView = new RemoteViews(appContext.getPackageName(), R.layout.download_progress);
myContentView.setImageViewResource(R.id.status_icon, R.drawable.ic_menu_save);
myContentView.setTextViewText(R.id.status_text, "download in progress");
myContentView.setProgressBar(R.id.status_progress, 100, 0, false);
notification.contentView = myContentView;
notification.contentView.apply(appContext, dl.getListView());
//instantiate the pending intent
Intent myIntent = new Intent(appContext, DownloadList.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
int requestID = (int) System.currentTimeMillis();
PendingIntent myPendingIntent = PendingIntent.getActivity(appContext, requestID, myIntent, 0);
notification.contentIntent = myPendingIntent;
//add the Notification object to the notification manager
notificationManager = (NotificationManager) appContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIF_ID, notification);
}
#Override
protected void onProgressUpdate(Integer... progress) {
//update progress bar
notification.contentView.setProgressBar(R.id.status_progress, 100, progress[0], false);
notificationManager.notify(NOTIF_ID, notification);
}
}
Note that my DownloadList class extends ListActivity.
Do I need to do something more that just "notification.contentView = myContentView;" in order to inflate the layout?
Hmm... Well I compared your code to my code that already works... and I don't see many differences... But, it is possible that one of these minor differences is important.
final Notification notification = new Notification(R.drawable.icon, "Downloading", System.currentTimeMillis());
notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
notification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.download_progress);
notification.contentView.setImageViewResource(R.id.status_icon, R.drawable.ic_status);
notification.contentView.setTextViewText(R.id.status_text, "Downloading in progress");
notification.contentView.setProgressBar(R.id.status_progress, 100, progress, false);
Intent notificationIntent = new Intent(MainPage.mainActivity, MainPage.class);
PendingIntent contentIntent = PendingIntent.getActivity(MainPage.mainActivity, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
//getApplicationContext();
final NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(
Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_MESSAGE, notification);
First, I looked at your old code and noticed that the NOTIF_ID = 1 I'm not so sure that is a good idea because what if someone else has an ID of one. Of course I could be mistaken about that, but I just pounded in a number like 792489743 and I expect no one else would have the same number. Just a precaution I suppose.
Second, I didn't get to see if the resources were correct? What does the stack trace say? I suppose that it would've just quit out on it if there was a problem there though.
Third, I put my in its own task as Service kinda as follows
public class DownloadService extends IntentService {
//initializing code and stuff
private class DownloadTask extends AsyncTask<String, Void, Boolean> {
and I did it in the doInBackground This way if the user kills the app or what not it wouldn't kill the download.
Lastly, I've never used apply I don't personally see how it would hurt, but I haven't seen an example that uses it either.
Hope this helps some!
It was an emulator problem after all.....
It lagged when I "dragged down" the notification! I killed some CPU extensive processes on my PC resulting to a faster emulator.
Lesson learned. Leave the heavy multitasking to pros or to another PC.

Categories

Resources