Question

I'm not able fire the notification to the notification bar on click of a button. Some thing that is to be done seems to be missing from my code. Any help would be appreciated. Thanks.

Code:

public class MainActivity extends Activity { private int numMessages = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

public void onClickNotify(View view){


    final int notificationID = 100;
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
    mBuilder.setContentTitle("Notification Alert, Click Me!");
    mBuilder.setContentText("Hi, This is Android Notification Detail!");
    mBuilder.setTicker("New Message Alert!");

    mBuilder.setNumber(++numMessages );

    Intent resultIntent = new Intent(this, NotificationView.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(NotificationView.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);

        // notificationID allows you to update the notification later on.
        mNotificationManager.notify(notificationID, mBuilder.build());
}

}

Was it helpful?

Solution

According to android http://developer.android.com/guide/topics/ui/notifiers/notifications.html

you might missed setSmallIcon()

Creating a Notification


You specify the UI information and actions for a notification in a NotificationCompat.Builder object. To create the notification itself, you call NotificationCompat.Builder.build(), which returns a Notification object containing your specifications. To issue the notification, you pass the Notification object to the system by calling NotificationManager.notify().

Required notification contents

A Notification object must contain the following:

• A small icon, set by setSmallIcon() 
• A title, set by setContentTitle() 
• Detail text, set by setContentText() 

hope it helps

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top