12

Ich versuche, die Benachrichtigung für eine Zeit in der Zukunft zu setzen. Ich habe den Code zum Erstellen einer Benachrichtigung, aber ich kann keine Option finden, um sie zu planen.So planen Sie Benachrichtigungen in Android

Wie kann ich Benachrichtigungen planen?

+1

Diese immer noch nützlich ist beca Verwenden Sie die andere Frage war 3 Jahre vor dieser und einige zugrunde liegende Verhalten hat sich geändert, also danke! – AfzalivE

Antwort

16

Sie benötigen PendingIntent und BroadCastReceiver für diesen Einsatz -

public void scheduleNotification(Context context, long delay, int notificationId) {//delay is after how much time(in millis) from current time you want to schedule the notification 
     NotificationCompat.Builder builder = new NotificationCompat.Builder(context) 
       .setContentTitle(context.getString(R.string.title)) 
       .setContentText(context.getString(R.string.content)) 
       .setAutoCancel(true) 
       .setSmallIcon(R.drawable.app_icon) 
       .setLargeIcon(((BitmapDrawable) context.getResources().getDrawable(R.drawable.app_icon)).getBitmap()) 
       .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)); 

     Intent intent = new Intent(context, YourActivity.class); 
     PendingIntent activity = PendingIntent.getActivity(context, notificationId, intent, PendingIntent.FLAG_CANCEL_CURRENT); 
     builder.setContentIntent(activity); 

     Notification notification = builder.build(); 

     Intent notificationIntent = new Intent(context, MyNotificationPublisher.class); 
     notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, notificationId); 
     notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notification); 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(context, notificationId, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT); 

     long futureInMillis = SystemClock.elapsedRealtime() + delay; 
     AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
     alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent); 
    } 
Auch

, benötigen Sie eine Benachrichtigung in Ihrem Receiver Klasse zeigen -

public class MyNotificationPublisher extends BroadcastReceiver { 

    public static String NOTIFICATION_ID = "notification_id"; 
    public static String NOTIFICATION = "notification"; 

    @Override 
    public void onReceive(final Context context, Intent intent) { 

     NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 

     Notification notification = intent.getParcelableExtra(NOTIFICATION); 
     int notificationId = intent.getIntExtra(NOTIFICATION_ID, 0); 
     notificationManager.notify(notificationId, notification); 
    } 
} 

Schließlich fordern scheduleNotification() mit entsprechenden Argumenten und Sie sind gut zu gehen!

+0

Vielen Dank, es hat für mich funktioniert. – OmerN

+0

so jeden Tag um 12 Uhr zum Beispiel ... sind diese Codes in Haupttätigkeit gehen? – Si8

+0

Dieser geplante Codealarm wird abgebrochen, wenn die App in der Liste "Zuletzt verwendete Apps" getippt wird. Wie vermeide ich dieses Problem? – NghiaDao

24

NICHT FÜR DIE VERWENDUNG IN OREO + (edit)

Die oben genannten Antworten sind gut - aber nicht der Ansicht, das Potenzial der Benutzer das Gerät neu zu starten (die durch Alarmmanager geplant PendingIntent des löscht).

Sie müssen einen WakefulBroadcastReceiver erstellen, der einen AlarmManager enthält, um einen PendingIntent zu planen. Wenn der WakefulBroadcastReceiver die Absicht behandelt - senden Sie Ihre Benachrichtigung und signalisieren Sie, dass der WakefulBroadcastReceiver abgeschlossen ist.

WakefulBroadcastReceiver

/** 
    * When the alarm fires, this WakefulBroadcastReceiver receives the broadcast Intent 
    * and then posts the notification. 
    */ 
    public class WakefulReceiver extends WakefulBroadcastReceiver { 
     // provides access to the system alarm services. 
     private AlarmManager mAlarmManager; 

     public void onReceive(Context context, Intent intent) { 
      //// TODO: post notification 
      WakefulReceiver.completeWakefulIntent(intent); 
     } 

     /** 
     * Sets the next alarm to run. When the alarm fires, 
     * the app broadcasts an Intent to this WakefulBroadcastReceiver. 
     * @param context the context of the app's Activity. 
     */ 
     public void setAlarm(Context context) { 
      mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
      Intent intent = new Intent(context, WakefulReceiver.class); 
      PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0); 

      Calendar calendar = Calendar.getInstance(); 
      calendar.setTimeInMillis(System.currentTimeMillis()); 
      //// TODO: use calendar.add(Calendar.SECOND,MINUTE,HOUR, int); 
      //calendar.add(Calendar.SECOND, 10); 

      //ALWAYS recompute the calendar after using add, set, roll 
      Date date = calendar.getTime(); 

      mAlarmManager.setExact(AlarmManager.RTC_WAKEUP, date.getTime(), alarmIntent); 

      // Enable {@code BootReceiver} to automatically restart when the 
      // device is rebooted. 
      //// TODO: you may need to reference the context by ApplicationActivity.class 
      ComponentName receiver = new ComponentName(context, BootReceiver.class); 
      PackageManager pm = context.getPackageManager(); 
      pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 
        PackageManager.DONT_KILL_APP); 
     } 

     /** 
     * Cancels the next alarm from running. Removes any intents set by this 
     * WakefulBroadcastReceiver. 
     * @param context the context of the app's Activity 
     */ 
     public void cancelAlarm(Context context) { 
      Log.d("WakefulAlarmReceiver", "{cancelAlarm}"); 

      mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
      Intent intent = new Intent(context, WakefulReceiver.class); 
      PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0); 

      mAlarmManager.cancel(alarmIntent); 

      // Disable {@code BootReceiver} so that it doesn't automatically restart when the device is rebooted. 
      //// TODO: you may need to reference the context by ApplicationActivity.class 
      ComponentName receiver = new ComponentName(context, BootReceiver.class); 
      PackageManager pm = context.getPackageManager(); 
      pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 
        PackageManager.DONT_KILL_APP); 
     } 

BootReceiver

public class BootReceiver extends BroadcastReceiver { 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) { 
      context = ApplicationActivity.class; 
      AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
      Intent intent = new Intent(context, WakefulReceiver.class); 
      PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0); 

      Calendar calendar = Calendar.getInstance(); 
      calendar.setTimeInMillis(System.currentTimeMillis()); 
      //// TODO: use calendar.add(Calendar.SECOND,MINUTE,HOUR, int); 
      //calendar.add(Calendar.SECOND, 10); 

      //ALWAYS recompute the calendar after using add, set, roll 
      Date date = calendar.getTime(); 

      alarmManager.setExact(AlarmManager.RTC_WAKEUP, date.getTime(), alarmIntent); 
      } 
     } 
    } 

AndroidManifest.xml

<receiver android:name=".WakefulReceiver"/> 

<receiver android:name=".BootReceiver" 
    android:enabled="false"> 
    <intent-filter> 
     <action android:name="android.intent.action.BOOT_COMPLETED"/> 
    </intent-filter> 
</receiver> 
+0

Das ist Gold! Freut mich, es zu finden. –

+2

Hey auf der 'BootReceiver.class' solltest du nicht manuell" android.intent.action.BOOT_COMPLETED "eingeben sondern" Intent.ACTION_BOOT_COMPLETED "verwenden – SARose

+1

good catch - Ich habe dies aktualisiert! Danke für Ihren Beitrag. – apelsoczi