Push Messages

Last updated 2 months ago

Before you can start receiving messages through FCM, you need to setup FCM in your project.

Firebase has a pretty comprehensive tutorial for setting up FCM. Follow the steps over here and you should be ready to receive messages via FCM.

Now that you’ve configured FCM in your project you can hook it up with Mitter using the following steps:

Register a delivery endpoint

Before receiving any messages from Mitter you need to register the device’s FCM token as a delivery endpoint with Mitter.

You can get and register the FCM token by calling the registerFcmToken() function on the Mitter object within the function which has the signature of didRegisterForRemoteNotificationsWithDeviceToken variable, like this:

InstanceID.instanceID().instanceID { (result, error) in
if let error = error {
print("Error fetching remote instange ID: \(error)")
} else if let result = result {
print("Remote instance ID token: \(result.token)")
self.mitter.registerFcmToken(token: result.token) {
result in
switch result {
case .success(let deliveryEndpoint):
print("Endpoint is: \(deliveryEndpoint.serializedEndpoint)")
case .error:
print("Unable to register endpoint!")
}
}
}
}

After that, you need to process incoming FCM messages by forwarding them to Mitter. Look for the function called userNotificationCenter() which has the variable named willPresent and then get the serialised data dictionary from the FCM notification dictionary, like this:

let messageString = userInfo["data"] as! String

Next, you need to convert this into a MessagingPipelinePayload object. This can be done by passing the serialised dictionary from the previous step, like this:

let messagingPipelinePayload = mitter.parseFcmMessage(data: messageString)

Now that you have the MessagingPipelinePayload object, you can check if the message is from Mitter by calling the function isMitterMessage() which returns Bool.

Next, you need to process the push message by passing the MessagingPipelinePayload object and hooking up the completion handlers, like this:

if mitter.isMitterMessage(messagingPipelinePayload) {
let payload = mitter.processPushMessage(messagingPipelinePayload!)
switch payload {
case .NewMessagePayload(let message, let channelId):
print("Received Message: \(message), for Channel: \(channelId)")
default:
print("Nothing to print!")
}
}

Here, payload is an enum which has various cases like NewMessagePayload, NewChannelPayload, etc.

Do note, for FCM messages to work, you need to run the project in a physical iOS device.

Also, you need to include dataType as cloud-notification on any message that choose to send using a direct API call from Postman or any other REST client.