Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Notification sent to user at a specific time Android

I'm trying to send a notification to a user at a specific time using an Alarm Manager. Basically, nothing happens at all and to me the code looks fine. My code for the Alarm Manager is below:

/** Notify the user when they have a task */
public void notifyAtTime() {
    Intent myIntent = new Intent(PlanActivity.this , Notification.class);     
    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
    PendingIntent pendingIntent = PendingIntent.getService(PlanActivity.this, 0, myIntent, 0);

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 12);
    calendar.set(Calendar.MINUTE, 17);
    calendar.set(Calendar.SECOND, 00);

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000 , pendingIntent);
}

The code for the notification, which is in the "Notification" class is below:

    public class Notification extends Service {


    @Override
    public void onCreate() {   

        Toast.makeText(this, "Notification", Toast.LENGTH_LONG).show();

        Intent intent = new Intent(this,PlanActivity.class);
        PendingIntent pending = PendingIntent.getActivity(this, 0, intent, 0);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                this).setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("Football")
                .setContentText("Don't forget that you have Football planned!")
                .setContentIntent(pending);

        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.notify(0, mBuilder.build());
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
}

The Toast which is set in the Notification class also doesn't come up. I don't know if it's something really silly that I'm missing but any help would be greatly appreciated! :)

like image 413
Connor McFadden Avatar asked Apr 03 '14 11:04

Connor McFadden


People also ask

Can you set notifications for certain times Android?

On Android, you will need to open Settings. Then choose Sound>Do Not Disturb>Schedules. On this screen, you have several options to set the days and times for Do Not Disturb, choose which apps to apply it to, and set exceptions.

How do I get notification time on Android?

You can use this firebase function in the onMessageReceived() method: Long time = remoteMessage. getSentTime();


1 Answers

This is your receiver class :

 public class OnetimeAlarmReceiver extends BroadcastReceiver {

  public void onReceive(Context context, Intent intent) {

  //Toast.makeText(context, "Repeating Alarm worked.", Toast.LENGTH_LONG).show();


  // try here

 // prepare intent which is triggered if the
 // notification is selected

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

 // build notification
 // the addAction re-use the same intent to keep the example short
 Notification n  = new Notification.Builder(this)
    .setContentTitle("New mail from " + "[email protected]")
    .setContentText("Subject")
    .setSmallIcon(R.drawable.icon)
    .setContentIntent(pIntent)
    .setAutoCancel(true)
    .addAction(R.drawable.icon, "Call", pIntent)
    .addAction(R.drawable.icon, "More", pIntent)
    .addAction(R.drawable.icon, "And more", pIntent).build();


    NotificationManager notificationManager = 
     (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    notificationManager.notify(0, n); 


   }
 }

In Second Activity to set Alerm :

  private void setAlarm(){

    Intent intent = new Intent(this, OnetimeAlarmReceiver.class);   

    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, REQUEST_CODE, intent, 0);

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + alarm_time , pendingIntent);
    System.out.println("Time Total ----- "+(System.currentTimeMillis()+total_mili));


} 

I would like to recommend this tutorial How to send notification

like image 107
Saj Avatar answered Oct 20 '22 00:10

Saj