Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我正在尝试实现下推通知,但“无法”;。构建();正在显示错误,而我不';我不知道为什么_Java_Android - Fatal编程技术网

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

这是我的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 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”;您只需在通知中使用此字符串即可。