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 false
3}

build.gradle (App)

Reklam · TEST
AdSense Test ReklamıYazı arası (gönderi içi) · Yazı arası
1plugins {
2id 'com.android.application'
3id 'com.google.gms.google-services'
4}
5
6dependencies {
7implementation 'com.google.firebase:firebase-messaging:23.4.1'
8}

AndroidManifest.xml

1<service
2android: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.FirebaseMessagingService
2import com.google.firebase.messaging.RemoteMessage
3import android.app.NotificationChannel
4import android.app.NotificationManager
5import androidx.core.app.NotificationCompat
6
7class MyFirebaseMessagingService: FirebaseMessagingService() {
8override fun onMessageReceived(remoteMessage: RemoteMessage) {
9val title = remoteMessage.notification?.title?: "Başlık"
10val body = remoteMessage.notification?.body?: "İçerik"
11
12showNotification(title, body)
13}
14
15override fun onNewToken(token: String) {
16super.onNewToken(token)
17println("Yeni token: $token")
18// Token'ı sunucunuza gönderin
19}
20
21private fun showNotification(title: String, body: String) {
22val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
23val channelId = "fcm_channel"
24
25if (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}
29
30val notification = NotificationCompat.Builder(this, channelId).setSmallIcon(android.R.drawable.ic_dialog_info).setContentTitle(title).setContentText(body).setAutoCancel(true).build()
31
32notificationManager.notify(1, notification)
33}
34}
35[/cod]
36
37**MainActivity.kt**
38
39[kod]
40import com.google.firebase.messaging.FirebaseMessaging
41
42class MainActivity: AppCompatActivity() {
43override fun onCreate(savedInstanceState: Bundle?) {
44super.onCreate(savedInstanceState)
45setContentView(R.layout.activity_main)
46
47FirebaseMessaging.getInstance().token.addOnCompleteListener { task ->
48if (task.isSuccessful) {
49val token = task.result
50println("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.

Reklam · TEST
AdSense Test ReklamıGönderi altı · Görüntülü
konu hakkında tepkinizi verebilirsiniz