Search code examples
androidservicenotificationsbackground-process

How to get notifications even app was closed in android


I am developing an application that trigger notifications for every five seconds but i need to do this even when the app was closed or killed like the whats app and gmail notifications...To run the notifications i am using the service but when i was closing or killing the app notifications was not coming can any one tell me how to do this This is what i had done This is my activity:

    package com.example.servicesandroid;


    import java.util.Timer;
    import java.util.TimerTask;

    import android.app.Activity;
    import android.app.ActivityManager;
    import android.app.ActivityManager.RunningServiceInfo;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.os.Handler;
    import android.view.View;
    import android.widget.Button;


    public class MainActivity extends Activity {

            Timer timer;
            Handler handler;
            TimerTask timer_task;

         public void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             handler = new Handler();
             setContentView(R.layout.activity_main);
             Button buttonStartService = (Button)findViewById(R.id.startservice);
             Button buttonStopService = (Button)findViewById(R.id.stopservice);
             timer = new Timer();
             timer_task = new TimerTask() {

                @Override
                public void run() {

                    Intent intent = new Intent(MainActivity.this, Androidservice.class);
                      MainActivity.this.startService(intent);
                }
            };
timer.schedule(timer_task, 5,5000);

This is my Service:

package com.example.servicesandroid;


import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.IBinder;

public class Androidservice extends Service {

    final static String ACTION = "NotifyServiceAction";
    final static String STOP_SERVICE = "";
    final static int RQS_STOP_SERVICE = 1;

    NotifyServiceReceiver notifyServiceReceiver;

    private static final int MY_NOTIFICATION_ID = 1;
    private NotificationManager notificationManager;
    private Notification myNotification;
    private final String myBlog = "http://sample.com/";

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        notifyServiceReceiver = new NotifyServiceReceiver();
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ACTION);
        registerReceiver(notifyServiceReceiver, intentFilter);

        // Send Notification
        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        myNotification = new Notification(R.drawable.ic_launcher,
                "Notification!", System.currentTimeMillis());
        Context context = getApplicationContext();
        String notificationTitle = "Notification!";
        String notificationText = "http://sample.com/";
        Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(myBlog));
        PendingIntent pendingIntent = PendingIntent.getActivity(
                getBaseContext(), 0, myIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
        myNotification.defaults |= Notification.DEFAULT_SOUND;
        myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
        myNotification.setLatestEventInfo(context, notificationTitle,
                notificationText, pendingIntent);
        notificationManager.notify(MY_NOTIFICATION_ID, myNotification);

        return  Service.START_STICKY;
    }



    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        this.unregisterReceiver(notifyServiceReceiver);
        super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    public class NotifyServiceReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context arg0, Intent arg1) {
            // TODO Auto-generated method stub
            int rqs = arg1.getIntExtra("RQS", 0);
            if (rqs == RQS_STOP_SERVICE) {
                stopSelf();


            }
        }
    }

}

Solution

  • You seem to start the service every time you fire the timer. Instead, shift the timer logic to the Service and start the service Once only. If you need more clarifications, pls ask.