更改推送

This commit is contained in:
2024-09-18 17:03:26 +08:00
parent cc463fc7c4
commit 93182c3985
11 changed files with 128 additions and 27 deletions

View File

@@ -21,6 +21,14 @@ android {
vectorDrawables {
useSupportLibrary = true
}
addManifestPlaceholders(
mapOf(
"JPUSH_PKGNAME " to applicationId!!,
"JPUSH_APPKEY" to "ad805ee9f2760376f4f47178",
"JPUSH_CHANNEL" to "developer-default",
)
)
}
buildTypes {
@@ -50,6 +58,7 @@ android {
excludes += "/META-INF/{AL2.0,LGPL2.1}"
}
}
}
dependencies {
@@ -101,5 +110,6 @@ dependencies {
implementation("com.google.firebase:firebase-analytics")
implementation("com.google.firebase:firebase-perf")
implementation("com.google.firebase:firebase-messaging-ktx")
implementation ("cn.jiguang.sdk:jpush:5.4.0") // 必选此处以JPush 5.4.0 版本为例注意5.0.0 版本
}

View File

@@ -46,10 +46,12 @@
<intent-filter>
<action android:name="android.intent.action.SEND" />
<action android:name="android.intent.action.SEND_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
</activity>
<service
android:name=".MyFirebaseMessagingService"
android:exported="false">
@@ -57,6 +59,27 @@
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service
android:name=".JpushService"
android:enabled="true"
android:exported="false"
android:process=":pushcore">
<intent-filter>
<action android:name="cn.jiguang.user.service.action" />
</intent-filter>
</service>
<receiver
android:name=".JpushReciver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="cn.jpush.android.intent.RECEIVER_MESSAGE" />
<category android:name="com.aiosman.riderpro" />
</intent-filter>
</receiver>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.aiosman.riderpro.fileprovider"
@@ -66,8 +89,13 @@
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
<provider
android:name="cn.jpush.android.service.InitProvider"
android:authorities="${applicationId}.jiguang.InitProvider"
android:exported="false"
tools:node="remove"></provider>
</application>
</manifest>

View File

@@ -0,0 +1,41 @@
package com.aiosman.riderpro
import android.content.Context
import android.content.Intent
import android.util.Log
import cn.jpush.android.api.NotificationMessage
import cn.jpush.android.service.JPushMessageReceiver
import com.google.gson.Gson
import com.google.gson.annotations.SerializedName
data class ActionExtra(
@SerializedName("action")
val action: String,
@SerializedName("postId")
val postId: String?,
)
class JpushReciver : JPushMessageReceiver() {
val gson = Gson()
override fun onInAppMessageClick(p0: Context?, p1: NotificationMessage?) {
super.onInAppMessageClick(p0, p1)
// 打开自定义的页面
Log.d("JpushReciver", "onInAppMessageClick")
}
override fun onNotifyMessageOpened(context: Context?, message: NotificationMessage) {
super.onNotifyMessageOpened(context, message)
// 打开自定义的页面
Log.d("JpushReciver", "onNotifyMessageOpened")
val actionExtra = message.notificationExtras?.let {
gson.fromJson(it, ActionExtra::class.java)
}
actionExtra?.postId?.let {
val intent = Intent(context, MainActivity::class.java).apply {
putExtra("POST_ID", it)
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
context?.startActivity(intent)
}
}
}

View File

@@ -0,0 +1,9 @@
package com.aiosman.riderpro
import android.content.Context
import cn.jpush.android.service.JCommonService
class JpushService : JCommonService() {
}

View File

@@ -21,18 +21,16 @@ import androidx.compose.runtime.compositionLocalOf
import androidx.core.content.ContextCompat
import androidx.core.view.WindowCompat
import androidx.lifecycle.ProcessLifecycleOwner
import androidx.lifecycle.viewModelScope
import androidx.navigation.NavHostController
import cn.jiguang.api.utils.JCollectionAuth
import cn.jpush.android.api.JPushInterface
import com.aiosman.riderpro.data.AccountService
import com.aiosman.riderpro.data.AccountServiceImpl
import com.aiosman.riderpro.ui.Navigation
import com.aiosman.riderpro.ui.NavigationRoute
import com.aiosman.riderpro.ui.post.NewPostViewModel
import com.aiosman.riderpro.ui.post.PostViewModel
import com.google.android.libraries.places.api.Places
import com.google.firebase.analytics.FirebaseAnalytics
import com.google.firebase.analytics.ktx.analytics
import com.google.firebase.ktx.Firebase
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
@@ -81,21 +79,33 @@ class MainActivity : ComponentActivity() {
Places.initialize(applicationContext, "AIzaSyDpgLDH1-SECw_pdjJq_msynq1XrxwgKVI")
}
// 初始化 Firebase Analytics
analytics = Firebase.analytics
// analytics = Firebase.analytics
// 请求通知权限
askNotificationPermission()
// 加载一些本地化的配置
AppStore.init(this)
JPushInterface.setDebugMode(true);
// 调整点一初始化代码前增加setAuth调用
JCollectionAuth.setAuth(this, true)
JPushInterface.init(this)
// JPushInterface.setAlias(this, 0, "myTest")
// Log.d("MainActivity", "pushId: $pushId")
enableEdgeToEdge()
scope.launch {
// 检查是否有登录态
val isAccountValidate = getAccount()
var startDestination = NavigationRoute.Login.route
// 如果有登录态,且记住登录状态,且账号有效,则初始化 FCM下一步进入首页
if (AppStore.token != null && AppStore.rememberMe && isAccountValidate) {
Messaging.InitFCM(scope)
Messaging.RegistDevice(scope,this@MainActivity)
startDestination = NavigationRoute.Index.route
}
setContent {

View File

@@ -1,6 +1,8 @@
package com.aiosman.riderpro
import android.content.Context
import android.util.Log
import cn.jpush.android.api.JPushInterface
import com.aiosman.riderpro.data.AccountService
import com.aiosman.riderpro.data.AccountServiceImpl
import com.google.android.gms.tasks.OnCompleteListener
@@ -9,7 +11,21 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
object Messaging {
fun InitFCM(scope: CoroutineScope) {
fun RegistDevice(scope: CoroutineScope, context: Context) {
registerJpush(scope, context)
// registerFCM(scope)
}
fun registerJpush(scope: CoroutineScope, context: Context) {
val accountService: AccountService = AccountServiceImpl()
val regId = JPushInterface.getRegistrationID(context)
scope.launch {
accountService.registerMessageChannel(client = "jpush", identifier = regId)
}
}
fun registerFCM(scope: CoroutineScope) {
val accountService: AccountService = AccountServiceImpl()
FirebaseMessaging.getInstance().token.addOnCompleteListener(OnCompleteListener { task ->
if (!task.isSuccessful) {
@@ -27,4 +43,5 @@ object Messaging {
}
})
}
}

View File

@@ -273,6 +273,7 @@ class GridDragDropState internal constructor(
get() = this.offset + this.size
}
operator fun IntOffset.plus(size: IntSize): IntOffset {
return IntOffset(x + size.width, y + size.height)
}

View File

@@ -4,19 +4,13 @@ import android.widget.Toast
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.LocalMinimumInteractiveComponentEnforcement
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
@@ -24,12 +18,10 @@ import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.scale
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.aiosman.riderpro.AppState
import com.aiosman.riderpro.AppStore
import com.aiosman.riderpro.ErrorCode
@@ -43,9 +35,7 @@ import com.aiosman.riderpro.getErrorMessageCode
import com.aiosman.riderpro.ui.NavigationRoute
import com.aiosman.riderpro.ui.comment.NoticeScreenHeader
import com.aiosman.riderpro.ui.composables.ActionButton
import com.aiosman.riderpro.ui.composables.Checkbox
import com.aiosman.riderpro.ui.composables.CheckboxWithLabel
import com.aiosman.riderpro.ui.composables.StatusBarMaskLayout
import com.aiosman.riderpro.ui.composables.StatusBarSpacer
import com.aiosman.riderpro.ui.composables.TextInputField
import kotlinx.coroutines.Dispatchers
@@ -165,7 +155,7 @@ fun EmailSignupScreen() {
try {
val resp = accountService.getMyAccount()
AppState.UserId = resp.id
Messaging.InitFCM(scope)
Messaging.RegistDevice(scope, context)
} catch (e: ServiceException) {
scope.launch(Dispatchers.Main) {
Toast.makeText(context, "Failed to get account", Toast.LENGTH_SHORT).show()

View File

@@ -85,7 +85,7 @@ fun SignupScreen() {
try {
val resp = accountService.getMyAccount()
AppState.UserId = resp.id
Messaging.InitFCM(coroutineScope)
Messaging.RegistDevice(coroutineScope, context)
} catch (e: ServiceException) {
coroutineScope.launch(Dispatchers.Main) {
Toast.makeText(context, "Failed to get account", Toast.LENGTH_SHORT)

View File

@@ -14,13 +14,9 @@ import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Checkbox
import androidx.compose.material3.CheckboxDefaults
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.LocalMinimumInteractiveComponentEnforcement
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
@@ -34,7 +30,6 @@ import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.credentials.CredentialManager
import com.aiosman.riderpro.AppStore
import com.aiosman.riderpro.LocalNavController
import com.aiosman.riderpro.Messaging
@@ -45,7 +40,6 @@ import com.aiosman.riderpro.data.ServiceException
import com.aiosman.riderpro.ui.NavigationRoute
import com.aiosman.riderpro.ui.comment.NoticeScreenHeader
import com.aiosman.riderpro.ui.composables.ActionButton
import com.aiosman.riderpro.ui.composables.StatusBarMaskLayout
import com.aiosman.riderpro.ui.composables.StatusBarSpacer
import com.aiosman.riderpro.ui.composables.TextInputField
import com.aiosman.riderpro.ui.modifiers.noRippleClickable
@@ -86,7 +80,7 @@ fun UserAuthScreen() {
this.rememberMe = rememberMe
saveData()
}
Messaging.InitFCM(scope)
Messaging.RegistDevice(scope, context)
navController.navigate(NavigationRoute.Index.route) {
popUpTo(NavigationRoute.Login.route) { inclusive = true }
}

View File

@@ -7,3 +7,4 @@ plugins {
id("com.google.firebase.firebase-perf") version "1.4.2" apply false
}