A notification is a message you can display to the user outside of your application's normal UI. When you tell the system to issue a notification, it first appears as an icon in the notification area. To see the details of the notification, the user opens the notification drawer. Both the notification area and the notification drawer are system-controlled areas that the user can view at any time.
What are we going to do?
Today, we will make an android notification and we will use an intent to respond to the user if the notification clicked.
1.
Notice the Notification (yeah bro) |
3.
responds when the notification clicked |
MainActivity.java
import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; public class MainActivity extends Activity { private int count=0; private static final int NOTIFICATION_ID = 1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // notifyMgr=(NotificationManager)getSystemService(NOTIFICATION_SERVICE); Notification n = new Notification(R.drawable.yourownpicturehere, getString(R.string.noticeMe), System.currentTimeMillis()); PendingIntent i=PendingIntent.getActivity(this, 0, new Intent(this, NotifyActivity.class), 0); n.setLatestEventInfo(getApplicationContext(), getString(R.string.title), getString(R.string.message), i); n.number=++count; n.flags |= Notification.FLAG_AUTO_CANCEL; n.flags |= Notification.DEFAULT_SOUND; n.flags |= Notification.DEFAULT_VIBRATE; n.ledARGB = 0xff0000ff; n.flags |= Notification.FLAG_SHOW_LIGHTS; // Now invoke the Notification Service String notifService = Context.NOTIFICATION_SERVICE; NotificationManager mgr = (NotificationManager) getSystemService(notifService); mgr.notify(NOTIFICATION_ID, n); } }NotifyActivity.java
import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class NotifyActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView txtdisplay=new TextView(this); txtdisplay.setText("Yeah you did it!"); setContentView(txtdisplay); } }Strings.xml
Don't forget to add this in your Manifest before the :hey you really great Yeah bro! you are excited, Yeah!