当从 RN 0.71 切换到 0.74 并相应地 API 33->34 时,出现了问题
致命异常:主要 进程:com.compassionclips,PID:30138 java.lang.RuntimeException:无法启动服务 com.asterinet.react.bgactions.RNBackgroundActionsTask
我更新了此问题的帮助https://react-native-community.github.io/upgrade-helper/?from=0.71.3&to=0.74.2 但我没有得到工作版本 - 当我运行应用程序时,我得到(当工作后台服务时)“继续停止”
我的MainActivity.kt
package com.goapp
import com.facebook.react.ReactActivity
import com.facebook.react.ReactActivityDelegate
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.fabricEnabled
import com.facebook.react.defaults.DefaultReactActivityDelegate
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.ContentResolver
import android.media.AudioAttributes
import android.net.Uri
import android.os.Build
import android.os.Bundle
import androidx.core.app.NotificationCompat
// import android.app.PendingIntent;
// import android.app.Service;
// import android.content.Context;
// import android.app.Intent;
// import androidx.core.app.ServiceCompat
class MainActivity : ReactActivity() {
/**
* Returns the name of the main component registered from JavaScript. This is used to schedule
* rendering of the component.
*/
override fun getMainComponentName(): String = "GoApp"
/**
* добавление типов уведомлений
*/
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createNotificationChannel("notification", "avengers")
createNotificationChannel("cancel", "cancel_order")
createNotificationChannel("order_ready", "order_ready")
}
}
private fun createNotificationChannel(channelId: String, soundFileName: String) {
val notificationChannel = NotificationChannel(
channelId,
"goapp",
NotificationManager.IMPORTANCE_HIGH
).apply {
setShowBadge(true)
description = ""
val att = AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
.build()
setSound(Uri.parse("${ContentResolver.SCHEME_ANDROID_RESOURCE}://${packageName}/raw/$soundFileName"), att)
enableVibration(true)
vibrationPattern = longArrayOf(400, 400)
lockscreenVisibility = NotificationCompat.VISIBILITY_PUBLIC
}
val manager = getSystemService(NotificationManager::class.java)
manager.createNotificationChannel(notificationChannel)
//if i add this -> BUILD FAILED
// if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
// startForeground(SERVICE_ID, notification)
// } else {
// startForeground(SERVICE_ID, notification,
// FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK)
// }
//
}
override fun createReactActivityDelegate(): ReactActivityDelegate =
DefaultReactActivityDelegate(this, mainComponentName, fabricEnabled)
}
这是我的 AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS" tools:ignore="ProtectedPermissions" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<application
android:name=".MainApplication"
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round"
android:allowBackup="false"
android:theme="@style/AppTheme">
<!-- <service
android:name=".MainActivity"
android:foregroundServiceType="mediaPlayback"
android:exported="true"
/> -->
<!-- if add this also -> BUILD FAILED -->
...
我做错了什么,请帮忙🥲
API级别34或更高级别要求所有前台服务声明服务类型。类型取决于服务的功能。像这样的东西:
<application ... >
...
<service
android:name="com.asterinet.react.bgactions.RNBackgroundActionsTask"
android:foregroundServiceType="shortService"/>
...
</application>
有关详细信息,请参阅在清单中声明前台服务和INSTALL.md。