gpt4 book ai didi

java - 我正在尝试实现下推通知,但 ".build()"显示错误,我不知道为什么

转载 作者:行者123 更新时间:2023-12-02 11:16:20 25 4
gpt4 key购买 nike

这是我的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

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

关于java - 我正在尝试实现下推通知,但 ".build()"显示错误,我不知道为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50251138/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com