不显示Android的通知
我正在尝试在设定的时间段后使用以下命令发出通知:不显示Android的通知,android,android-notifications,Android,Android Notifications,我正在尝试在设定的时间段后使用以下命令发出通知: scheduleNotification(getNotification("Notification content..") differ); 以及以下功能- private Notification getNotification(String content) { Notification.Builder builder = new Notification.Builder(this); builder.se
scheduleNotification(getNotification("Notification content..") differ);
以及以下功能-
private Notification getNotification(String content) {
Notification.Builder builder = new Notification.Builder(this);
builder.setContentTitle("Scheduled Notification");
builder.setContentText(content);
builder.setSmallIcon(R.drawable.icon);
return builder.build();
}
private void scheduleNotification(Notification notification, long 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);
}
记录延迟值后,我得到57580,大约57秒,但即使在这段时间之后,我在状态栏上也没有收到任何通知
请帮助。您需要一个
广播接收器
从系统获取通知
public static class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
getNotification(String content);
}
}
记住在AndroidManifest中声明类:
<receiver android:name=".AlarmReceiver" />
你应该用一个广播接收器来做这个把戏。在onReceive()中安排报警和启动通知。。。。。