Android NotificationCompat.Builder:无法解析方法生成()
我有下面这个简单的类,我想用它来通知用户收到的消息(我会随着应用的发展而改进它)。但现在,在最后一行中,出现以下错误,我无法运行它: 无法解析方法生成() 代码如下:Android NotificationCompat.Builder:无法解析方法生成(),android,notifications,builder,Android,Notifications,Builder,我有下面这个简单的类,我想用它来通知用户收到的消息(我会随着应用的发展而改进它)。但现在,在最后一行中,出现以下错误,我无法运行它: 无法解析方法生成() 代码如下: import android.app.Activity; import android.app.NotificationManager; import android.content.Context; import android.os.Bundle; import android.support.v4.app.Notificat
import android.app.Activity;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
public class UserNotificationActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void triggerNoti() {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My notification")
.setContentText("Hello World!");
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(001, mBuilder.build());
}
}
我试过了,但没有任何改变
我做错了什么
p.S.:目标(&min)sdk=21通知方法在支持V4和API级别19以上时有所更改 . 您可以尝试下面的代码块
public void sendNotification(String message, String title, Intent intent, int not_id) {
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notification;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
notification
= new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.app_icon)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setContentTitle(title)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
} else {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.app_icon);
notification
= new NotificationCompat.Builder(this)
.setContentTitle(title)
.setSmallIcon(R.drawable.small_icon)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setContentText(message)
.setAutoCancel(true)
//.setColor(Color.parseColor("#1a4994"))
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setLargeIcon(bitmap)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
}
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(not_id, notification.build());
}
public void sendNotification(字符串消息、字符串标题、意图、int not_id){
PendingEvent PendingEvent=PendingEvent.getActivity(this,0,intent,
悬挂式帐篷(一杆旗帜);
Uri defaultSoundUri=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_通知);
通知相容建造商通知;
if(Build.VERSION.SDK\u INT
更新内容:
公共频道(上下文){
如果(Build.VERSION.SDK_INT<26){
返回;
}
通知经理通知经理=
(NotificationManager)context.getSystemService(context.NOTIFICATION\u服务);
NotificationChannel=新建NotificationChannel(“默认”,
“频道名称”,
NotificationManager.重要性(默认值);
频道。设置描述(“频道描述”);
notificationManager.createNotificationChannel(频道);
}
感谢您的解决方案,但遗憾的是,这并不能完全解决我的困境,因为我也有同样的错误!!最后一行出现相同错误检查您的导入:import android.support.v4.app.NotificationCompat;
public void initChannels(Context context) {
if (Build.VERSION.SDK_INT < 26) {
return;
}
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel("default",
"Channel name",
NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription("Channel description");
notificationManager.createNotificationChannel(channel);
}
private void createNotification(){
Intent intent = new Intent(this,NotificationReceive.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,(int) System.currentTimeMillis(),intent,0);
Notification notification = new Notification.Builder(this)
.setContentTitle("Message");
.setContentText("this is a tittle")
.setSmallIcon(R.drawable.schollo)
.setContentIntent(pendingIntent);
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notification.flags = notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0,notification);
}