2016-02-25 149 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" />