ably / docs

Ably Realtime API documentation
https://ably.com/docs
Apache License 2.0
20 stars 41 forks source link

Document how to use FCM / Firebase SDK with Ably #680

Open mattheworiordan opened 5 years ago

mattheworiordan commented 5 years ago

This is from internal chats.

We need to:

On iOS

Ok, I read about FCM and it is possible to get a FCM registration token by using the Firebase iOS SDK in the app You can use it to register the device manually like I shared here, that's basically what Push.activate does

let deviceDetails = ARTDeviceDetails(id: ULID().ulidString)
deviceDetails.secret = generateSecret()
deviceDetails.platform = "android" //or "ios"
deviceDetails.formFactor = "tablet" //or "phone", "tv", "watch", "desktop", "tablet", "car"
deviceDetails.clientId = "clientA"
deviceDetails.metadata = NSMutableDictionary()
deviceDetails.push.recipient = [
    "transportType": "gcm", //or "apns"
    "registrationToken": "xx"
]

rest.push.admin.deviceRegistrations.save(deviceDetails) { error in
    if let error = error {
        print(error)
    }
    else {
        print("Success")
    }
}

And the client has the responsibility of saving the device details because, currently, the local device is always assuming APNS

On Android

There's a customer there who is asking if they can do push device registration using fcm (ie not apns)

it ought to be possible I think if they do push.deviceRegistrations.save(localDevice) if they set the recipientdetails -transportType=fcmandregistrationToken=xx`

┆Issue is synchronized with this Jira Task by Unito

ricardopereira commented 5 years ago

I would like to test this by myself and see if it really works and then write a conclusion about it.

tomczoink commented 5 years ago

iOS: https://support.ably.io/solution/articles/3000089745

Android register with FCM: https://support.ably.io/solution/articles/3000089746

Just seen @ricardopereira 's comment, I'll make these drafts for now, once we're certain they work we can publish.

ricardopereira commented 5 years ago

@tomczoink Sorry, those links doesn't open for me. Are they private?

tomczoink commented 5 years ago

@ricardopereira I've published them to make sure you can see them.

ricardopereira commented 5 years ago

I did some experiments and now I understand how Firebase FCM works.

First of all, the code I gave you won't work because, at least in iOS, all actions will use the local device. That local device is generated automatically and it's expecting an APNS DeviceToken. So, i.e., it will always use the local device when the user subscribe to a channel (the request requires device authentication).

Even so, from the project I built and what I read from the Firebase FCM documentation, this particular issue doesn't make sense to me. Why does the user wants to assign a FCM registrationToken to Ably Device? Does the client want to use Firebase FCM and Ably Push at the same time? If that's the case then there's no problem while working with the two of them at the same time. I tested it by myself. So you can receive messages with notifications using Firebase and you can receive Ably channel notifications.

Here is a basic and possible setup that I did with Ably and Firebase FCM working together.

IMG_4522

Complete AblyPushFCM.project.

ricardopereira commented 5 years ago

Please have in mind, in iOS, if you want to send a notification to a specific device, then you need the APNS DeviceToken. In Android you use the FCM RegistrationToken. You can have a RegistrationToken using Firebase FCM iOS SDK and that FCM.RegistrationToken is like our Ably.LocalDevice.id. The LocalDevice.id is always related with the APNS DeviceToken and so do the Firebase. Ably doesn't need to know the iOS FCM RegistrationToken because Ably as already all the needed data to send push notifications to the related device.