I'm looking for this answer for a few days already on the web. I'm really new to both android java and stackoverflow.
So what I want to do is to send a reminder everyday at 12:30 to remind my user to use the App (The notification should be send without having the application running, if it's possible). What I've learned from this website is that I have to create a notifyService class which extends Service.
I've tried every solutions I found, and I've never recieved any notification, so I guess either my NotifyService class is wrong, or I'm calling it in a bad way.
I've tested my creation of the notification (without using notifyservice) on the mainActivity and linked it on a button click and it worked fine, so I guess the problem comes from elsewhere.
Here my NotifiyService class :
public class NotifyService extends Service{
private int NOTIFICATION_ID = 2;
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
private Handler notificationTimer = new Handler() {
@Override
public void handleMessage(Message msg) {
if(msg.what==0) {
sendNotifications();
}
super.handleMessage(msg);
}
};
private Runnable notificationCaller = new Runnable() {
@Override
public void run() {
Message msg = notificationTimer.obtainMessage();
msg.what = 0;
notificationTimer.sendMessage(msg);
}
};
public void sendNotifications(){
Calendar calendar = Calendar.getInstance();
Calendar timer = Calendar.getInstance();
timer.set(Calendar.HOUR_OF_DAY, 12);
timer.set(Calendar.MINUTE, 30);
timer.set(Calendar.SECOND, 00);
Long difference = calendar.getTimeInMillis() - timer.getTimeInMillis();
if(difference<0){
notificationTimer.postDelayed(notificationCaller, -(difference));
}
else{
notificationTimer.postDelayed(notificationCaller, difference);
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://developer.android.com/reference/android/app/Notification.html"));
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.ic_stat_notification);
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
builder.setContentTitle("Pensez à vérifier votre glycémie !");
builder.setContentText("Bonjour jeune Padawan, il est temps de penser à contrôler ta glycémie.");
builder.setSubText("Click pour ouvrir Diab'App :)");
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, builder.build());
notificationTimer.removeCallbacksAndMessages(null);
notificationTimer.postDelayed(notificationCaller, 86400000);
}
}
And here is my mainActivity : (I won't link all of it because most of it has nothing to deal with the problem here)
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
/* some code for my database init and layouts */
Intent service = new Intent(this, NotifyService.class);
this.startService(service);
}
I'm not sure what the problem is here, is it because I call the startService on the onCreate function, so I have to launch my app to get notify ?
Thanks !