How to use Notification in Android?
Android represent the Notifications by using Notification class.We have to create a notification by using NotificationManager class,it can be received from the Activity via getSystemService() method.
Eg: NotificationManager notificationManager = (NotificationManager)getSystemService (NOTIFICATION_SERVICE);
Here i use two edit text for giving the email and message and one button for create a notification.
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/username_et"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:inputType="text"/>
<EditText
android:id="@+id/mess_et"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp"
android:inputType="text" />
<Button
android:id="@+id/button1"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:onClick="createNotification"
android:text="Create Notification" android:layout_gravity="center_horizontal"/>
</LinearLayout>
Another xml for receive the notification message UI, name as notification_receiver.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is the result activity opened from the notification" >
</TextView>
</LinearLayout>
Java class name NotificationActivity.java
public class NotificationActivity extends Activity {
EditText username_et,mess_et;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void createNotification(View view) {
username_et=(EditText)findViewById(R.id.username_et);
mess_et=(EditText)findViewById(R.id.mess_et);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.mss,"A new notification", System.currentTimeMillis());
// Hide the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Intent intent = new Intent(this, NotificationReceiver.class);
PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);
notification.setLatestEventInfo(this, username_et.getText().toString(), mess_et.getText().toString(),activity);
notification.number += 1;
notificationManager.notify(0, notification);
}
}
and finally NotificationReceiver.java class.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notification_receiver);
}
Really advisory and effective assemblage.
ReplyDeleteone click root software
Very informative blog.Thanks for share it.
ReplyDeleteandroid mobile application
Awesome post Android training course in Delhi, Noida, Gurgaon, Mumbai. Its so much informative for the followers. I like the way you describe this post. Its really helpful for the users of this site. I am also searching about these type of sites now a days. So your site really helps me for searching the new and great stuff. This site contain all my specifications. Thanks for sharing. Keep it up. I am waiting for your more posts like this or related to any other informative topic.
ReplyDelete