2016-02-25 200 views
-1

我尝试使用弹出一个通知的时间设置后段:通知不显示的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秒,但即使在这段时间之后,我也没有收到关于状态栏的任何通知。

请帮忙。

+0

你应该做的伎俩与广播接收器。报警并在onReceive()中启动通知..... – Opiatefuchs

回答

1

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

public static class AlarmReceiver extends BroadcastReceiver { 

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

记住宣布在AndroidManifest类:

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