我试图实施下推通知但是" .build()"显示错误,我不知道为什么

时间:2018-05-09 10:28:09

标签: 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 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()显示错误不知道为什么我该怎么办谢谢

1 个答案:

答案 0 :(得分:1)

你可以尝试这个..

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

您可以将任何整数值提供给YOUR_NOTIFICATION_ID,将字符串值提供给channelId