web-dev-qa-db-fra.com

Notification envoyée à l'utilisateur à un moment précis Android

J'essaie d'envoyer une notification à un utilisateur à une heure précise à l'aide d'un gestionnaire d'alarmes. En gros, rien ne se passe et pour moi, le code est correct. Mon code pour le gestionnaire d'alarmes est ci-dessous:

/** 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);
}

Le code de la notification, qui se trouve dans la classe "Notification", est ci-dessous:

    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;
    }
}

Le pain grillé défini dans la classe Notification ne s'affiche pas non plus. Je ne sais pas si c'est quelque chose de vraiment idiot qui me manque, mais toute aide serait grandement appréciée! :)

11
Connor McFadden

Ceci est votre classe de récepteur: 

 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); 


   }
 }

En deuxième activité pour définir l'alarme:

  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));


} 

Je voudrais recommander ce tutoriel Comment envoyer une notification

9
Saj

Essayez de mettre votre code de notification dans la fonction onStartCommand au lieu de onCreate.

1
Amine ZGHIDI

L'écriture du code alarmmanager dans la méthode onCreate de l'activité LAUNCHER de l'application a résolu mon problème.

0
Moses Kirathe