Kumar KL
Kumar KL

Reputation: 15335

Push_Notification on Android with WebServices

I have a scenario which always checking with the web services. If there is a changes in the DataBase then it should pop-up a alert message . I have done With a WHILE LOOP.

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Push_notificaton();
        }

public void Push_notificaton(){
    while(true)
    {
        try
        {
SoapObject request = new SoapObject("http://tempuri.org/","getMessage");

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);

        //Web method call
        HttpTransportSE androidHttpTransport = new HttpTransportSE("http://180.215.66.194/WebService.asmx");
        androidHttpTransport.call("http://tempuri.org/"+ "getMessage", envelope);
        //get the response
        SoapPrimitive response = (SoapPrimitive)envelope.getResponse();

        //the response object can be retrieved by its name: result.getProperty("objectName");
        String message = (String)response.toString();
        if(!currentMsg.equals(message))
        {
        currentMsg=message;
        AlertDialog.Builder alertDialogBuilder = new    AlertDialog.Builder(this);

            // set title
            alertDialogBuilder.setTitle("Your Title");

            // set dialog message
            alertDialogBuilder
                .setMessage("Click yes to exit!")
                .setCancelable(false)
                .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {
                        // if this button is clicked, close
                        // current activity
                        MessagesRequestActivity.this.finish();
                    }
                  })
                .setNegativeButton("No",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {
                        // if this button is clicked, just close
                        // the dialog box and do nothing
                        dialog.cancel();
                    }
                });

                // create alert dialog
                AlertDialog alertDialog = alertDialogBuilder.create();

                // show it
                alertDialog.show();
        alertDialog.setIcon(R.drawable.ic_launcher);
        alertDialog.show();
        }
        Toast.makeText(this, message, Toast.LENGTH_LONG).show();

        }
        catch (Exception e)
        {
        e.printStackTrace();
        }

But here I'm unable to get the output because of continuous looping. Let me know any other way to overcome from this.

Upvotes: 1

Views: 1032

Answers (2)

Nezam
Nezam

Reputation: 4132

You should use cloud services which would automatically push notifications to your android application which may trigger a particular implementation or handle.

You should have a look at GCM (Google Cloud Messaging).

What is GCM?

Google Cloud Messaging for Android (GCM) is a service that allows you to send data from your server to your users' Android-powered device. This could be a lightweight message telling your app there is new data to be fetched from the server (for instance, a movie uploaded by a friend), or it could be a message containing up to 4kb of payload data (so apps like instant messaging can consume the message directly). The above link shows you the steps which you need to go through for configuration of your app with google.

Apparently,go through this tutorial for mySQL and php which successfully sets up a app for GCM use

Why use GCM and its advantages?

  • It would rid you of the constant checking with your server for db changes.It causes a huge performance issue in your app.
  • Basically,what you would be doing is automatically call a piece of code whenever there's a change in the database.More feasibly,not only in modification of data,this can be used elsewhere too.So you won't have to bother over checking the server everytime.Google takes that headache.

If you're using asp.net.You would use this piece of code to trigger the push

Remember, GCM implementation is very easy and can be quickly implemented.

Upvotes: 2

tundundun
tundundun

Reputation: 644

  1. Don't do any request to the web-service in ui-thread. Move your code to Service or AsyncTask. The former is better.

  2. This approach is energy consuming. At least, try to make requests with sleep(someTime).

Upvotes: 2

Related Questions