【发布时间】:2014-03-19 08:53:06
【问题描述】:
我正在开发一个应用程序,我在其中使用推送通知,这工作正常。我的 API 级别最低为 8,最高为 19。
我的生成通知代码如下:
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);
警告是: 构造函数 Notification(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);
}
但现在我在 if 中遇到错误:
调用需要 API 级别 11(当前最低为 8):新的 android.app.Notification.Builder 和 Else 部分中的警告保持不变。
现在该怎么办,请指导。
【问题讨论】:
标签: android notifications deprecated android-notifications