2014-03-19 2 views
0

푸시 알림을 사용중인 애플리케이션을 개발 중이며 정상적으로 작동합니다. 내 API 레벨 분은 8이고 최대는 19Android : 알림 API 수준의 오류

다음과 같이 알림을 생성의 내 코드는 다음과 같습니다

여기
private static void generateNotification(Context context, String message) { 
    int icon = R.drawable.ic_launcher; 
    long when = System.currentTimeMillis(); 
    NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); 
    Notification notification = new Notification(icon, message, when); 

    String title = context.getString(R.string.app_name); 

    Intent notificationIntent = new Intent(context, SplashScreenActivity.class); 
    notificationIntent.putExtra("message", message); 
    // set intent so it does not start a new activity 
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | 
      Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    PendingIntent intent = 
      PendingIntent.getActivity(context, 0, notificationIntent, 0); 
    notification.setLatestEventInfo(context, title, message, intent); 
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 

    // Play default notification sound 
    notification.defaults |= Notification.DEFAULT_SOUND; 

    //notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "your_sound_file_name.mp3"); 

    // Vibrate if vibrate is enabled 
    notification.defaults |= Notification.DEFAULT_VIBRATE; 
    notificationManager.notify(0, notification); 
} 

내가 두 장소에 경고 점점 오전 :

Notification notification = new Notification(icon, message, when); 

notification.setLatestEventInfo(context, title, message, intent); 

경고는 다음과 같습니다. 생성자 Not ification (int, CharSequence, long)은 더 이상 사용되지 않습니다.Notification 유형의 setLatestEventInfo (Context, CharSequence, CharSequence, PendingIntent) 메서드는 각각이 아닙니다.

그래서 나는 같이 코드를 변경했다 :

Notification notification; 

    int currentVersion = android.os.Build.VERSION.SDK_INT; 
    int honeycombVersion = android.os.Build.VERSION_CODES.HONEYCOMB; 

    if (currentVersion >= honeycombVersion){ 
     notification = new Notification.Builder(context) 
             .setContentTitle(",App") 
             .setContentText(message) 
             .setSmallIcon(icon) 
             .build(); 
    } else{ 
     notification = new Notification(icon, message, when); 
    } 

을하지만 같은이 경우 지금은 에 오류가 점점 오전 :

전화는 API 레벨 11를 필요로 (현재 분 8) : 새로운 android.app.Notification.Builder의 경고 기타 부분은 동일하게 유지됩니다.

이제 어떻게해야합니까? 상황 당신이 할 수있는 최선의

답변

0

이 체크를

int apiVersion = android.os.Build.VERSION.SDK_INT; 
if (apiVersion >= Build.VERSION_CODES.HONEYCOMB){ 
    // call the constructor available since api level 11 
} else { 
    // fallback call the old constructor 
} 

을 추가하고의 오류에 대한

0

disapper 경고/오류를 확인하기 위해 SuppressLint 주석을 추가하는 것입니다 "전화는 API 레벨 11를 필요로 (현재 분 8) : 새로운 android.app.Notification.Builder "

시도가 build.gradle에 minSdkVersion 11minSdkVersion 8을 변경할 수