CodeBlanks Logo Icon CodeBlanks

Özet - Push bildirimler

token, yerel ve uzak bildirimi birleştirerek bir bildirim sistemi yazarsın.

Özet - Bildirimler

Bu grupta expo-notifications paketini kurmayı, push token almayı, yerel bildirim planlamayı, sunucudan uzak bildirim göndermeyi ve bildirim olaylarını dinlemeyi gördün. Bildirimler uygulama kapalıyken bile kullanıcıya ulaşan güçlü bir kanaldır.

Özette üç parçayı birleştireceğiz: token alma, yerel bildirim zamanlama ve olay dinleyicisi kurma. Bu üçü birlikte gerçek bir bildirim akışı oluşturur. Şimdi bunları tek bir uygulamada birleştirelim.

Uygulama Hatırlatma planla

scheduleReminder işlevini tamamla. Notifications.scheduleNotificationAsync çağır. content: {{ title: 'Hatırlatma', body: 'Vakti geldi', data: {{ taskId: 42 }} }} ve trigger: {{ seconds: 30 }} ver. await ile bekle.

Başlangıç kodu

App.js
import * as Notifications from 'expo-notifications';

export async function scheduleReminder() {
  // otuz saniye sonra görev bildirimi planla
}

Örnek çözüm

App.js
await Notifications.scheduleNotificationAsync({
  content: { title: 'Hatırlatma', body: 'Vakti geldi', data: { taskId: 42 } },
  trigger: { seconds: 30 },
});

Uygulama useEffect ile dinleyici kur

Boş useEffect'i tamamla. const sub = Notifications.addNotificationResponseReceivedListener((response) => console.log(response.notification.request.content.data)) ile bir yanıt dinleyicisi kur. return () => sub.remove(); ile temizle. Bağımlılık dizisi [] olsun.

Başlangıç kodu

App.js
import React from 'react';
import * as Notifications from 'expo-notifications';

export function useReminderTap() {
  React.useEffect(() => {
    // yanıt dinleyicisi kur ve temizlik döndür
  }, []);
}

Örnek çözüm

App.js
const sub = Notifications.addNotificationResponseReceivedListener((response) => {
  console.log(response.notification.request.content.data);
});
return () => sub.remove();

Soru ve cevaplar

Dersin sorularını cevaplarıyla birlikte inceleyebilirsin. Kendini denemek istersen dersi interaktif olarak çöz.

1 Token alma işlevini yaz

Soru
const token = await Notifications.____({ projectId: 'my-project' });
Cevap
const token = await Notifications.getExpoPushTokenAsync({ projectId: 'my-project' });

Açıklama: Bu işlev bir Expo token'ı döndürür.

2 Akış parçasını rolüne eşle

Kavram Anlamı
getExpoPushTokenAsync Token alır
scheduleNotificationAsync Yerel bildirim planlar
addNotificationReceivedListener Olay dinler
sub.remove() Dinleyiciyi kaldırır

İleri seviye bölümündeki tüm dersler