F
F
Fotonick2016-03-03 00:26:24
Android
Fotonick, 2016-03-03 00:26:24

Why is my app giving more notifications than it should?

In my application, after loading the phone, the time is set in the calendar for calling a reminder

public class BootUpReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        Intent myIntent = new Intent(context , NotifyService.class);
        AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        PendingIntent pendingIntent = PendingIntent.getService(context, 0, myIntent, 0);
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 22);
        calendar.set(Calendar.MINUTE, 23);
        calendar.set(Calendar.SECOND, 00);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60 * 1000, pendingIntent);  //set repeating every 24 hours


    }

}

which calls the reminder service
public class NotifyService extends Service {

    NotificationManager nm;

    @Override
    public void onCreate() {
        super.onCreate();
        nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        sendNotif();
        return super.onStartCommand(intent, flags, startId);
    }


    void sendNotif() {

        Intent intent = new Intent(this, SplashActivity.class);
        PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);


        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
                .setContentTitle("UpgradeIM")
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentIntent(pIntent)
                .setContentText("Пора написать отчёт за день")
                .setAutoCancel(true);


        //add sound
        Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        mBuilder.setSound(uri);

        //vibrate
        long[] v = {500,1000};
        mBuilder.setVibrate(v);

        nm.notify(3, mBuilder.build());

    }

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

According to my plans, this should be only one notification at 22:23, but the notification appears 4-5 times a day at a completely random time and once at the appointed time.
What have I done wrong? How can I see all the reminders from applications installed for the day on my phone?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Zagaevsky, 2016-03-03
@zagayevskiy

Looks okay. Or maybe you, while testing, set several alarms? Try calling cancel() with your intent. Reinstalling the app should help too. As far as I know, there is no api for getting all alarms.

K
Konstantin Dovnar, 2016-03-03
@SolidlSnake

AlarmManager starting from Android 4 (or 3?) is generally very strange and unpredictable.
In my application, I sweated a lot so that it worked once at the right time.
Here 's a little example without repetition, in the setNotification method .
Here 's a little example with repetitions, in the setUpdateRepeat method .
From what I remember:
You can get a list of all alarms via adb using the command:
adb shell dumpsys alarm

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question