Java 我正在尝试实现下推通知,但“无法”;。构建();正在显示错误,而我不';我不知道为什么
这是我的java文件Java 我正在尝试实现下推通知,但“无法”;。构建();正在显示错误,而我不';我不知道为什么,java,android,Java,Android,这是我的java文件 public class two extends Fragment { EditText ed1, ed2, ed3; public two() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedI
public class two extends Fragment {
EditText ed1, ed2, ed3;
public two() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_two, container, false);
ed1 = (EditText) view.findViewById(R.id.editText);
ed2 = (EditText) view.findViewById(R.id.editText2);
ed3 = (EditText) view.findViewById(R.id.editText3);
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_two, container, false);
}
public void sendNotification(View v){
String Name = ed1.getText().toString().trim();
String Tittle = ed2.getText().toString().trim();
String Question = ed3.getText().toString().trim();
NotificationManager notif = (NotificationManager)getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
Notification n = new Notification.Builder(getActivity().getApplicationContext()).
setContentText(Question).
setContentTitle(Tittle + "-" + Name).
setSmallIcon(R.drawable.map).build();
notif.notify(0,n);
}
}
显示错误的倒数第二行的build()不知道为什么请告诉我该怎么办谢谢你可以试试这个
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Notification Title")
.setContentText("Notification Content")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setShowWhen(true)
.setChannelId(channelId)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(YOUR_NOTIFICATION_ID, mBuilder.build());
这里我使用了NotificationCompat
而不是Notification
您可以为
您的通知\u ID
指定任何整数值,为channelId
指定字符串值“显示错误”-什么错误?在哪里使用build()
以及在哪里调用sendNotification()
方法?我尝试了构建(),但是channelId给了我错误。。我是java新手,所以我可能不知道如何操作。您可以简单地添加channelID,如下所示,String channelID=“channel\u id\u notification”;您只需在通知中使用此字符串即可。