how to create a notification for specific date and time in android studio without using service?
Asked
Active
Viewed 1.6k times
1
-
1Welcome to stack overflow! For some tips on how to ask questions here see http://stackoverflow.com/help/on-topic. Is there a specific problem you'rerunning into? Could you share some of the code you have right now? – Onots Jan 04 '16 at 00:52
1 Answers
8
use the class AlarmManager that runs a code at the background. put this code in the main Activity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your layout);
boolean alarm = (PendingIntent.getBroadcast(this, 0, new Intent("ALARM"), PendingIntent.FLAG_NO_CREATE) == null);
if(alarm){
Intent itAlarm = new Intent("ALARM");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,0,itAlarm,0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 3);
AlarmManager alarme = (AlarmManager) getSystemService(ALARM_SERVICE);
alarme.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),60000, pendingIntent);
}
}
create a new class that will control if it is at the hour to notify.
public class BroadcastManager extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
try {
String yourDate = "04/01/2016";
String yourHour = "16:45:23";
Date d = new Date();
DateFormat date = new SimpleDateFormat("dd/MM/yyyy");
DateFormat hour = new SimpleDateFormat("HH:mm:ss");
if (date.equals(yourDate) && hour.equals(yourHour)){
Intent it = new Intent(context, MainActivity.class);
createNotification(context, it, "new mensage", "body!", "this is a mensage");
}
}catch (Exception e){
Log.i("date","error == "+e.getMessage());
}
}
public void createNotification(Context context, Intent intent, CharSequence ticker, CharSequence title, CharSequence descricao){
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent p = PendingIntent.getActivity(context, 0, intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setTicker(ticker);
builder.setContentTitle(title);
builder.setContentText(descricao);
builder.setSmallIcon(R.drawable.yourIcon);
builder.setContentIntent(p);
Notification n = builder.build();
//create the notification
n.vibrate = new long[]{150, 300, 150, 400};
n.flags = Notification.FLAG_AUTO_CANCEL;
nm.notify(R.drawable.yourIcon, n);
//create a vibration
try{
Uri som = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone toque = RingtoneManager.getRingtone(context, som);
toque.play();
}
catch(Exception e){}
}
in the manifest put this:
<receiver
android:name="BroadcastManager"
android:label="BroadcastReceiverAux">
<intent-filter>
<action android:name="ALARM" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
give this permission:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"

Jenix
- 2,996
- 2
- 29
- 58

Fabiano Araujo
- 546
- 1
- 4
- 15
-
Its working perfectly on Marshmallow but not working on android 10. Any solution? – Saify Mar 22 '21 at 08:57
-
@Saify this post might help: https://stackoverflow.com/a/56232572/8192914 – ganjaam Sep 11 '22 at 08:08