Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AlarmManager with Notification Android

Tags:

android

I'm trying to give the user a notification each day on a certain time so I use an AlarmManager with a notification. I have this:

public void check_products(int hour, int minute){=
    Calendar calendar = Calendar.getInstance();

    Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager am = (AlarmManager) MainActivity.this.getSystemService(MainActivity.this.ALARM_SERVICE);
    am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000, pendingIntent);
}

public class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

        Intent notificationIntent = new Intent(context, MainActivity.class);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(context)
                .setContentTitle("Reminder")
                .setContentText("You need to eat")
                .setWhen(System.currentTimeMillis())
                .setContentIntent(pendingIntent);

        notificationManager.notify(1, mNotifyBuilder.build());
    }
}

And in the manifest (under the app permissions it says "set an alarm"):

<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<receiver android:name="com.example.check_products.AlarmReceiver"/>

In my main I call the function check_products with the parameters, but whatever I try to do; it doesn't show anything... Does someone know what I'm doing wrong?

I'm verry new to android programming so I wouldn't know how to combine the alarmmanager and notifications...

like image 229
Steven Avatar asked Mar 28 '26 15:03

Steven


2 Answers

Try like this, it may help you

Intent myIntent1 = new Intent(sign_in.this,MyNotificationService.class);
pendingintent2 = PendingIntent.getService(sign_in.this, 1,myIntent1, 1);
AlarmManager alarmManager1 = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar1Notify = Calendar.getInstance();

calendar1Notify.setTimeInMillis(System.currentTimeMillis());
calendar1Notify.set(Calendar.HOUR_OF_DAY, 8);
calendar1Notify.set(Calendar.MINUTE, 00);

alarmManager1.set(AlarmManager.RTC_WAKEUP,calendar1Notify.getTimeInMillis(), pendingintent2);
long time24h = 24*60*60*1000;
alarmManager1.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar1Notify.getTimeInMillis(),time24h,pendingintent2);

link For Reference

See above link Your question is Solved .

like image 65
Mano Avatar answered Mar 31 '26 03:03

Mano


use this Example for reference..

http://androidideasblog.blogspot.in/2011/07/alarmmanager-and-notificationmanager.html

Use This code for Repeating alarm in it..

AlarmManager am = (AlarmManager) MainActivity.this.getSystemService(MainActivity.this.ALARM_SERVICE);

am.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
        AlarmManager.INTERVAL_DAY, alarmIntent);
like image 29
Bhavin Kevadiya Avatar answered Mar 31 '26 04:03

Bhavin Kevadiya