使用安卓上传服务时出现"Bad notification for startForeground"错误



我不知道为什么会出现此错误,因为我实际上没有在我的应用程序中使用任何通知通道,而且我不打算这样做。 有人可以帮我解决这个问题吗?

错误:

2019-12-10 16:58:03.151 9542-9542/com.example.demoproject E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.demoproject, PID: 9542
android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: null
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1894)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7156)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)

活动:

public class File_upload extends AppCompatActivity {
Button SelectButton, UploadButton;
EditText PdfNameEditText ;
Uri uri;
String username;
public static final String PDF_UPLOAD_HTTP_URL = "https://depcollproj.000webhostapp.com/FileUpload.php";
public int PDF_REQ_CODE = 1;
String PdfNameHolder, PdfPathHolder, PdfID;
@Override
public void onBackPressed() {
Log.d("CDA", "onBackPressed Called");
Intent setIntent = new Intent(getApplicationContext(),TeacherView.class);
setIntent.putExtra("username",username);
startActivity(setIntent);
finish();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_file_upload);
AllowRunTimePermission();
Intent i=getIntent();
username=i.getStringExtra("username");
SelectButton = (Button) findViewById(R.id.button);
UploadButton = (Button) findViewById(R.id.button2);
PdfNameEditText = (EditText) findViewById(R.id.editText);
SelectButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// PDF selection code start from here .
Intent intent = new Intent();
intent.setType("application/pdf");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Pdf"), PDF_REQ_CODE);
}
});
UploadButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
PdfUploadFunction();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PDF_REQ_CODE && resultCode == RESULT_OK && data != null && data.getData() != null) {
uri = data.getData();
SelectButton.setText("PDF is Selected");
}
}
public void PdfUploadFunction() {
PdfNameHolder = PdfNameEditText.getText().toString().trim();
PdfPathHolder = FilePath.getPath(this, uri);
if (PdfPathHolder == null) {
Toast.makeText(this, "Please move your PDF file to internal storage & try again.", Toast.LENGTH_LONG).show();
} else {
try {
PdfID = UUID.randomUUID().toString();
new MultipartUploadRequest(this, PdfID, PDF_UPLOAD_HTTP_URL)
.addFileToUpload(PdfPathHolder, "pdf")
.addParameter("name", PdfNameHolder)
//.addParameter("username",username)
.setNotificationConfig(new UploadNotificationConfig())
.setMaxRetries(5)
.startUpload();
Toast.makeText(this, " Upload Successful ", Toast.LENGTH_SHORT).show();
Log.d("Upload Procedure", "PdfUploadFunction:  -------"+PdfNameHolder+username);
}
catch (Exception exception) {
Log.d("Upload Procedure", "PdfUploadFunction: Fail");
Toast.makeText(this, exception.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}

public void AllowRunTimePermission(){
if (ActivityCompat.shouldShowRequestPermissionRationale(File_upload.this, Manifest.permission.READ_EXTERNAL_STORAGE)) {
Toast.makeText(File_upload.this,"READ_EXTERNAL_STORAGE permission Access Dialog", Toast.LENGTH_LONG).show();
} else {
ActivityCompat.requestPermissions(File_upload.this,new String[]{ Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
}
}
@Override
public void onRequestPermissionsResult(int RC, String per[], int[] Result) {
switch (RC) {
case 1:
if (Result.length > 0 && Result[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(File_upload.this,"Permission Granted", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(File_upload.this,"Permission Canceled", Toast.LENGTH_LONG).show();
}
break;
}
}
}

你能试试下面的答案吗

尝试更新以下内容:

new MultipartUploadRequest(this, PdfID, PDF_UPLOAD_HTTP_URL)
.addFileToUpload(PdfPathHolder, "pdf")
.addParameter("name", PdfNameHolder)
//.addParameter("username",username)
.setNotificationConfig(new UploadNotificationConfig())
.setMaxRetries(5)
.startUpload();

对此:

MultipartUploadRequest uploadRequest = new MultipartUploadRequest(this, PdfID, PDF_UPLOAD_HTTP_URL)
.addFileToUpload(PdfPathHolder, "pdf")
.addParameter("name", PdfNameHolder)
//.addParameter("username",username)
.setMaxRetries(5);
// For Android > 8, we need to set an Channel to the UploadNotificationConfig.
// So, here, we create the channel and set it to the MultipartUploadRequest
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager notificationManager = (NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel("Upload", "Upload service", NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
UploadNotificationConfig notificationConfig = new UploadNotificationConfig();
notificationConfig.setNotificationChannelId("Upload")
uploadRequest.setNotificationConfig(notificationConfig)
} else {
// If android < Oreo, just set a simple notification (or remove if you don't wanna any notification
// Notification is mandatory for Android > 8
uploadRequest.setNotificationConfig(new UploadNotificationConfig())
}
uploadRequest.startUpload();

原因

出现问题是因为您拥有的上传库启动了服务。在 Android 8 上,前台服务需要通知,而这些通知需要通道。当您使用setNotificationConfig(new UploadNotificationConfig())时,您正在设置通知,但您没有添加任何导致您观察到的崩溃的通道。

因此,为了修复,您必须创建UploadNotificationConfig,设置通知通道,然后,您可以将其设置为上传请求。之后才开始上传。

编辑

通道 ID 逻辑已添加到该库的版本 3.4 上。因此,您必须更新 build.gradle 才能至少使用 3.4 版。

implementation 'net.gotev:uploadservice:3.4'

最新更新