mercredi 1 juillet 2015

Android: Notification is displaying more than once

I need to show a notification at 3 AM everyday. It is working fine and sending notification around 3 AM. But when I test in my phone the notification comes immediately. I read that if the time is already crossed it will show the notification. I am fine with this. Issue: When i click the notification it opens my activity correctly. But after few seconds it is loading another notification. If i click that again it is closing the notification and creating a new one after few seconds.

Here is the code: 1. In the activity onCreate()

Calendar calendar = Calendar.getInstance();
// calendar.set(Calendar.MONTH, 10);
// calendar.set(Calendar.YEAR, 2014);
// calendar.set(Calendar.DAY_OF_MONTH, 13);
calendar.set(Calendar.HOUR_OF_DAY, 3);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM, Calendar.AM);

Intent myIntent = new Intent(LitCalActivity.this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(LitCalActivity.this, 0, myIntent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

In My service Class

public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);

        // Code to notification setup
        Intent litrcalintent = new Intent(this, LitCalActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("FromNotify", "YES");
        litrcalintent.putExtras(bundle);
        PendingIntent pIntent = PendingIntent.getActivity(this, 0,
                litrcalintent, PendingIntent.FLAG_UPDATE_CURRENT);

        SimpleDateFormat format = new SimpleDateFormat("MMMM dd yyyy");
        String today = format.format(currentDate);
        String todayDetails = getLitCalDetails();

        Notification notification = new Notification.Builder(this)
                .setContentTitle("Liturgical Calendar for " + today)
                .setContentText(todayDetails)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentIntent(pIntent)
                .setAutoCancel(true)
                .setOngoing(false)
                .addAction(R.drawable.ic_launcher, "more...", pIntent).build();

        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        notificationManager.notify(0, notification);


    }

Aucun commentaire:

Enregistrer un commentaire