How to add push notification in android app?

Android Push Notification

A notification is a message you can display to the user outside of your application's normal UI. Android provides NotificationManager class for this purpose. In order to use this class, you need to instantiate an object of this class by requesting the android system through getSystemService() method.

NotificationManager NM;
NM=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

After that you will create Notification through Notification class and specify its attributes such as icon, title and time e.t.c.

Notification notify=new Notification(android.R.drawable.stat_notify_more,title,System.currentTimeMillis());

The next thing you need to do is to create a PendingIntent by passing context and intent as a parameter.

PendingIntent pending=PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0);

The last thing you need to do is to call setLatestEventInfo method of the Notification class and pass the pending intent along with notfication subject and body details. And then finally call the notify method of the NotificationManager class.

notify.setLatestEventInfo(getApplicationContext(), subject, body,pending);
NM.notify(0, notify);

Java Snippet

public class MainActivity extends Activity
{
NotificationManager NM;
EditText one,two,three;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
one = (EditText)findViewById(R.id.editText1);
two = (EditText)findViewById(R.id.editText2);
three = (EditText)findViewById(R.id.editText3);
}
@SuppressWarnings("deprecation")
public void notify(View vobj)
{
String title = one.getText().toString();
String subject = two.getText().toString();
String body = three.getText().toString();
NM=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Notification notify=new Notification(android.R.drawable.stat_notify_more,title,System.currentTimeMillis());
PendingIntent pending=PendingIntent.getActivity(
getApplicationContext(),0, new Intent(),0);
notify.setLatestEventInfo(getApplicationContext(),subject,body,pending);
NM.notify(0, notify);
}
}

XML Snippet

<RelativeLayoutxmlns: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" >
<Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="86dp"
        android:onClick="notify"
        android:text="@string/notification" />
<EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="53dp"
        android:ems="10" />
<EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="28dp"
        android:ems="10" />
<EditText
        android:id="@+id/editText3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText2"
        android:layout_below="@+id/editText2"
        android:layout_marginTop="23dp"
        android:ems="10" />
<TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/editText1"
        android:layout_marginRight="14dp"
        android:layout_toLeftOf="@+id/editText1"
        android:text="@string/title" />
<TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/editText3"
        android:layout_alignRight="@+id/textView1"
        android:text="@string/heading" />
<TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/editText3"
        android:layout_alignLeft="@+id/textView2"
        android:text="@string/body" />
<TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/textView2"
        android:text="@string/create"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    </RelativeLayout>

Notification With Buttons

Intent i = new Intent(this,Next.class);
PendingIntentpIntent = PendingIntent.getActivity(this, 0, i, 0);
Notification n  = new Notification.Builder(this)
        .setContentTitle("New mail from " + "android@gmail.com")
        .setContentText("Sample Test Notification")
        .setSmallIcon(R.drawable.icon)
        .setContentIntent(pIntent)
        .setAutoCancel(true)
        .addAction(R.drawable.icon, "Call", pIntent)
        .addAction(R.drawable.icon, "More", pIntent)
        .addAction(R.drawable.icon, "And more", pIntent).build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, n);
public void show(View v)
{
    Intent i = new Intent(this,ActivityOne.class);
PendingIntent pi = PendingIntent.getActivity(this,0,i,0);
    Uri ref = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    Notification n = new Notification.Builder(this)
            .setContentTitle("FB Message")
            .setContentText("Message From Group")
            .setSound(ref)
            .setAutoCancel(true)
            .setSmallIcon(R.drawable.myicon)
            .setLights(Color.BLUE, 500, 500)
            .addAction(R.drawable.myicon,"Open",pi)
            .build();
NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
nm.notify(0,n);
}

Visibility Configuration

The application can control the level of detail visible in the notifications displayed in a secure lock screen. Called setVisibility() and you specify one of the following values:

VISIBILITY_PUBLIC displays the full content of the notification.
VISIBILITY_SECRET does not display any part of this notification on the lock screen.
VISIBILITY_PRIVATE displays basic information such as the icon and the title of the content of the notification, but hides the full content.

I hope it helps! Thanks for being here, and see you next blog post!
Comment via Facebook
0 Comment via Google

0 تعليقات:

Post a Comment