Bir istemci uygulamasını sunucudan veya istemciden bir konuya abone edebilirsiniz:
Sunucuda Firebase Admin SDK kullanarak.
İstemcide, uygulamanızdaki istemci tarafı API'sini kullanarak.
Yönetici SDK'sını kullanarak konu aboneliklerini yönetme
Firebase Admin SDK sunucu tarafında temel konu yönetimi görevlerini gerçekleştirmenize olanak tanır. Kayıt jetonları verildiğinde, sunucu mantığını kullanarak istemci uygulaması örneklerine toplu olarak abone olabilir ve abonelikten çıkabilirsiniz.
İstemci uygulaması örneklerini mevcut herhangi bir konuya abone edebilir veya yeni bir konu oluşturabilirsiniz. Bir istemci uygulamasını yeni bir konuya (Firebase projenizde henüz mevcut olmayan bir konu) abone etmek için API'yi kullandığınızda FCM'de bu adla yeni bir konu oluşturulur ve daha sonra herhangi bir istemci bu konuya abone olabilir.
İlgili cihazları bir konuya abone etmek için Firebase Admin SDK abonelik yöntemine kayıt jetonlarının bir listesini iletebilirsiniz:
Node.js
// These registration tokens come from the client FCM SDKs.
const registrationTokens = [
'YOUR_REGISTRATION_TOKEN_1',
// ...
'YOUR_REGISTRATION_TOKEN_n'
];
// Subscribe the devices corresponding to the registration tokens to the
// topic.
getMessaging().subscribeToTopic(registrationTokens, topic)
.then((response) => {
// See the MessagingTopicManagementResponse reference documentation
// for the contents of response.
console.log('Successfully subscribed to topic:', response);
})
.catch((error) => {
console.log('Error subscribing to topic:', error);
});
Java
// These registration tokens come from the client FCM SDKs.
List<String> registrationTokens = Arrays.asList(
"YOUR_REGISTRATION_TOKEN_1",
// ...
"YOUR_REGISTRATION_TOKEN_n"
);
// Subscribe the devices corresponding to the registration tokens to the
// topic.
TopicManagementResponse response = FirebaseMessaging.getInstance().subscribeToTopic(
registrationTokens, topic);
// See the TopicManagementResponse reference documentation
// for the contents of response.
System.out.println(response.getSuccessCount() + " tokens were subscribed successfully");
Python
# These registration tokens come from the client FCM SDKs.
registration_tokens = [
'YOUR_REGISTRATION_TOKEN_1',
# ...
'YOUR_REGISTRATION_TOKEN_n',
]
# Subscribe the devices corresponding to the registration tokens to the
# topic.
response = messaging.subscribe_to_topic(registration_tokens, topic)
# See the TopicManagementResponse reference documentation
# for the contents of response.
print(response.success_count, 'tokens were subscribed successfully')
Go
// These registration tokens come from the client FCM SDKs.
registrationTokens := []string{
"YOUR_REGISTRATION_TOKEN_1",
// ...
"YOUR_REGISTRATION_TOKEN_n",
}
// Subscribe the devices corresponding to the registration tokens to the
// topic.
response, err := client.SubscribeToTopic(ctx, registrationTokens, topic)
if err != nil {
log.Fatalln(err)
}
// See the TopicManagementResponse reference documentation
// for the contents of response.
fmt.Println(response.SuccessCount, "tokens were subscribed successfully")
C#
// These registration tokens come from the client FCM SDKs.
var registrationTokens = new List<string>()
{
"YOUR_REGISTRATION_TOKEN_1",
// ...
"YOUR_REGISTRATION_TOKEN_n",
};
// Subscribe the devices corresponding to the registration tokens to the
// topic
var response = await FirebaseMessaging.DefaultInstance.SubscribeToTopicAsync(
registrationTokens, topic);
// See the TopicManagementResponse reference documentation
// for the contents of response.
Console.WriteLine($"{response.SuccessCount} tokens were subscribed successfully");
Firebase Admin SDK, kayıt jetonlarını uygun yönteme ileterek cihazların bir konunun e-posta listesinden çıkmasına da olanak tanır:
Node.js
// These registration tokens come from the client FCM SDKs.
const registrationTokens = [
'YOUR_REGISTRATION_TOKEN_1',
// ...
'YOUR_REGISTRATION_TOKEN_n'
];
// Unsubscribe the devices corresponding to the registration tokens from
// the topic.
getMessaging().unsubscribeFromTopic(registrationTokens, topic)
.then((response) => {
// See the MessagingTopicManagementResponse reference documentation
// for the contents of response.
console.log('Successfully unsubscribed from topic:', response);
})
.catch((error) => {
console.log('Error unsubscribing from topic:', error);
});
Java
// These registration tokens come from the client FCM SDKs.
List<String> registrationTokens = Arrays.asList(
"YOUR_REGISTRATION_TOKEN_1",
// ...
"YOUR_REGISTRATION_TOKEN_n"
);
// Unsubscribe the devices corresponding to the registration tokens from
// the topic.
TopicManagementResponse response = FirebaseMessaging.getInstance().unsubscribeFromTopic(
registrationTokens, topic);
// See the TopicManagementResponse reference documentation
// for the contents of response.
System.out.println(response.getSuccessCount() + " tokens were unsubscribed successfully");
Python
# These registration tokens come from the client FCM SDKs.
registration_tokens = [
'YOUR_REGISTRATION_TOKEN_1',
# ...
'YOUR_REGISTRATION_TOKEN_n',
]
# Unubscribe the devices corresponding to the registration tokens from the
# topic.
response = messaging.unsubscribe_from_topic(registration_tokens, topic)
# See the TopicManagementResponse reference documentation
# for the contents of response.
print(response.success_count, 'tokens were unsubscribed successfully')
Go
// These registration tokens come from the client FCM SDKs.
registrationTokens := []string{
"YOUR_REGISTRATION_TOKEN_1",
// ...
"YOUR_REGISTRATION_TOKEN_n",
}
// Unsubscribe the devices corresponding to the registration tokens from
// the topic.
response, err := client.UnsubscribeFromTopic(ctx, registrationTokens, topic)
if err != nil {
log.Fatalln(err)
}
// See the TopicManagementResponse reference documentation
// for the contents of response.
fmt.Println(response.SuccessCount, "tokens were unsubscribed successfully")
C#
// These registration tokens come from the client FCM SDKs.
var registrationTokens = new List<string>()
{
"YOUR_REGISTRATION_TOKEN_1",
// ...
"YOUR_REGISTRATION_TOKEN_n",
};
// Unsubscribe the devices corresponding to the registration tokens from the
// topic
var response = await FirebaseMessaging.DefaultInstance.UnsubscribeFromTopicAsync(
registrationTokens, topic);
// See the TopicManagementResponse reference documentation
// for the contents of response.
Console.WriteLine($"{response.SuccessCount} tokens were unsubscribed successfully");
subscribeToTopic()
ve unsubscribeFromTopic()
yöntemleri, FCM'dan gelen yanıtı içeren bir nesneyle sonuçlanır. Dönüş türü, istekte belirtilen kayıt jetonlarının sayısından bağımsız olarak aynı biçime sahiptir.
Hata durumunda (kimlik doğrulama hataları, geçersiz jeton veya konu vb.) bu yöntemler hataya neden olur. Açıklamalar ve çözüm adımları da dahil olmak üzere hata kodlarının tam listesi için Firebase Admin SDK Hatalar bölümüne bakın.
İstemci uygulamanızdan konu aboneliklerini yönetme
İstemci uygulaması örnekleri, Firebase SDK'ları aracılığıyla doğrudan uygulamanızdan konulara abone olabilir veya abonelikten çıkabilir. Aboneliğin başarılı olması için ilk başarısızlık durumunda FCM yeniden deneme yapıldığını unutmayın.
Platformunuzu seçin:
Android
İstemci uygulamaları, mevcut herhangi bir konuya abone olabilir veya yeni bir konu oluşturabilir. Bir istemci uygulaması yeni bir konu adına (Firebase projenizde henüz mevcut olmayan bir ad) abone olduğunda FCM içinde bu ada sahip yeni bir konu oluşturulur ve daha sonra herhangi bir istemci bu konuya abone olabilir.
Bir konuya abone olmak için istemci uygulaması, Firebase Cloud Messaging
subscribeToTopic()
ile FCM konu adını çağırır. Bu yöntem, aboneliğin başarılı olup olmadığını belirlemek için tamamlanma işleyicisi tarafından kullanılabilen bir Task
döndürür:
Kotlin
Firebase.messaging.subscribeToTopic("weather") .addOnCompleteListener { task -> var msg = "Subscribed" if (!task.isSuccessful) { msg = "Subscribe failed" } Log.d(TAG, msg) Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show() }
Java
FirebaseMessaging.getInstance().subscribeToTopic("weather") .addOnCompleteListener(new OnCompleteListener<Void>() { @Override public void onComplete(@NonNull Task<Void> task) { String msg = "Subscribed"; if (!task.isSuccessful()) { msg = "Subscribe failed"; } Log.d(TAG, msg); Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show(); } });
Abonelikten çıkmak için istemci uygulaması, konu adıyla birlikte Firebase Cloud Messaging unsubscribeFromTopic()
çağrısında bulunur.
iOS
İstemci uygulamaları, mevcut herhangi bir konuya abone olabilir veya yeni bir konu oluşturabilir. Bir istemci uygulaması yeni bir konu adına (Firebase projenizde henüz mevcut olmayan bir ad) abone olduğunda FCM içinde bu ada sahip yeni bir konu oluşturulur ve daha sonra herhangi bir istemci bu konuya abone olabilir.
Bir konuya abone olmak için uygulamanızın ana iş parçacığından abonelik yöntemini çağırın (FCM iş parçacığı açısından güvenli değildir). Abonelik isteği ilk başta başarısız olursa FCM otomatik olarak yeniden denenir. Aboneliğin tamamlanamadığı durumlarda, abonelik, tamamlanma işleyicisinde yakalayabileceğiniz bir hata verir.
Swift
Messaging.messaging().subscribe(toTopic: "weather") { error in print("Subscribed to weather topic") }
Objective-C
[[FIRMessaging messaging] subscribeToTopic:@"weather" completion:^(NSError * _Nullable error) { NSLog(@"Subscribed to weather topic"); }];
Bu çağrı, FCM arka uca eşzamansız bir istekte bulunur ve istemciyi belirtilen konuya abone eder. subscribeToTopic:topic
işlevini çağırmadan önce istemci uygulaması örneğinin, geri çağırma didReceiveRegistrationToken
aracılığıyla bir kayıt jetonu aldığından emin olun.
Uygulama her başlatıldığında FCM, istenen tüm konulara abone olunduğundan emin olur. E-posta listesinden çıkmak için unsubscribeFromTopic:topic
numaralı telefonu arayın. FCM, arka planda konu listesinden çıkar.
C++
Bir konuya abone olmak için uygulamanızdan ::firebase::messaging::Subscribe
numaralı telefonu arayın. Bu, FCM arka ucuna eşzamansız bir istek gönderir ve istemciyi belirtilen konuya abone yapar.
::firebase::messaging::Subscribe("example");
Abonelik isteği başlangıçta başarısız olursa FCM, konuya başarıyla abone olana kadar yeniden dener. Uygulama her başlatıldığında FCM, istenen tüm konulara abone olunduğundan emin olur.
E-posta listesinden çıkmak için ::firebase::messaging::Unsubscribe
numaralı telefonu arayın.
FCM, arka planda konu listesinden çıkar.
Unity
Bir konuya abone olmak için uygulamanızdan Firebase.Messaging.FirebaseMessaging.Subscribe
'ı arayın. Bu, FCM arka ucuna eşzamansız bir istek gönderir ve istemciyi belirtilen konuya abone yapar.
Firebase.Messaging.FirebaseMessaging.Subscribe("/topics/example");
Abonelik isteği başlangıçta başarısız olursa FCM, konuya başarıyla abone olana kadar yeniden dener. Uygulama her başlatıldığında FCM, istenen tüm konulara abone olunduğundan emin olur.
Abonelikten çıkmak için Firebase.Messaging.FirebaseMessaging.Unsubscribe
numaralı telefonu arayın. FCM, arka planda konu aboneliğinden çıkma işlemini gerçekleştirir.
Eski sunucu tarafı konu yönetimi (kullanımdan kaldırıldı)
Örnek kimliklerinin ne olduğunu öğrenmek için Örnek kimliği sayfasını ziyaret edin. Kullanımdan kaldırılan uç noktalarla ilgili ayrıntılar için Instance ID API Referansları'na bakın.