我正在尝试使用 Kotlin 在我的应用上实现 Firebase Cloud 消息传递。
在MyFirebaseMessagingService.kt中,我收到消息"MyFirebaseMessagingService未在清单中注册,
此外,我的应用程序在构建时不断停止。
我花了一整天的时间寻找解决方案,但还没有找到。
我尝试添加几个服务,但没有成功。
(如:"MyFirebaseMessagingService">
"。MyFirebaseMessagingService">
".java。MyFirebaseMessagingService">
".火力基地。MyFirebaseMessagingService">
安卓清单.xml
<service
android:name="MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>`
MyFirebaseMessagingService.kt
import android.annotation.SuppressLint
import android.util.Log
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
import android.app.NotificationManager
import android.media.RingtoneManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import androidx.core.app.NotificationCompat
import com.example.recapp.MainActivity
import com.example.recapp.R
import com.google.firebase.iid.InstanceIdResult
import com.google.android.gms.tasks.OnSuccessListener
import com.google.firebase.iid.FirebaseInstanceId
class MyFirebaseMessagingService : FirebaseMessagingService(){
val TAG = "FirebaseMessagingService"
override fun onNewToken(token: String) {
Log.d("New_Token", token)
}
@SuppressLint("LongLogTag")
override fun onMessageReceived(remoteMessage: RemoteMessage) {
super.onMessageReceived(remoteMessage)
Log.d(TAG, "Poruka: ${remoteMessage.from}")
if (remoteMessage.notification != null) {
Log.d(TAG, "Sadrzaj: ${remoteMessage.notification?.body}")
sendNotification(remoteMessage)
}
}
private fun sendNotification(remoteMessage: RemoteMessage) {
val intent = Intent(this, MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
val pendingIntent = PendingIntent.getActivity(this@MyFirebaseMessagingService, 0, intent, PendingIntent.FLAG_ONE_SHOT)
val soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notificationBuilder = NotificationCompat.Builder(this@MyFirebaseMessagingService, "Notification")
.setSmallIcon(com.example.recapp.R.mipmap.ic_launcher)
.setContentTitle("Poruka")
.setContentText(remoteMessage.notification?.body)
.setAutoCancel(true)
.setSound(soundUri)
.setContentIntent(pendingIntent)
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(0, notificationBuilder.build())
}
}
应用构建没有错误,但在构建时会不断停止。
构建 gradle (myApp( 可以吗?
dependencies {
classpath 'com.android.tools.build:gradle:3.4.2'
classpath 'com.google.gms:google-services:4.3.0'
构建 gradle (应用程序( 可以吗?
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.core:core-ktx:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.0.0'
implementation 'com.google.firebase:firebase-core:17.0.1'
implementation 'com.google.firebase:firebase-messaging:19.0.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
apply plugin: 'com.google.gms.google-services'
您必须声明服务的全名。例如,如果您的服务在包 com.example
中,则它将com.example.MyFirebaseMessagingService
<service
android:name="com.example.MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
在build.gradle级别应用程序中的Firebase分支:
implementation 'com.google.firebase:firebase-analytics:17.4.3'
implementation 'com.google.firebase:firebase-core:17.4.3'
implementation 'com.google.firebase:firebase-iid:20.2.1'
implementation 'com.google.firebase:firebase-messaging:20.2.1'
和清单
<service
android:name = "com.aplikasipdam.lampupanggungcirebon.MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>