2015-09-08 23 views
5

Sto provando a fare una notifica su API 10 e sotto è il mio codice ma sto ottenendo un errore di sintassi setLatestEventInfo non può essere risolto. Sono indovinando la sua qualcosa a che fare con il livello di APIsetLatestEventInfo non può essere risolto

public static void sendNotification(Context caller, Class<?> activityToLaunch, String title, String msg, int numberOfEvents, boolean sound, boolean flashLed, boolean vibrate, int iconID) { 
    NotificationManager notifier = (NotificationManager) caller.getSystemService(Context.NOTIFICATION_SERVICE); 

    final Notification notify = new Notification(iconID, "", System.currentTimeMillis()); 

    notify.icon = iconID; 
    notify.tickerText = title; 
    notify.when = System.currentTimeMillis(); 
    notify.number = numberOfEvents; 
    notify.flags |= Notification.FLAG_AUTO_CANCEL; 
    if (sound) notify.defaults |= Notification.DEFAULT_SOUND; 

    if (flashLed) { 
     // add lights 
     notify.flags |= Notification.FLAG_SHOW_LIGHTS; 
     notify.ledARGB = Color.BLUE; 
     notify.ledOnMS = 500; 
     notify.ledOffMS = 500; 
    } 

    if (vibrate) { 
     notify.vibrate = new long[]{100, 200, 300}; 
    } 

    Intent toLaunch = new Intent(caller, activityToLaunch); 
    toLaunch.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK); 
    toLaunch.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    PendingIntent intentBack = PendingIntent.getActivity(caller, notify.number, toLaunch, 0); 
    notify.setLatestEventInfo(caller, title, msg, intentBack); 
    notifier.notify(notify.number, notify); 
    notify.number = notify.number + 1; 
} 

risposta

8

Se la versione di compilazione SDK è impostato su api 23+ vedrete questo problema. Questo metodo è stato rimosso in M ​​(api 23).

Per le notifiche per api 4 in poi è possibile utilizzare lo NotificationCompat.Builder che è stato aggiunto nello Android Support Library. Il Android Documentation ha un esempio decente:

Notification noti = new Notification.Builder(mContext) 
     .setContentTitle("New mail from " + sender.toString()) 
     .setContentText(subject) 
     .setSmallIcon(R.drawable.new_mail) 
     .setLargeIcon(aBitmap) 
     .build(); 

si può sostituire "Notification.Builder" per "NotificationCompat" se necessario per supportare le versioni più vecchie api.

+0

Cosa succede se non si utilizza la libreria di supporto perché aggiunge megabyte alle dimensioni dell'app? – Justin

Problemi correlati