Saturday , October 20 2018
Home / Android / Android Notifications

Android Notifications

A notification is a message you can display to the user outside of your application’s normal UI. Android allows to put notification into the titlebar of your application. The user can expand the notification bar and by selecting the notification the user can trigger another activity.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.

Hear is the complete code of create and sending notification.

1) MainActivity.java

package com.example.notificationdemo;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class MainActivity extends ActionBarActivity {
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

b1=(Button)findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Notify(“You’ve received new message”);
}
});
}
private void Notify(String notificationTitle, String notificationMessage){
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
@SuppressWarnings(“deprecation”)

Notification notification = new Notification(R.drawable.abc,”New Message”, System.currentTimeMillis());
Intent notificationIntent = new Intent(this,NotificationView.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0);

notification.setLatestEventInfo(MainActivity.this, notificationTitle,notificationMessage, pendingIntent);
notificationManager.notify(9999, notification);
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.

int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
2) res/layout/notification.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent” >

<TextView
android:layout_width=”fill_parent”
android:layout_height=”400dp”
android:text=”Hi, Your Detailed notification view goes here….” />
</LinearLayout>
3) NotificationView.java

package com.example.notificationdemo;

import android.os.Bundle;
import android.app.Activity;

public class NotificationView extends Activity{
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.notification);
}
}

4) res/layout/activity_main.xml

<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:paddingBottom=”@dimen/activity_vertical_margin”
android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
tools:context=”MainActivity”>

<TextView
android:id=”@+id/textView1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Notification Example”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:textSize=”30dp” />

<TextView
android:id=”@+id/textView2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Tutorials point ”
android:textColor=”#ff87ff09″
android:textSize=”30dp”
android:layout_below=”@+id/textView1″
android:layout_centerHorizontal=”true”
android:layout_marginTop=”48dp” />

<ImageButton
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:id=”@+id/imageButton”
android:src=”@drawable/abc”
android:layout_below=”@+id/textView2″
android:layout_centerHorizontal=”true”
android:layout_marginTop=”42dp” />

<Button
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Notification”
android:id=”@+id/button”
android:layout_marginTop=”62dp”
android:layout_below=”@+id/imageButton”
android:layout_centerHorizontal=”true” />

</RelativeLayout>
5) res/values/strings.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<resources>
<string name=”action_settings”>Settings</string>
<string name=”app_name”>w2class </string>
</resources>

6) AndroidManifest.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.example.notificationdemo”
android:versionCode=”1″
android:versionName=”1.0″ >

<application
android:allowBackup=”true”
android:icon=”@drawable/ic_launcher”
android:label=”@string/app_name”
android:theme=”@style/AppTheme” >

<activity
android:name=”com.example.notificationdemo.MainActivity”
android:label=”@string/app_name” >

<intent-filter>
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>

</activity>

<activity android:name=”.NotificationView”
android:label=”Details of notification”
android:parentActivityName=”.MainActivity”>
<meta-data
android:name=”android.support.PARENT_ACTIVITY”
android:value=”.MainActivity”/>
</activity>

</application>
</manifest>

Previews

notify1

 

notify2

About admin

Check Also

Android Activity Launch Modes

Launch mode is an instruction for Android OS which specifies how the activity should be ...

Leave a Reply