Reputation: 69
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);
Upvotes: 0
Views: 1243
Reputation: 4383
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.
Upvotes: 1
Reputation: 18978
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.
Upvotes: 2
Reputation: 16142
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);
}
Upvotes: 0