I am currently working on an android project and I am trying to create a notification from a standard java class (not an activity class).
I've added a icon into the drawable folder called icon.png but when I try and set the icon to an int using R.drawable.icon icon never shows up only the default built in android icons.
I don't understand why it is not showing my own icons.
Thanks for any help you can provide.
UPDATE
As requested below is the code for setting up the notification
private void showNotification(String username, String password)
{
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager notificationManager = (NotificationManager)context.getSystemService(ns);
CharSequence tickerText = "Username Copied";
long when = System.currentTimeMillis();
int icon = 0;
Notification notification = new Notification(icon, tickerText, when);
CharSequence contentTitle = "BPM - Login Management";
CharSequence contentText = "Username Copied";
Intent intentNotification = new Intent();
intentNotification.setAction(Long.toString(when));
notification.flags |= Notification.FLAG_AUTO_CANCEL;
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intentNotification, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
final int NOTIFICATION_ID = 1;
notificationManager.notify(NOTIFICATION_ID, notification);
}
I've only set icon = 0 just to stop it from giving me errors. I know I need to use R.drawable in someway to get the actual icon ID. Just not sure why this isn't working
Check your imports.
You want to import COM.YOURAPP.R, not android.R
I am not sure why I can't add comment to this question. Anyway have you tired to click on project menu and then click Clean..? then open R.java file and see if your icon is listed there?
Try this :
int icon = R.drawable.icon;
instead of
int icon =0;
Related
For some reason, I don't get to use android-support-library in my code. In order to perform a Notification that matches Android 4.0 style, I write this:
private void noti() {
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
int icon = R.drawable.ic_launcher;
CharSequence text = "Notification Text";
CharSequence contentTitle = "Notification Title";
CharSequence contentText = "Sample notification text.";
long when = System.currentTimeMillis();
Intent intent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification notification = new Notification(icon,text,when);
notification.setLatestEventInfo(this, contentTitle, contentText, contentIntent);
notificationManager.notify(1, notification);
}
How to control the progress bar in it? The way in developer.google.com is using Notification.Builder, but Builder is in the android-support-library.
I found another way to do this is
notification.setProgressBar(int viewId, int max, int progress, boolean indeterminate)
But another problem is that it needs a custom notification layout which contains a progress bar. If I use a custom layout, it won't match android 4.0 style.
What can I do now? Please help me, Thanks!
I solved the problem by using a custom layout witch came from android source code here , so it looks like a system Notification. Hope this can help others.
I am working on an Android application.The app is like native sms application.I am facing a problem in notification part. In native app, they handle the notification in the following way.
1)if messages from an specific number ,then the click on the notification will lead to the chat page of the corresponding contact and the notification will clear.
2)if messages from the different number, then the click on the notification will lead to home page and notification won't clear.
I had done the first part and I don't have any idea to do the second part.
Is there any way to leave the notification part without clear and call an intent.?
you have to add a flag:
notification.flags |= Notification.FLAG_ONGOING_EVENT;
here a whole example:
private static final int NOTIFICATION_EX = 0;
private NotificationManager notificationManager;
...
in onCreate:
notificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
int icon = R.drawable.youricon;
CharSequence tickerText = "Sticky notification";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
Context context = getApplicationContext();
CharSequence contentTitle = "Sticky notification";
CharSequence contentText = "...click here and it wont go away...";
Intent notificationIntent = new Intent(this, mainmenu.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this,
0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle,
contentText, contentIntent);
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notificationManager.notify(NOTIFICATION_EX, notification);
the intent you can adjust to whatever you like.
You will have to create two different kind of notification, the first one you have already implemented, and the second one you can just copy the first one and check if the number is a different one use the second notification and set the flags to not clear notification2.flags |= Notification.FLAG_ONGOING_EVENT; on click with the HomeActivity in the PendingIntent which has Intent:
I want to notify user of mobile as particular event take place,Using notification bar icon.
Is there any way to do the same?
use this method where you required.its worked for me properly.
Hope it will solve your problem.
private void notification(Context c)
{
// TODO Auto-generated method stub
EfficientAdapter1 e1=new EfficientAdapter1(c);
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) e1.no(ns);
//int icon = R.drawable.xyz;//your image
CharSequence tickerText = "ticker text";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, Text, when);
Context context = c;
CharSequence contentTitle = "your title";
CharSequence contentText = " your text";
Intent notificationIntent = new Intent(context, ViewAllSMS.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
final int HELLO_ID = 1;
notification.flags = Notification.FLAG_INSISTENT | Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(HELLO_ID, notification);
}
Use the Android NotificationManager (http://developer.android.com/reference/android/app/Notification.html)
Refer the app dev guide example - http://developer.android.com/guide/topics/ui/notifiers/notifications.html
After setting the required properties and calling the notify method, the status bar would show up the notification which you require.
Omkar Ghaisas
You can always go for pop ups. they are neat and gives you chance to notify the user. Other wise you can use toast also. Just the problem in toast is that it appears for some time and then disappears, so if somehow user misses that toast he may never know about toast. or say notification. You can see example here
I am developing an Android app and I want a status bar notification that cannot be cleared by the user. Does anyone know how to do that? I've seen them with apps like Skimble where a non-clearable notification appears while using the application.
Also, I'd when the user clicks/presses the notification, I want it to return to the app instance that is already running. Right now it starts a new instance. Again like the Skimble app, I just want to return to the already running instance of the app.
Thanks.
I found a solution to this problem. I used FLAG_ONGOING_EVENT to make the notification persist. Here's the code:
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.mypic;
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
notification.flags = Notification.FLAG_ONGOING_EVENT;
Context context = getApplicationContext();
CharSequence contentTitle = "Title Text";
CharSequence contentText = "Content text.";
Intent intent = new Intent(this, MyClass.class);
intent.setAction("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
final int HELLO_ID = 1;
mNotificationManager.notify(HELLO_ID, notification);
Here is the solution for the NotificationCompat.Builder
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("MyApp")
.setContentText("MyMsg")
.setOngoing(true);
More at the Google docs.
notification.setLatestEventInfo is deprecated so you have to use the Notification.builder and if your using 2.X and lower API you have to use NotificationComacpt.Builder and this is not applicable and I couldn't make my notification stays. so I used setDeleteIntent and pointed to the same activity and on resume method just call the block of code which shows your notification. So it is kinda work around.
I would like to be able to fire a notification to alert the users about a timer that has finished, however i do not wish to have an intent when you click the notification.
I've tried passing in null for the intent
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);
int icon = R.drawable.icon;
CharSequence tickerText = "Hello";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
CharSequence contentTitle = "My notification";
CharSequence contentText = "Hello World!";
notification.setLatestEventInfo(context, contentTitle, contentText, null);
mNotificationManager.notify(HELLO_ID, notification);
You may pass the parameter
PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0)
instead of
null
on
notification.setLatestEventInfo(context, contentTitle, contentText, null);
The last parameter in setLatestEventInfo() is a PendingIntent and not an Intent. If you need the notification to not do anything when tapped is to pass an empty PendingIntent which is done as follows: PendingIntent.getActivity(context, 0, null, 0).
Interesting question and I would like to see if this works. I did a little digging and found a lot of ppl asking the same question. cbursk seems to have found a hack to get this intended functionality, which is to pass a Dialog to the notification intent instead of an Activity. I'm guessing the Dialog does nothing, or is programmed to dismiss itself right away, not sure. But I'm currently looking at this thread and going to test it out.