2

特定の時間の後に通知を送信するための助けが必要です。ここでいくつかの例を見てきましたが、うまくいきません。

ボタンをクリックすると、今から 5 秒後に AlarmManager を設定するメソッドが呼び出され、次にメソッドを呼び出して通知を送信する必要があります。以下は同じコードです。

private void startAlarm() {
        AlarmManager alarmManager = (AlarmManager) this.getSystemService(this.ALARM_SERVICE);

        Calendar rightNow = Calendar.getInstance();
         long when = rightNow.getTimeInMillis()+5000;

        Intent intent = new Intent(this, RemainderService.class);
        PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 0);
        alarmManager.set(AlarmManager.RTC_WAKEUP,when,pendingIntent);
    }

別のクラス (ファイル) から ReminderService.java

public class RemainderService extends IntentService {
    public RemainderService() {
        super("RemainderService");
    }

    private static final int NOTIF_ID = 1;

    @Override
      protected void onHandleIntent(Intent intent) {

        NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        long when = System.currentTimeMillis();         // notification time

        Notification notification = new Notification(R.drawable.icon, "reminder", when);
        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.flags |= notification.FLAG_AUTO_CANCEL;

        Intent notificationIntent = new Intent(this, Notify.class);
        notificationIntent.putExtra("notificationID", 20);

        PendingIntent contentIntent = PendingIntent.getActivity(this, 20, notificationIntent , 0);

        notification.setLatestEventInfo(getApplicationContext(), "It's about time", "You should open the app now", contentIntent);

        nm.notify(NOTIF_ID, notification);
    }
}

基本的なヒューマン エラーです。マニフェスト ファイルに詳細を追加するのを忘れていました。

4

0 に答える 0