Android Notifications - android

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.

Related

Notification PendingIntent from BroadcastReceiver

guys.
I've been debugging this for days, but I can't find an answer for this.
As the title says, I am using BroadcastReceiver to make my notification.
Everything was fine until I add .addAction.
It needs PendingIntent for default and I made a PendingIntent.
But the problem is this; when I add .addAction, It says this
Incompatiable types.
Required : android.app.Notification
Found : android.app.Notification.Builder
the code works perfectly fine with out the .addAction and the PendingIntent.
What could be the problem?
Here is my code,
BroadcastReceiver, WifiService.java
Intent intent = new Intent(mcontext, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pIntent = PendingIntent.getActivity(mcontext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification noti = new Notification.Builder(mcontext)
.setContentTitle("Welcome home!")
.setContentText("You have a to-do; " + hometodo)
.setSmallIcon(R.drawable.home_light)
.setLargeIcon(icon)
.setLights(0xFFFFD800, 5000, 0)
.addAction(R.drawable.archive_notification, "Mark as done", pIntent);
//.addAction gets the error.
.build();
NotificationManager notificationManager = (NotificationManager) mcontext.getSystemService(mcontext.NOTIFICATION_SERVICE);
notificationManager.notify(0, noti);
used NotificationCompat for setting .addAction(...).
Check official docs
action buttons won't appear on platforms prior to Android 4.1. Action
buttons depend on expanded notifications, which are only available in
Android 4.1 and later.

Perform an action clicking notifications in android

I am trying to create notifications correctly, but as I can not perform an action when you click on the notification. I hope you can help me or give some clue.
It is recommended to use Notification.Builder and NotificationCompat.Builder from the support library for building notifications.
Also why do use reuse the Intent which was caught by your BroadcastReceiver? It should refer to the actual activity which you want to launch by click.
For example:
Intent intent = new Intent(context, MainActivity.class);
Notification notification = new NotificationCompat.Builder(context)
.setContentTitle(contentTitle)
.setContentText(contentText)
.setContentIntent(PendingIntent.getActivity(context, 0, intent, 0))
.setAutoCancel(true)
.build();
If you doesn't really want to launch some activity, you may start a Service or send a broadcast message instead. See PendingIntent.
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notif = new Notification(R.drawable.ic_launcher, "Text",
System.currentTimeMillis());
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("somekey", "someextra");
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
notif.setLatestEventInfo(this, "Title", "Message", pIntent);
notif.flags |= Notification.FLAG_AUTO_CANCEL;
nm.notify(1, notif);
Use Notification.Builder for new version api

Displaying a notification in 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

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 show notification from BroadcastReceiver

I have a class which extends BroadcastReceiver that gets called whenever new Wifi scan results are available (the receiver is registered in the manifest with the Scan_Results broadcast as the intent-filter).
From this class, I want to be able to show a notification to the user. Currently, I pass the context that is received as a parameter in the onReceive method of my broadcast intent class to a "show notification" method of another class.
When it gets to the line:
myNotificationManager.notify(notificationId, notification);
it fails with the following exception:
java.lang.IllegalArgumentException: contentView required: pkg=com.mumfordmedia.trackify id=2131034122 notification=Notification(vibrate=null,sound=null,defaults=0x0,flags=0x0)
Any idea why this is happening? All I can think of is because the context that I am getting from the onReceive parameter is not ... for lack of a better phrase, "right for the job"...
Any ideas?
Thanks,
Max.
ContentView is the view which is required when the notification is clicked. The code below works fine and setLatestEventInfo() is required method.
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher,
"Hello from service", System.currentTimeMillis());
Intent intent = new Intent(this, MainActivity.class);
notification.setLatestEventInfo(this, "contentTitle", "contentText",
PendingIntent.getActivity(this, 1, intent, 0));
manager.notify(111, notification);
Not sure exactly why it wasnt working before but here is the code I got it working with:
Declare the following outside of any method:
int YOURAPP_NOTIFICATION_ID = 1234567890;
NotificationManager mNotificationManager;
Then in the onReceive method call the following:
mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
showNotification(context, R.drawable.icon, "short", false);
Then declare the following method:
private void showNotification(Context context, int statusBarIconID, String string, boolean showIconOnly) {
// This is who should be launched if the user selects our notification.
Intent contentIntent = new Intent();
// choose the ticker text
String tickerText = "ticket text";
Notification n = new Notification(R.drawable.icon, "ticker text", System.currentTimeMillis());
PendingIntent appIntent = PendingIntent.getActivity(context, 0, contentIntent, 0);
n.setLatestEventInfo(context, "1", "2", appIntent);
mNotificationManager.notify(YOURAPP_NOTIFICATION_ID, n);
}
You have to call Notification.setLatestEventInfo().
Use this code along with your notification
Intent intent = new Intent(this, MusicDroid.class);
PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);
notification.setLatestEventInfo(this, "This is the title",
"This is the text", activity);
notification.number += 1;
nm.notify(NOTIFY_ID, notification);
From what I can tell, when you're creating you notification to pass to the Notification manager, you're not giving it a content view to display. Review the line where you actually create the notification to see if you're actually giving the notification a view to display.
For those who are using NotificationCompat, the following code will work:
NotificationCompat.Builder n = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.icon).setContentText("Notify Title").setContentText("Sample Text");
NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Intent i = new Intent(this,MainActivity.class);
PendingIntent ac = PendingIntent.getActivity(this, 0, i, 0);
n.setContentIntent(ac);
nm.notify(12222, n.build());

Categories

Resources