2017-12-05 9 views
1

아마도이 문제는 내가 예상 한 것보다 간단합니다. 함수 내에서 생성 된 notification을 표시하고 싶습니다. 디버깅 이유).이전 API 레벨 (예 : lvl 23)을 지원하는 Android 알림을 만드는 방법

Android Studio 3.0.1 (23) (이전 장치를 지원하기 위해) LVL API 및 알림 표시하는 제

규범 LVL API 광고와 Nexus5 장치에서 실행 :

PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0); 
    Notification notification = new NotificationCompat.Builder(this) 
      .setTicker("SomethingTicker") 
      .setSmallIcon(R.drawable.someImage) 
      .setContentTitle("SomeTitle") 
      .setContentText("SomeText") 
      .setContentIntent(pi) 
      .build(); 

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    notificationManager.notify(001, notification); 

NotificationCompat.Builder 그런 도시 컨텍스트 만 사용되지 않습니다. 최신 버전에서는 알림 채널을 사용해야합니다.

문제 : 알림 채널을 만들고 등록하려면 api lvl> 27을 사용해야합니다.

무엇이 누락 되었습니까? 그리고 가장 좋은 방법은 무엇입니까

답변

3

활동의 onCreate 기능 내에서 생성 된 알림을 표시하고 싶습니다.

Notification을 표시하는 데 이상한 시간입니다.

무엇이 누락 되었습니까?

NotificationManager mgr= 
    (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 

if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O && 
    mgr.getNotificationChannel(CHANNEL_WHATEVER)==null) { 
    mgr.createNotificationChannel(new NotificationChannel(CHANNEL_WHATEVER, 
    "Whatever", NotificationManager.IMPORTANCE_DEFAULT)); 
} 

그런 다음, 모든 API 수준의 NotificationCompat.Builder에 (여기 CHANNEL_WHATEVER를) 채널 식별자를 전달 :

는 당신은 안드로이드 8.0 이상 기기에서 실행됩니다 NotificationChannel를 만드는 코드가 필요 건설자. 구형 장치에서는 채널이 무시됩니다.

+0

참고 : 디버깅을 이유로 생성 시간이 다소 차이났습니다. 그것을 선택적 매개 변수로 설정하는 것이 좋습니다. 고맙습니다;) – schlenger

+0

@schlenger : "디버깅을 위해 생성 시간이 다소 차이났습니다."- 아, 알겠습니다. – CommonsWare