Wednesday, 11 June 2014

Simple PushNotification in ANdroid Programmatically

when I click the button, notification will come on status bar in android mobile..

Ex: 1
//---------------------------------------------------------------------------------------------
package com.example.testing1;
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.view.View.OnClickListener;
import android.widget.Button;

public class MainActivityPushNotification extends Activity {
   
    NotificationManager notifManagerObject ;
    private String title = "Testing Notification Title", body = "Push Notification Messages" ;
    Button btnOne ;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.push_notification);
       
        btnOne = (Button) findViewById(R.id.button1) ;
       
       
        btnOne.setOnClickListener(new OnClickListener() {
            @SuppressWarnings("deprecation")
            @Override
            public void onClick(View v) {
               
                notifManagerObject = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE) ;
               
                @SuppressWarnings("deprecation")
                Notification nofity = new Notification(R.drawable.ic_launcher, title, 5) ;
               
                PendingIntent pendIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0) ;
               
                nofity.setLatestEventInfo(MainActivityPushNotification.this, title, body, pendIntent) ;
               
                notifManagerObject.notify(0, nofity) ;
               
               
            }
        }) ;
       
    }
}
//-----------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="Button" />

</RelativeLayout>

No comments:

Post a Comment