0

I am trying to get a simple notification. I've seen lots of examples using old API methods, but i want to use the newer method. I have put this code together from the API resource, and it runs without error. However, i am not seeing any notifications. I have the minimum required to view a notification, at least, i believe so from what i have read. Here is my code :

public class Login extends Activity { public static Context ctx; public void onCreate(Bundle savedInstanceState) { ctx = this; } private static void notice(String msgfrom, String msg) { NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(ctx); mBuilder.setSmallIcon(R.drawable.ic_launcher); mBuilder.setContentTitle("New message from " + msgfrom.toString()); mBuilder.setContentText(msg); mBuilder.build(); } } 

THANKS TO HELP BELOW, I MODIFIED MY CODE TO THIS

NotificationCompat.Builder nb = new NotificationCompat.Builder(ctx); nb.setSmallIcon(R.drawable.ic_launcher); nb.setContentTitle("New message from " + msgfrom.toString()); nb.setContentText(msg); nb.setAutoCancel(true); Notification notification = nb.build(); NotificationManager NM = (NotificationManager) ctx.getSystemService(NOTIFICATION_SERVICE); NM.notify(0, notification); 

3 Answers 3

2

enter image description here

If you want to run Notification in All Android device <= android 4.2 then just add android-support-v4.jar file in you Project and

use below code:

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.notification_icon) .setContentTitle("My notification") .setContentText("Hello World!"); // Creates an explicit intent for an Activity in your app Intent resultIntent = new Intent(this, ResultActivity.class); // The stack builder object will contain an artificial back stack for the // started Activity. // This ensures that navigating backward from the Activity leads out of // your application to the Home screen. TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); // Adds the back stack for the Intent (but not the Intent itself) stackBuilder.addParentStack(ResultActivity.class); // Adds the Intent that starts the Activity to the top of the stack stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent( 0, PendingIntent.FLAG_UPDATE_CURRENT ); mBuilder.setContentIntent(resultPendingIntent); NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); // mId allows you to update the notification later on. mNotificationManager.notify(mId, mBuilder.build()); 

this code is work in android 2.1 to 4.2.

for more details check this and this links.

Sign up to request clarification or add additional context in comments.

1 Comment

Most excellent.. Thank you... I shorted down my code for my situation, but it was your code that got me there!
1

You are missing the last step. You are building your notification, and you still need to post it to the system, in the following manner :

( (NotificationManager) ctx.getSystemService ( Context.NOTIFICATION_SERVICE ) ).notify ( 100 , mBuilder.build () ); 

EDIT :

The integer 100 (that I chose randomly) used in the notify method is a unique identifier for the notification. You should use the same identifier if you ever need to update or cancel your notification.


NOTE :

You might have a memory leak by storing your activity's contact in a static variable ! Instead of that, you can change this :

private static void notice(String msgfrom, String msg) 

to this

private static void notice(Context ctx , String msgfrom, String msg) 

and hence delete the static variable.

2 Comments

Thanks, yea, i derived that from 'Dhaval Sodha Parmar' response. It's a learning curve...
As for you suggestion to not storing my activity in a static variable. Good suggestion - I will try that.
0

Try this

Just call inside your onCreate()

 Public void Player_Notification() { final String myBlog = "http://android-er.blogspot.com/"; notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); myNotification = new Notification(R.drawable.ic_launcher,"Audio Track is playing in background!",System.currentTimeMillis()); Context context = getApplicationContext(); // here is title String notificationTitle = Title; // here is sub title String notificationText = data; // write your activity name which you want to open when user click on notification Intent myIntent = new Intent(this, Activty.class); PendingIntent pendingIntent = PendingIntent.getActivity(Activty.this, 0, myIntent, Intent.FLAG_ACTIVITY_NEW_TASK); // intent will call your activity as you want myNotification.defaults |= Notification.DEFAULT_SOUND; myNotification.flags |= Notification.FLAG_AUTO_CANCEL; myNotification.setLatestEventInfo(context, notificationTitle, notificationText, pendingIntent); notificationManager.notify(MY_NOTIFICATION_ID, myNotification); } 

1 Comment

this isn't using the NotificationCompat

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.