我刚刚导入了一个android项目,在Notification部分,我必须使用setLatestEventInfo。但Android Studio说无法解析方法setLatestEventinfo。这是我的代码片段,请在回答时帮助编辑我的代码
NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher,"CEPF Mobile",0);
Intent notify_intent = new Intent(context,SpecialSerivceReportActivity.class);
notify_intent.putExtra("filename", "Special Service Report");
notify_intent.putExtra("URL", urls[0]);
PendingIntent contentIntent = PendingIntent.getActivity(context,(int)System.currentTimeMillis(),notify_intent,PendingIntent.FLAG_ONE_SHOT);
notification.setLatestEventInfo(context, "CEPF Mobile", "New post in Special Service Report", contentIntent);
notification.flags = Notification.FLAG_AUTO_CANCEL;
notification.defaults = Notification.DEFAULT_SOUND ;
nm.notify((int)System.currentTimeMillis(),notification);
}
else if(message.equalsIgnoreCase("Special Columnist Blog"))
{
NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher,"CEPF Mobile",0);
Intent notify_intent = new Intent(context,SpecialSerivceReportActivity.class);
notify_intent.putExtra("filename", "Special Columnist Blog");
notify_intent.putExtra("URL", urls[1]);
PendingIntent contentIntent = PendingIntent.getActivity(context,(int)System.currentTimeMillis(),notify_intent,PendingIntent.FLAG_ONE_SHOT);
notification.setLatestEventInfo(context, "CEPF Mobile", "New post in Special Columnist Blog", contentIntent);
notification.flags = Notification.FLAG_AUTO_CANCEL;
notification.defaults = Notification.DEFAULT_SOUND ;
nm.notify((int)System.currentTimeMillis(),notification);
}
答案 0 :(得分:3)
setLatestEventInfo()
来自Notification
,如果您的目标是API 23或更高版本,则需要重写代码。
而不是:
Notification notification = new Notification(R.drawable.ic_launcher,"CEPF Mobile",0);
notification.setLatestEventInfo(context, "CEPF Mobile", "New post in Special Columnist Blog", contentIntent);
执行:
Notification.Builder builder = new Notification.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("CEPF Mobile")
.setContentText("New post in Special Columnist Blog")
.setContentIntent(contentIntent);
Notification notification = builder.buid();
关于使用的一些注意事项:
NotificationCompat.Builder
代替Notification.Builder
。答案 1 :(得分:0)
对于最佳方法,请在android studio的支持下创建通知代码,
右键点击应用,然后点击新,然后点击用户界面,再点击通知,然后点击完成。
代码已准备好使用。