通知未显示Android

时间:2016-02-25 11:50:15

标签: android android-notifications

我试图在设定的一段时间后使用以下方式提出通知:

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

请帮忙。

1 个答案:

答案 0 :(得分:1)

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

public static class AlarmReceiver extends BroadcastReceiver {

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

请记住在AndroidManifest中声明该类:

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