2017-09-16 30 views
3

這是我在下面給出的代碼。儘管創建了通知頻道,但無法在Android O上創建任何通知。儘管創建了頻道,但是Android O中沒有顯示通知

private void weatherNotification(WeatherInfo weather) { 
    Intent intent = new Intent(this, WeatherActivity.class); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); 

    String temperatureScale = prefs.getUnits().equals("metric") ? getString(R.string.c) : getString(R.string.f); 
    String speedScale = prefs.getUnits().equals("metric") ? getString(R.string.mps) : getString(R.string.mph); 

    String temperature = getString(R.string.temperature , weather.getMain().getTemp() , temperatureScale); 
    String city = getString(R.string.city , weather.getName() + ", " + weather.getSys().getCountry()); 
    String wind = getString(R.string.wind_ , weather.getWind().getSpeed(), speedScale); 
    String humidity = getString(R.string.humidity , weather.getMain().getHumidity()); 
    String pressure = getString(R.string.pressure, weather.getMain().getPressure()); 

    String data = city + "\n" + temperature + "\n" + wind + "\n" + humidity + "\n" + pressure; 

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { 
     String id = "w01", name = getString(R.string.weather_notification_title); 
     int importance = NotificationManager.IMPORTANCE_DEFAULT; 
     String desc = getString(R.string.weather_notification_description); 

     NotificationChannel channel = new NotificationChannel(id, name, importance); 
     channel.setDescription(desc); 
     notificationManager.createNotificationChannel(channel); 
    } 
    Notification.Builder builder = new Notification.Builder(this); 

    builder.setAutoCancel(false); 
    builder.setContentTitle("Weather Notification"); 
    builder.setContentText(Math.round(weather.getMain().getTemp()) + temperatureScale + " at " + weather.getName()); 
    builder.setStyle(new Notification.BigTextStyle().bigText(data)); 
    builder.setSmallIcon(R.drawable.ic_notification_icon); 
    builder.setContentIntent(pendingIntent); 
    if (Build.VERSION.SDK_INT >= 24) 
     builder.setColor(Color.parseColor("#ff0000")); 
    Notification notification = builder.build(); 
    notificationManager.notify(0 , notification); 
} 

我相信我按照所有所需要的在Android o創建一個通知的步驟 - 創建使用通知管理器通知信道,其次是建立在這位經理的通知。我不知道我哪裏錯了。

編輯1:製造weatherNotification以下變化()方法,仍然不能正常工作:

private void weatherNotification(WeatherInfo weather) { 

    .... 

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { 
     String id = "w01", name = getString(R.string.weather_notification_title); 
     int importance = NotificationManager.IMPORTANCE_DEFAULT; 
     String desc = getString(R.string.weather_notification_description); 

     NotificationChannel channel = new NotificationChannel(id, name, importance); 
     channel.setDescription(desc); 
     notificationManager.createNotificationChannel(channel); 
     builder = new Notification.Builder(this , id); 
    } 
    builder = new Notification.Builder(this); 
    builder.setAutoCancel(false); 
    builder.setContentTitle("Weather Notification"); 
    builder.setContentText(Math.round(weather.getMain().getTemp()) + temperatureScale + " at " + weather.getName()); 
    builder.setStyle(new Notification.BigTextStyle().bigText(data)); 
    builder.setSmallIcon(R.drawable.ic_notification_icon); 
    builder.setContentIntent(pendingIntent); 
    if (Build.VERSION.SDK_INT >= 24) 
     builder.setColor(Color.parseColor("#ff0000")); 
    Notification notification = builder.build(); 
    notificationManager.notify(0 , notification); 
    .... 
} 

編輯2:從編輯1碼,我發現該建築工地正在重新再生。於是,我再次做了以下修改:

private void weatherNotification(WeatherInfo weather) { 
    .... 
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { 
     String id = "w01", name = getString(R.string.weather_notification_title); 
     int importance = NotificationManager.IMPORTANCE_DEFAULT; 
     String desc = getString(R.string.weather_notification_description); 

     NotificationChannel channel = new NotificationChannel(id, name, importance); 
     channel.setDescription(desc); 
     notificationManager.createNotificationChannel(channel); 
     builder = new Notification.Builder(this , id); 
    } 
    else 
     builder = new Notification.Builder(this); 
    .... 
} 

回答

3

你應該在這條線得到一個棄用警告:

Notification.Builder builder = new Notification.Builder(this); 

這是因爲新的構造函數將您的頻道ID:

Notification.Builder builder = new Notification.Builder(this, id); 

(儘管您需要稍微修改一下代碼,以便id仍然有效)

引用the JavaDocs for the constructor that you are using right now:「所有發佈的通知都必須指定一個NotificationChannel ID。」現在,您在籌集Notification時沒有使用該通道。 AFAIK,這將阻止你的Notification被顯示。

+0

請檢查編輯1 – Sparker0i

+0

編輯2,它的工作..謝謝 – Sparker0i

相關問題