Sıfırdan Push Notification Sistemi: Firebase Cloud Messaging
Push notification, kullanıcıyı uygulamadan haberdar etmenin en etkili yolu. Firebase Cloud Messaging (FCM) kullanarak bunu 10 dakikada ayağa kaldırabiliriz.
İlk olarak Firebase Console'dan projenizi oluşturun ve Android uygulamasını ekleyin. SHA-1 imzanızı almayı unutmayın. Ardından google-services.json dosyasını app klasörüne koyun.
build.gradle (Project)
1plugins {2id 'com.google.gms.google-services' version '4.4.0' apply false3}build.gradle (App)
1plugins {2id 'com.android.application'3id 'com.google.gms.google-services'4}56dependencies {7implementation 'com.google.firebase:firebase-messaging:23.4.1'8}AndroidManifest.xml
1<service2android:name=".MyFirebaseMessagingService"3android:exported="false">4<intent-filter>5<action android:name="com.google.firebase.MESSAGING_EVENT" />6</intent-filter>7</service>MyFirebaseMessagingService.kt
1import com.google.firebase.messaging.FirebaseMessagingService2import com.google.firebase.messaging.RemoteMessage3import android.app.NotificationChannel4import android.app.NotificationManager5import androidx.core.app.NotificationCompat67class MyFirebaseMessagingService: FirebaseMessagingService() {8override fun onMessageReceived(remoteMessage: RemoteMessage) {9val title = remoteMessage.notification?.title?: "Başlık"10val body = remoteMessage.notification?.body?: "İçerik"1112showNotification(title, body)13}1415override fun onNewToken(token: String) {16super.onNewToken(token)17println("Yeni token: $token")18// Token'ı sunucunuza gönderin19}2021private fun showNotification(title: String, body: String) {22val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager23val channelId = "fcm_channel"2425if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {26val channel = NotificationChannel(channelId, "FCM Notifications", NotificationManager.IMPORTANCE_HIGH)27notificationManager.createNotificationChannel(channel)28}2930val notification = NotificationCompat.Builder(this, channelId).setSmallIcon(android.R.drawable.ic_dialog_info).setContentTitle(title).setContentText(body).setAutoCancel(true).build()3132notificationManager.notify(1, notification)33}34}35[/cod]3637**MainActivity.kt**3839[kod]40import com.google.firebase.messaging.FirebaseMessaging4142class MainActivity: AppCompatActivity() {43override fun onCreate(savedInstanceState: Bundle?) {44super.onCreate(savedInstanceState)45setContentView(R.layout.activity_main)4647FirebaseMessaging.getInstance().token.addOnCompleteListener { task ->48if (task.isSuccessful) {49val token = task.result50println("FCM Token: $token")51}52}53}54}Beklenen Çıktı:
Uygulamayı çalıştırdığınızda Logcat'te "FCM Token: eJzd..." şeklinde token görürsünüz. Firebase Console > Cloud Messaging sekmesinden bu token'a test mesajı gönderin. Uygulama açıkken bildirim görünecektir.
Sık Yapılan Hata: google-services.json dosyasını app klasörüne koymayı unutmak. Ayrıca Android 13+ için POST_NOTIFICATIONS izni gerekir, AndroidManifest.xml'e ekleyin.
0 yorum