Displaying a notification in android - android

I am using the following code to show a notification in my android application, but using this, with the notification an activity also shows up (i.e. a black blank screen) I dont want this screen, but just a simple notification and when the user clicks the notification then I want to launch the activity. What should be done to this code?
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notify = new Notification(R.drawable.mcube_logo, "Message Sent", Calendar.getInstance().getTimeInMillis());
Intent intent = new Intent(RecieveAlarm.this, otherActivity.class);
PendingIntent pendingIntent =PendingIntent.getActivity(RecieveAlarm.this, 1, null, PendingIntent.FLAG_CANCEL_CURRENT);
notify.setLatestEventInfo(RecieveAlarm.this, "Title", "Details", pendingIntent);
notificationManager.notify(0, notify);

I suggest you to use:
Notification not = new Notification(idIcon, text, System.currentTimeMillis());
PendingIntent pInt = PendingIntent.getActivity(ctx, 0, new Intent(ctx, the_class_to_call), 0);
not.setLatestEventInfo(ctx, app_name, text, pInt);
and then not.notify....

use AlertDialog, then launch your desired activity on positive button click

Related

Notification is not being dismissed (Android)

Notification setAutoCancel(true) doesn't work if clicking on Action
I have a notification with an action within it. When I tap on the notification it gets removed from the list. However, when I click on the Action it successfully completes the Action (namely makes a call), but when I return to the list of notifications, it remains there.
Relative code of the AlarmReceiver:
public class AlarmReceiver extends BroadcastReceiver {
Meeting meeting;
/**
* Handle received notifications about meetings that are going to start
*/
#Override
public void onReceive(Context context, Intent intent) {
// Get extras from the notification intent
Bundle extras = intent.getExtras();
this.meeting = extras.getParcelable("MeetingParcel");
// build notification pending intent to go to the details page when click on the body of the notification
Intent notificationIntent = new Intent(context, MeetingDetails.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
notificationIntent.putExtra("MeetingParcel", meeting); // send meeting that we received to the MeetingDetails class
notificationIntent.putExtra("notificationIntent", true); // flag to know where the details screen is opening from
PendingIntent pIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
// build intents for the call now button
Intent phoneCall = Call._callIntent(meeting);
if (phoneCall != null) {
PendingIntent phoneCallIntent = PendingIntent.getActivity(context, 0, phoneCall, PendingIntent.FLAG_CANCEL_CURRENT);
int flags = Notification.FLAG_AUTO_CANCEL;
// build notification object
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
Notification notification = builder.setContentTitle("Call In")
.setContentText(intent.getStringExtra("contextText"))
.setTicker("Call In Notification")
.setColor(ContextCompat.getColor(context, R.color.colorBluePrimary))
.setAutoCancel(true) // will remove notification from the status bar once is clicked
.setDefaults(Notification.DEFAULT_ALL) // Default vibration, default sound, default LED: requires VIBRATE permission
.setSmallIcon(R.drawable.icon_notifications)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(meeting.description))
.addAction(R.drawable.icon_device, "Call Now", phoneCallIntent)
.setCategory(Notification.CATEGORY_EVENT) // handle notification as a calendar event
.setPriority(Notification.PRIORITY_HIGH) // this will show the notification floating. Priority is high because it is a time sensitive notification
.setContentIntent(pIntent).build();
notification.flags = flags;
// tell the notification manager to notify the user with our custom notification
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
}
}
}
use this flag:
Notification.FLAG_AUTO_CANCEL
inside this:
int flags = Notification.FLAG_AUTO_CANCEL;
Notification notification = builder.build();
notification.flags = flags;
Documentation
Ok turns out it's a known problem already, and it needs extra code to be done (keeping reference to notification through id). Have no clue why API does not provide this, as it seems very logical to do. But anyways,
see this answer in stackoverflow:
When you called notify on the notification manager you gave it an id - that is the unique id you can use to access it later (this is from the notification manager:
notify(int id, Notification notification)
To cancel, you would call:
cancel(int id)
with the same id. So, basically, you need to keep track of the id or possibly put the id into a Bundle you add to the Intent inside the PendingIntent?
I faced this problem today, and found that FLAG_AUTO_CANCEL and setAutoCanel(true), both work when click on notification,
but not work for action click
so simply, in the target service or activity of action, cancel the notification
NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancelAll();
or if have more notification
manager.cancel(notificationId);
You have created two pending intent use in boths and change Flag too.
PendingIntent pIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis(), notificationIntent, 0);
PendingIntent phoneCallIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis(), phoneCall, PendingIntent.FLAG_UPDATE_CURRENT);
// CHANGE TO THIS LINE
PendingIntent pIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
PendingIntent phoneCallIntent = PendingIntent.getActivity(context, 0, phoneCall, PendingIntent.FLAG_CANCEL_CURRENT);

