Android AlarmManager Push Notifications

I am using this notification code:

package com.example.mega; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class TimeAlarm extends BroadcastReceiver { NotificationManager nm; @Override public void onReceive(Context context, Intent intent) { nm = (NotificationManager) context .getSystemService(Context.NOTIFICATION_SERVICE); CharSequence from = "Tech"; CharSequence message = "Check out our NEW COLLECTION !!"; PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(), 0); Notification notif = new Notification(R.drawable.icon, "Check out our NEW COLLECTION !!" , System.currentTimeMillis()); notif.setLatestEventInfo(context, from, message, contentIntent); nm.notify(1, notif); } } 

As you can see, I have not added AlarmManager yet, since I have no experience using it, I am new to android. What should I add to the code to show a notification every 24 hours?

+4
source share
1 answer
 Calendar calendar = Calendar.getInstance(); // 8.00 (8 AM) calendar.set(Calendar.HOUR_OF_DAY, 8); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); PendingIntent pi = PendingIntent.getService(context, 0 , new Intent(context, Your_Class.class),PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi); 

It rings an alarm daily at 8:00 a.m.

Or you can use https://github.com/commonsguy/cwac-wakeful . Take a look at the documentation here.

Take a look at this: Fire notification every 24 hours and at the exact time of 8 a.m.

And this: http://blog.blundellapps.com/notification-for-a-user-chosen-time/

+4
source

Source: https://habr.com/ru/post/1495110/


All Articles