Özet - Push bildirimler
- React Native kursu
- İleri seviye
- Ders 6/83
- 5 dk
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
import * as Notifications from 'expo-notifications';
export async function scheduleReminder() {
// otuz saniye sonra görev bildirimi planla
}Örnek çözüm
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
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
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
const token = await Notifications.____({ projectId: 'my-project' });
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 |