Search code examples
androidnotificationsbroadcastreceiversharedpreferencesalarmmanager

Can't control notification manager android


I having an application that should send notifications everyday at exact time. It's okey with triggering at time, but every time I close app notification appears, when it musn't.

Notification class:

 @Override
public int onStartCommand(Intent intent,int flags, int startId)
{
    super.onStartCommand(intent, flags, startId);
    Context context = getApplicationContext();
    Intent resultIntent = new Intent(getApplicationContext(), Love.class);

    PendingIntent contentIntent = PendingIntent.getActivity(context,
            0, resultIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);
    /* Invoking the default notification service */
    NotificationCompat.Builder  mBuilder = new NotificationCompat.Builder(context);

    mBuilder.setContentTitle("New Message");
    mBuilder.setContentText("You've received new message.");
    mBuilder.setTicker("New Message Alert!");
    mBuilder.setSmallIcon(R.mipmap.ic_launcher);
    mBuilder.setContentIntent(contentIntent);

    android.app.Notification notification = mBuilder.build();
    notification.defaults = notification.DEFAULT_SOUND |
            notification.DEFAULT_VIBRATE;
    notification.flags |= notification.FLAG_AUTO_CANCEL;

    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(1, notification);

    return Service.START_STICKY;
}

MainActivity:

    Calendar c = Calendar.getInstance();
    c.set(Calendar.HOUR_OF_DAY, 10);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);
    long day = TimeUnit.HOURS.toMillis(24);


    Intent alarmIntent = new Intent(this, AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),day, pendingIntent);

And BroadcastReceiver:

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent)
    {
        Intent service1 = new Intent(context, Notification.class);
        context.startService(service1);

    }
}

Receiver:

    @Override
    public void onReceive(Context context, Intent intent)
    {
        Intent service1 = new Intent(context, Notification.class);
        context.startService(service1);

        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        Boolean notif = sharedPreferences.getBoolean("notif", true);
        if (!notif) context.stopService(service1);

    }

Notification must be sent only at 10:00, but it has very strange behave and method stopService doesn't work every time(if even false). I suppose that somthing from with flags, but I still have no idea what exactly I did wrong.

Thanks in advance. And, please, don't unrate my question, even it is very easy or just stupid. I'm a newbie, and simply want to know.


Solution

  • You just need to destroy service, when it's done:

    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(1, notification);
    stopSelf()
    return Service.START_STICKY;