im 当前尝试在我的应用程序中推送通知。 我有 2 个带有通知的 AsyncTask,但是当我在第二个日志上执行两个时只显示,但是当我评论第二个时,第一个显示我知道我的 API 工作正常
第一个异步任务:
public class backWorkerNotifTom extends AsyncTask<String,String,String> {
Context context;
String result;
String[] BookTittle;
public backWorkerNotifTom(Context context){this.context = context;}
@Override
protected String doInBackground(String... params) {
String type = params[0];
String NotifTomURL = "http://192.168.254.120/LibrayAPI/SelectNotifTom.php";
if (type.equals("SelectNotifTom")){
String dateTom = params[1];
String borrowerID = params[2];
try {
String data = URLEncoder.encode("date_tom","UTF-8") + "=" +
URLEncoder.encode(dateTom,"UTF-8");
data += "&" + URLEncoder.encode("borrower_id","UTF-8") + "=" +
URLEncoder.encode(borrowerID,"UTF-8");
URL url = new URL(NotifTomURL);
URLConnection urlConnection = url.openConnection();
urlConnection.setDoOutput(true);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(urlConnection.getOutputStream());
outputStreamWriter.write(data);
outputStreamWriter.flush();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = bufferedReader.readLine())!= null){
sb.append(line+"n");
}
result = sb.toString();
JSONArray jsonArray = new JSONArray(result);
JSONObject jsonObject;
BookTittle = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
jsonObject = jsonArray.getJSONObject(i);
BookTittle[i] = jsonObject.getString("BookTittle");
// PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(context, NotifList.class), 0);
NotificationCompat.Builder mbuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_book_black_24dp)
.setContentTitle("Library Alert")
.setContentText("Tommorow is the Due Day of the Book " + BookTittle[i] + " You Borrowed")
//.setContentIntent(pendingIntent)
.setTicker("Notifications");
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
if (notificationManager != null) {
notificationManager.notify(i, mbuilder.build());
}
}
}
catch (Exception ex){
ex.printStackTrace();
}
}
return result;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
}
}
第二个语法任务:
public class backWorkerNotif extends AsyncTask<String, String, String> {
String result = null;
String[] BookTittle;
Context context;
public backWorkerNotif(Context context){this.context = context;}
@Override
protected String doInBackground(String... params) {
String selectNotif_url = "http://192.168.254.120/LibrayAPI/SelectNotif.php";
String type = params[0];
if (type.equals("Notif")) {
String DateNow = params[1];
String BorrowerID = params[2];
try {
String data = URLEncoder.encode("date_now", "UTF-8") + "=" +
URLEncoder.encode(DateNow, "UTF-8");
data += "&" + URLEncoder.encode("borrower_id", "UTF-8") + "=" +
URLEncoder.encode(BorrowerID, "UTF-8");
URL url = new URL(selectNotif_url);
URLConnection urlConnection = url.openConnection();
urlConnection.setDoOutput(true);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(urlConnection.getOutputStream());
outputStreamWriter.write(data);
outputStreamWriter.flush();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
sb.append(line + "n");
}
result = sb.toString();
if (result.isEmpty()) {
} else {
//Json
JSONArray jsonArray = new JSONArray(result);
JSONObject jsonObject;
BookTittle = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
jsonObject = jsonArray.getJSONObject(i);
BookTittle[i] = jsonObject.getString("BookTittle");
// PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(context, NotifList.class), 0);
NotificationCompat.Builder mbuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_book_black_24dp)
.setContentTitle("Library notification")
.setContentText("Today is the Due Day of the Book " + BookTittle[i] + " You Borrowed")
//.setContentIntent(pendingIntent)
.setTicker("Notification Alert");
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
if (notificationManager != null) {
notificationManager.notify(i, mbuilder.build());
}
}
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String jsonArray) {
super.onPostExecute(jsonArray);
}
}
登录课程
public class Login extends AppCompatActivity {
EditText username,password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
username = findViewById(R.id.edtUsername);
password = findViewById(R.id.edtPassword);
}
public void onLogin(View view) {
String Username = username.getText().toString();
String Password = password.getText().toString();
//notif call
//call notif for overdue tommorow
backWorkerNotifTom backWorkerNotifTom = new backWorkerNotifTom(this);
//get datetime tom
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_YEAR,1);
Date dateTom = calendar.getTime();
SimpleDateFormat sf1 = new SimpleDateFormat("yyyy-MM-dd");
String dateTomString = sf1.format(dateTom);
Toast.makeText(this, dateTomString, Toast.LENGTH_SHORT).show();
backWorkerNotifTom.execute("SelectNotifTom",dateTomString,Username);
backWorkerNotif backWorkerNotif = new backWorkerNotif(this);
Date date = Calendar.getInstance().getTime();
SimpleDateFormat SF = new SimpleDateFormat("yyyy-MM-dd");
String DateNow = SF.format(date);
backWorkerNotif.execute("Notif", DateNow, Username);
String Type = "login";
GlobalVariable.BorrowerID = Username;
GlobalVariable.Password = Password;
backgroundWorker _backgroundWorker = new backgroundWorker(this);
_backgroundWorker.execute(Type, Username, Password);
}
}
这可能是因为您在创建两种类型时使用相同的 ID 的通知。这将导致第二个覆盖第一个。如果要显示多个通知,则每个通知都需要一个单独的 ID。
在两个异步任务中都notificationManager.notify(i, mbuilder.build());
有问题的代码行。因为 'i' 被创建为数组长度从 0 开始的 int,所以这两个任务之间会有很多冲突。您需要确保每个通知都使用唯一的 ID