First of all you need a broadcast receiver (inside its own class, plus a bit of code in the manifest) that reacts to the USER_PRESENT action (when user unlocks screen) or the BOOT_COMPLETED (when cellphone finishes loading the OS and all other stuff). Boot activates the Broadcast, the broadcast starts the service. Inside the onStartCommand() method of your service, you run your connection to the webservice every X minutes. Your service should Implement AsyncTask to connect to the Web service. In the onCompleted() method of that task, you call the notifications.
MANIFEST:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver android:name="classes.myReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<service android:name="classes.myService">
</service>
CLASS:
public class myReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context ctx, Intent intent) {
Intent service = new Intent(ctx, myService.class);
if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)||intent.getAction().equals(Intent.ACTION_USER_PRESENT)||intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
ctx.startService(service);
}
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
ctx.stopService(service);
}
}
}
SAMPLE NOTIFICATION
private void showNotification() {
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationBuilder = new NotificationCompat.Builder(this);
notificationBuilder.setContent(getNotificationContent());
notificationBuilder.setOngoing(true);
notificationBuilder.setTicker("Hello");
notificationBuilder.setSmallIcon(R.drawable.notif_icon);
mNotificationManager.notify(R.id.notification_layout, notificationBuilder.build());
}
private RemoteViews getNotificationContent() {
RemoteViews notificationContent = new RemoteViews(getPackageName(), R.layout.keyphrase_recogniser_notification);
notificationContent.setTextViewText(R.id.notification_title, "title");
notificationContent.setTextViewText(R.id.notification_subtitle, "subtitle");
return notificationContent;
}
This is a broad guide, if you need more specific code let us know.