Firebase Integration
Information about Firebase Messaging Integration
Firebase Implementation
Step 1: Steps for Host Apps with Existing FCM Implementation
Handling Push Message
Use the following code in your FirebaseMessagingService class in method onMessageReceived
Note: Follow these steps if your application has Firebase Cloud Messaging (FCM). If not, you can skip this section.
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getSenderId().equals(SiprocalSDK.getFCMSenderId())) {
SiprocalSDK.handleFCMMessage(remoteMessage.getData().toString()); //can use getApplicationContext()
return;
}else{
//your implementation
}
}
override fun onMessageReceived(message: RemoteMessage) {
super.onMessageReceived(message)
if(message.senderId.equals(SiprocalSDK.getFCMSenderId(applicationContext))){
SiprocalSDK.handleFCMMessage(message.data.toString())
return
}else{
//your implementation
}
}
Handling Token Firebase
Use the following code in your FirebaseMessagingService class in onNewToken:
@Override
public void onNewToken(String s){
super.onNewToken(s);
// your code
SiprocalSDK.refreshFCMToken(applicationContext); //can use getApplicationContext()
}override fun onNewToken(token: String) {
// your code
SiprocalSDK.refreshFCMToken(applicationContext)
}Step 2: Configure Siprocal Server for the Host App
Minimal configuration needs to be done on the Siprocal Server to connect to the host app via FCM. Provide the package name of the host app to SDK provider.
Step 3: Topic Subscription Management (Optional)
Minimal configuration needs to be done on the Siprocal Server to connect to the host app via FCM. Provide the package name of the host app to SDK provider.
The SDK provides helper methods to subscribe and unsubscribe from FCM topics.
Subscribe to a Topic
Use the following method to subscribe the device to a topic:
import com.siprocal.sdk.client.fcm.SiprocalTopicCallback
public class MainActivity extends AppCompatActivity {
@Override
public void onCreate() {
super.onCreate();
SiprocalSDK.subscribeToTopic("tech_news", new SiprocalTopicCallback() {
@Override
public void onSuccess(){
Log.i(TAG, "Successfully subscribed to topic");
}
@Override
public void onError(String e) {
Log.e(TAG, "Error subscribing to topic: " + e);
}
});
}
}import com.siprocal.sdk.client.fcm.SiprocalTopicCallback
class MainActivity : AppCompatActivity() {
override fun onCreate() {
super.onCreate()
SiprocalSDK.subscribeToTopic("tech_news", object : SiprocalTopicCallback {
override fun onSuccess() {
Log.i(TAG, "Successfully subscribed to topic")
}
override fun onError(e: String) {
Log.e(TAG, "Error subscribing to topic: $e")
}
})
}
}Unsubscribe from a Topic
Use the following method to unsubscribe the device from a topic:
import com.siprocal.sdk.client.fcm.SiprocalTopicCallback
public class MainActivity extends AppCompatActivity {
@Override
public void onCreate() {
super.onCreate();
SiprocalSDK.unsubscribeFromTopic("tech_news", new SiprocalTopicCallback() {
@Override
public void onSuccess() {
Log.i(TAG, "Successfully unsubscribed from topic");
}
@Override
public void onError(String e) {
Log.e(TAG, "Error unsubscribing from topic: " + e);
}
});
}
}import com.siprocal.sdk.client.fcm.SiprocalTopicCallback
class MainActivity : AppCompatActivity() {
override fun onCreate() {
super.onCreate()
SiprocalSDK.unsubscribeFromTopic("tech_news", object : SiprocalTopicCallback {
override fun onSuccess() {
Log.i(TAG, "Successfully unsubscribed from topic")
}
override fun onError(e: String) {
Log.e(TAG, "Error unsubscribing from topic: $e")
}
})
}
}Subscribe to Multiple Topics
import com.siprocal.sdk.client.fcm.SiprocalMultipleTopicCallback
import com.siprocal.sdk.client.fcm.TopicResult
public class MainActivity extends AppCompatActivity {
@Override
public void onCreate() {
super.onCreate();
List<String> topics = Arrays.asList("sports", "music", "cinema");
SiprocalSDK.subscribeToMultipleTopics(topics, new SiprocalMultipleTopicCallback() {
@Override
public void onCompleted(Map<String, TopicResult> result) {
for (Map.Entry<String, TopicResult> entry : result.entrySet()) {
String topicName = entry.getKey();
TopicResult status = entry.getValue();
if (status.isSuccess()) {
Log.i(TAG, "✅ Successfully subscribed to: " + topicName);
} else {
Log.e(TAG, "❌ Failed to subscribe to: " + topicName +
". Reason: " + status.getMessage());
}
}
}
@Override
public void onError(String e) {
Log.e(TAG, "Error handling multiple subscriptions: " + e);
}
});
}
}import com.siprocal.sdk.client.fcm.SiprocalMultipleTopicCallback
import com.siprocal.sdk.client.fcm.TopicResult
class MainActivity : AppCompatActivity() {
override fun onCreate() {
super.onCreate()
val topics = listOf("sports", "music", "cinema")
SiprocalSDK.subscribeToMultipleTopics(topics, object : SiprocalMultipleTopicCallback {
override fun onCompleted(result: Map<String, TopicResult>) {
result.forEach { (topic, status) ->
if (status.isSuccess) {
Log.i("SiprocalSDK", "✅ Successfully subscribed to: $topic")
} else {
Log.e("SiprocalSDK", "❌ Failed to subscribe to: $topic. Reason: ${status.message}")
}
}
}
override fun onError(e: String) {
Log.e(TAG, "Error handling multiple subscriptions: $e")
}
})
}
}Unsubscribe from Multiple Topics
import com.siprocal.sdk.client.fcm.SiprocalMultipleTopicCallback
import com.siprocal.sdk.client.fcm.TopicResult
public class MainActivity extends AppCompatActivity {
@Override
public void onCreate() {
super.onCreate();
List<String> topics = Arrays.asList("sports", "music", "cinema");
SiprocalSDK.unsubscribeFromMultipleTopics(topics, new SiprocalMultipleTopicCallback() {
@Override
public void onCompleted(Map<String, TopicResult> result) {
for (Map.Entry<String, TopicResult> entry : result.entrySet()) {
String topic = entry.getKey();
TopicResult status = entry.getValue();
if (status.isSuccess()) {
Log.i(TAG, "✅ Unsubscribed from: " + topic);
} else {
Log.e(TAG, "❌ Failed to unsubscribe from: " + topic +
". Reason: " + status.getMessage());
}
}
}
@Override
public void onError(String e) {
Log.e(TAG, "Error handling multiple unsubscriptions: " + e);
}
});
}
}import com.siprocal.sdk.client.fcm.SiprocalMultipleTopicCallback
import com.siprocal.sdk.client.fcm.TopicResult
class MainActivity : AppCompatActivity() {
override fun onCreate() {
super.onCreate()
val topics = listOf("sports", "music", "cinema")
SiprocalSDK.unsubscribeFromMultipleTopics(topics, object : SiprocalMultipleTopicCallback {
override fun onCompleted(result: Map<String, TopicResult>) {
result.forEach { (topic, status) ->
if (status.isSuccess) {
Log.i("SiprocalSDK", "✅ Unsubscribed from: $topic")
} else {
Log.e(
"SiprocalSDK",
"❌ Failed to unsubscribe from: $topic. Reason: ${status.message}"
)
}
}
}
override fun onError(e: String) {
Log.e(TAG, "Error handling multiple unsubscriptions: $e")
}
})
}
}