2017-09-16 6 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); 
} 

는 내가 안드로이드 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 : 편집 한 코드에서, 나는 빌더가 다시 재생되고있는 것을 발견했다. 그래서 나는 다시 다음과 같이 변경했다 :

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

확인하십시오. EDIT 1 – Sparker0i

+0

EDIT 2, 작동했습니다. 감사합니다. – Sparker0i