2017-10-11 7 views
0

毎日異なる時刻に通知を(バックグラウンドで)送信する単純なアプリを実行したいと思います。私はインターネット上でそれを行う方法を探しましたが、私はアンドロイドプログラミングには新しく、私は多くを理解していません。誰かが私を助けることができるので、私はすべてのコードが欲しいわけではありませんが、それを行う方法に関する基本的なアイデアだけです。 ありがとうございます。アプリは特定の時刻にバックグラウンドで通知を送信します

答えて

0

NotificationManagerとAlarmManagerをルックアップします。

例から採取

public class NotificationPublisher extends BroadcastReceiver { 

    public static String NOTIFICATION_ID = "notification-id"; 
    public static String NOTIFICATION = "notification"; 

    public void onReceive(Context context, Intent intent) { 

    NotificationManager notificationManager = (NotificationManager) 
     context.getSystemService(Context.NOTIFICATION_SERVICE); 
    Notification notification = intent.getParcelableExtra(NOTIFICATION); 
    int id = intent.getIntExtra(NOTIFICATION_ID, 0); 
    notificationManager.notify(id, notification); 

    } 
} 

private void scheduleNotification(Notification notification, int delay) { 

    Intent notificationIntent = new Intent(this, NotificationPublisher.class); 
    notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, 1); 
    notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notification); 
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

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

がコード:https://gist.github.com/BrandonSmith/6679223

関連する問題