when to clear notification count of status notification in android

I have a method for creating notification. I want to clear notification number as soon as a user clicks on status notification.
public void createNotification(){
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification=null;
notification = new Notification(R.drawable.ic_launcher, "You have a new message", System.currentTimeMillis());
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.number = **count**++;
Intent intent = new Intent();
intent.setClass(TabInterfaceActivity.this, TabInterfaceActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent activity = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.contentIntent = activity;
notification.setLatestEventInfo(this, " New Message", message, notification.contentIntent);
}
If by "I want to clear notification number" you mean you want to keep the notification itself still displayed, but you want just to update/remove counter, then you should set your notification PendingIntent to point to your code that would handle this scenario for you, and when it completes it shall redirect to your target TabInterfaceActivity (or you can make it more flexible and pass target in PendingIntent's Bundle).

Flagging issues in android

I'm trying to get my Notification to not cancel when the user presses "Clear All" So far I have the intent working properly on everything except this:
Intent intent = new Intent(getBaseContext(), MainActivity.class);
intent.addFlags(Notification.FLAG_ONGOING_EVENT);
intent.addFlags(Notification.FLAG_NO_CLEAR);
PendingIntent contentIntent = PendingIntent.getActivity(
getBaseContext(), 0, intent, 0);
The question I have at this point is: Are my flags correct?
Yes, your flags look pretty much correct, although I don't know if you even need the FLAG_NO_CLEAR. I currently have an app which creates an ongoing (non-cancellable) notification - I only use the FLAG_ONGOING_EVENT and it works fine for me. I pretty much just copied it from a tutorial and then added the ongoing event flag.
Here's some sample code:
String text = "notification";
Notification notification = new Notification(R.drawable.icon, text,
System.currentTimeMillis());
//launch the activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent().setComponent(ComponentName.unflattenFromString("com.example/com.example.MyActivity")), 0);
// set the label and text...
notification.setLatestEventInfo(this, getText(R.string.notification_label),
text, contentIntent);
notification.flags = Notification.FLAG_ONGOING_EVENT;
// Send the notification.
// We use a string id because it is a unique number. We use it later to cancel.
NotificationManager mNM;
mNM.notify(R.string.notification_label, notification);

Click notification that takes you to a particular class

What I have below is some notification code. What I would like to achieve with my notification code is when the notification is clicked, it will take the user to a particular class. Here is my notification code:
final int NOTIF_ID = 1234;
NotificationManager notifManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification note = new Notification(R.drawable.icon, "New Offers CLOSEBY!", System.currentTimeMillis());
PendingIntent intent = PendingIntent.getActivity(this, 0, new Intent(this, intentTHING.class), 0);
note.setLatestEventInfo(this, "You have 5 new offers", "Please CLICK THIS Notification to see what is avaliable!", intent);
notifManager.notify(NOTIF_ID, note);
I'm assuming that when the notification is clicked, this code here is supposed to take you to the declared class:
PendingIntent intent = PendingIntent.getActivity(this, 0, new Intent(this, intentTHING.class), 0);
However, it does not, the notification does appear, but I cannot see the class I have referenced in my code (i.e when clicked it does not take me to the class I want it to). I have the notification popup currently after a button has been pressed in another class. Once the user clicks on the notification it should push that screen and replace it with the intentTHING.class. Any ideas as to what I may be missing?
Set pending Intent like this:
PendingIntent intent = PendingIntent.getActivity(this, 0, new Intent(this, intentTHING.class), 0);
note .contentIntent = intent;
note.setLatestEventInfo(this, "You have 5 new offers", "Please CLICK THIS Notification to see what is avaliable!", intent);
notifManager.notify(NOTIF_ID, note);
Am not sure but can try it out by doing this..
PendingIntent intent =
PendingIntent.getActivity(context, 0,
new Intent(this, intentTHING.class), PendingIntent.FLAG_UPDATE_CURRENT | Notification.FLAG_AUTO_CANCEL);

Android Notifications

Im trying to start a new activity once i press a notification...the related code is:
NotificationManager notifManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification note = new Notification(R.drawable.android, "New E-mail", System.currentTimeMillis());
PendingIntent intent = PendingIntent.getActivity(this, 0, new Intent(this, DatabaseActivity.class), 0);
note.setLatestEventInfo(this, "New E-mail", "You have one unread message.", intent);
notifManager.notify(NOTIF_ID, note);
But the activity just dsnt begin..the notification pops up..but if i click it nothin happens...plz advice!!!
Pardon the obvious question but is DatabaseActivity referenced in your manifest?
May be you can use getApplicationContext() instead of this in the pendingIntent definition.

Categories

Resources