0

I want send notification in my app every day (dialy with conditon) at 10:30:30, and when this value of condition (NOTIFY_SHOW) bigger from zero (0) notification send (for user) else dont send notification.

and i use this code in my app: this class for Service (alarm manager) :

public class DiaryAlarmService extends Service {

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @SuppressWarnings({"static-access", "deprecation"})
    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        try {
            intent = new Intent(this, AccountActivity.class);
            PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);
            Notification notification = new Notification.Builder(this).setContentTitle("سالگرد خاطره")
                    .setContentText("امروز سالگرد" + " " + NOTIFY_SHOW + " " + "خاطره ثبت شده می باشد.").setSmallIcon(R.mipmap.ic_launcher).setContentIntent(pIntent).build();
            NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
            notificationManager.notify(0, notification);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}

and this class for Broadcast Receiver:

public class DiaryReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        Intent service1 = new Intent(context, DiaryAlarmService.class);
        context.startService(service1);
    }
}

and this method in my activity for call service and alarm manager :

private void getReminderCount() {
            if (NOTIFY_SHOW != 0) {
                Calendar calendar = Calendar.getInstance();
                calendar.set(Calendar.HOUR_OF_DAY, 10);
                calendar.set(Calendar.MINUTE, 30);
                calendar.set(Calendar.SECOND, 30);
                Intent myIntent = new Intent(MainActivity.this, DiaryReceiver.class);
                PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent, 0);
                AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

and in call method in onCreat activity - now send notification daily when "NOTIFY_SHOW" id bigger from zero and NOTIFY_SHOW = 0 ! and send notification several time! how to put condition on notification !?

ghasem deh
  • 698
  • 1
  • 10
  • 26
  • It's better if you use JobScheduler see https://stackoverflow.com/questions/41811893/android-job-periodic-once-a-day-deviceidle-true-as-soon-as-possible –  Sep 19 '17 at 13:20
  • i dont use another lib in my app ! – ghasem deh Sep 19 '17 at 14:22
  • JobScheduler is part of Android SDK, but available from API 21 –  Sep 19 '17 at 18:52

0 Answers0