我正在尝试使用AlarmManager推送通知,这是我的代码:
package notification;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.media.RingtoneManager;
import android.os.Build;
import com.example.hoang.bnhnint.R;
import com.example.hoang.bnhnint.ViewScheduleActivity;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class NotificationUtils {
private Context context;
private static int INDEX = 1;
public NotificationUtils(Context context) {
this.context = context;
}
public void scheduleNotification(int hour_of_day, int minute, int notificationId) {//delay is after how much time(in millis) from current time you want to schedule the notification
Notification.Builder builder = new Notification.Builder(context)
.setContentTitle("Tới giờ uống thuốc buổi sáng")
.setContentText("Tới giờ uống thuốc buổi sáng")
.setAutoCancel(true)
.setSmallIcon(R.drawable.pill)
.setLargeIcon(((BitmapDrawable) context.getResources().getDrawable(R.drawable.pill)).getBitmap())
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
Intent intent = new Intent(context, ViewScheduleActivity.class);
PendingIntent activity = PendingIntent.getActivity(context, notificationId, intent, PendingIntent.FLAG_CANCEL_CURRENT);
builder.setContentIntent(activity);
Notification notification;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
notification = builder.build();
} else {
notification = builder.getNotification();
}
Intent notificationIntent = new Intent(context, NotificationPublisher.class);
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, notificationId);
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notification);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, notificationId, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
Calendar cal = new GregorianCalendar();
cal.set(Calendar.HOUR_OF_DAY, hour_of_day);
cal.set(Calendar.MINUTE, minute);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
}
}
这是我的发布者:
package notification;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class NotificationPublisher 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);
}
}
我在这里称呼它
TimePickerDialog timePickerDialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
NotificationUtils utils = new NotificationUtils(SetMedicineTakingTimeActivity.this);
utils.scheduleNotification(hourOfDay, minute, 1);
}
}, hour, minute, true);
经过大量研究后,我得出了这些代码。但这是行不通的。我试图每天在特定时间从TimePickerDialog推送通知。此外,任何人都可以解释通知ID真正做什么?