I'm developing my first android app for a client. The launch is scheduled on Saturday, everything works all right... but one thing.
I need the app to connect to the client's website once every 3 days, download events' info, then show a notification and (in some cases) send an email.
What is happening right now is that the app doesn't show any notification, but sends multiple mails every day instead: that should mean that the Alarm fires multiple times per day instead of once every 3.
In my manifest, i have these two receivers:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
[...]
<receiver android:name=".Helper.AlarmDownload"/>
<receiver
android:name=".Helper.AlarmBootReceiver"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
AlarmDownload.java does many thing. They all work, so i won't annoy you with the full code. Just the end:
Intent intentToRepeat = new Intent(context, Caricamento.class);
intentToRepeat.putExtra(Costanti.string_notif_idcorso,notifica.getInt(Costanti.sp_notifiche_idcorso,0));
//set flag to restart/relaunch the app
intentToRepeat.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//Pending intent to handle launch of Activity in intent above
PendingIntent pendingIntent =
PendingIntent.getActivity(context, NotificationHelper.ALARM_TYPE_RTC, intentToRepeat, PendingIntent.FLAG_UPDATE_CURRENT);
//Build notification
String titolonotifica = notifica.getString(Costanti.sp_notifiche_titolonotifica,"");
String subtitolo = notifica.getString(Costanti.sp_notifiche_subtitolo,"");
Notification repeatedNotification = buildLocalNotification(context, pendingIntent,titolonotifica,subtitolo).build();
//Send local notification
NotificationHelper.getNotificationManager(context).notify(NotificationHelper.ALARM_TYPE_RTC, repeatedNotification);
[...]
public NotificationCompat.Builder buildLocalNotification(Context context, PendingIntent pendingIntent, String titolo, String subtitolo) {
NotificationCompat.Builder mBuilder;
mBuilder = new NotificationCompat.Builder(context, "fisicamente")
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.icona_app)
.setContentTitle(titolo)
.setContentText(subtitolo)
//.setStyle(new NotificationCompat.BigTextStyle()
//.bigText(testo))
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setCategory(NotificationCompat.CATEGORY_EVENT);
return mBuilder;
}
So, the real issue should be in NotificationHelper.java:
public class NotificationHelper {
public static int ALARM_TYPE_RTC = 100;
private static AlarmManager alarmManager1;
private static PendingIntent alarmIntent1;
public static void scheduleRepeatingRTCNotification(Context context) {
SharedPreferences pref_login = context.getSharedPreferences(Costanti.SP_LOGIN, Context.MODE_PRIVATE);
Boolean primavolta = pref_login.getBoolean("primavolta",false);
//primavolta means first time
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
Random r1 = new Random();
int casuale1 = r1.nextInt(100);
calendar.set(Calendar.HOUR_OF_DAY, 11);
calendar.set(Calendar.MINUTE, casuale1);
if (primavolta && calendar.before(Calendar.getInstance())) calendar.add(Calendar.DAY_OF_MONTH, 1);
//I don't want to show the notification on the first login of the user, so i'm adding one day to the calendar.
Intent intent = new Intent(context, AlarmDownload.class);
alarmIntent1 = PendingIntent.getBroadcast(context, ALARM_TYPE_RTC, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager1 = (AlarmManager)context.getSystemService(ALARM_SERVICE);
alarmManager1.setInexactRepeating(AlarmManager.RTC,
calendar.getTimeInMillis(), 3 * AlarmManager.INTERVAL_DAY, alarmIntent1);
}
This is what i do in MainActivity.java, the first time the user logs in:
Boolean primavolta = preferenze.getBoolean("primavolta",false);
if (primavolta) {
//Apre il drawer
drawer.openDrawer(GravityCompat.START);
//Notifiche
NotificationHelper.scheduleRepeatingRTCNotification(getApplicationContext());
}
The drawer opens only during the first login, so the schedulerepeatingRTCNotification void doesn't fire more than once.
Any idea?
Thank you very much in advance!
Edited: there was a reference to a costant instead of 3 in the code.