2015-09-08 7 views
5

APIレベル10で通知しようとしていますが、私のコードは以下ですが、構文エラーが発生しました。setLatestEventInfoは解決できません。 APIレベルとの関係を推測していますsetLatestEventInfoを解決できません

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; 
} 

答えて

8

コンパイルSDKのバージョンがapi 23+に設定されている場合、この問題が発生します。このメソッドは、Mで削除されました(API 23)。

api 4以降の通知を行うにはAndroid Support Libraryに追加されたNotificationCompat.Builderを使用できます。 Android Documentationはまともな例があります。

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

古いAPIバージョンをサポートする必要がある場合は、「NotificationCompat」のための「Notification.Builder」を置き換えることができます。

+0

サポートライブラリを使用していない場合、メガバイトがアプリサイズに追加されるため、どうすればよいですか? – Justin

関連する問題