0

I want to get notifications from the server at fixed interval so I have set that server response into the service. So, basically I m using Services to run background and create notification. My problem is that I have got notifications not at the fixed interval. So how can get the notifications at the fixed interval. I also want to continue my service (get notifications)even if I restart my phone. I m little bit confused that if the phone will be switched off then I have to restart my service or not. If I have to restart it then how can I handle this. Your help will be appreciated. Thanks in advance.

Alert_notifications.java
public class Alert_notifications extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alert_notifications);
        Button buttonStartService = (Button)findViewById(R.id.startservice);
        Button buttonStopService = (Button)findViewById(R.id.stopservice);

        buttonStartService.setOnClickListener(new Button.OnClickListener(){

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(Alert_notifications.this, com.example.gpstracking.NotifyService.class);
                Alert_notifications.this.startService(intent);
            }});

        buttonStopService.setOnClickListener(new Button.OnClickListener(){

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent intent = new Intent();
                intent.setAction(NotifyService.ACTION);
                intent.putExtra("RQS", NotifyService.STOP_SERVICE);
                sendBroadcast(intent);
            }});

    }
}



NotifyService.java


public class NotifyService extends Service {

    final static String ACTION = "NotifyServiceAction";
    final static String STOP_SERVICE = "";
    final static int RQS_STOP_SERVICE = 1;
    private static final String url_Weather_details1="http://198.168.1.2/Weatherforecast1/";
    private static final String TAG_SUCCESS = "success";


    NotifyServiceReceiver notifyServiceReceiver;

    private static final int MY_NOTIFICATION_ID = 1;
    private NotificationManager notificationManager;
    private Notification myNotification;
    private final String myBlog = "http://android-er.blogspot.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) {
        ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
        JSONParser jsonParser = new JSONParser();
        params.add(new BasicNameValuePair("LAT", "LAT"));
        params.add(new BasicNameValuePair("LONGITUDE", "LONG"));
        Log.d("params", params.toString());
        // getting weather details by making HTTP request
        // Note that weather details url will use GET request
        JSONObject json = jsonParser.makeHttpRequest(url_Weather_details1,
                "GET", params);
        // check your log for json response
        Log.d("Weather Details", json.toString());

        // json success tag
        int success = 0;
        try {
            success = json.getInt(TAG_SUCCESS);
            System.out.println("success"+success);
        } 
        catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (success == 2) {
            // successfully received weather details

        // 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 = "Heavy Rain!";
        String notificationText = "";
        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 super.onStartCommand(intent, flags, startId);

    }


    @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();
            }
        }
    }

}
bhagyasri patel
  • 61
  • 1
  • 11
  • If you are firing notification from server, then you can set cronjob for it and set/schedule the interval. Please let me know if this idea helps or you need more clarification. – sUndeep Feb 10 '15 at 09:08

1 Answers1

0

I would recommend to use GCM to deliver "push"-notifications to Android clients or the clients could alternatively do a network call and check if there are new notifications. To take care that the device falls not asleep during the work of the Service, you should also take a look on WakefulBroadcastReceiver or acquire a WakeLock. If you're interested in restarting your Service after a boot, check also this answer to receive the ACTION_BOOT_COMPLETED system event on your BroadcastReceiver, in which you can restart the Service.

Community
  • 1
  • 1
momo
  • 3,313
  • 2
  • 19
  • 37