未显示通知 Android

Notifications not showing Android

我正尝试在设定的时间段后使用以下方式发出通知:

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 秒,但即使过了这段时间,我也没有在状态栏上收到任何通知。

请帮忙。

您需要 BroadcastReceiver 才能从系统获取通知。

public static class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        getNotification(String content);
    }        
}

并记得在您的 AndroidManifest 中声明 class:

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