Skip to content

Commit 02ac81e

Browse files
committed
Addressed suggestions.
1 parent 37504fe commit 02ac81e

1 file changed

Lines changed: 35 additions & 37 deletions

File tree

  • src/pages/docs/push/getting-started

src/pages/docs/push/getting-started/fcm.mdx

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ You'll learn how to set up your application with Firebase Cloud Messaging (FCM),
1212

1313
1. [Sign up](https://ably.com/signup) for an Ably account.
1414
2. Create a [new app](https://ably.com/accounts/any/apps/new), and create your first API key in the **API Keys** tab of the dashboard.
15-
3. Your API key will need the `publish` and `subscribe` capabilities. For sending push notifications from your app, you'll also need the `push-admin` capability.
16-
4. For channel-based push, add a rule for the channel with **Push notifications enabled** checked. In the dashboard left sidebar: **Configuration****Rules****Add** or **Edit** a rule, then enable the Push notifications option. See [channel rules](https://ably.com/docs/channels#rules) for details.
15+
3. Your API key needs the `publish` and `subscribe` capabilities. For sending push notifications from your app, you'll also need the `push-admin` capability.
16+
4. For channel-based push, add a rule for the channel with **Push notifications enabled** checked. In the dashboard left sidebar: **Configuration****Rules****Add** or **Edit** a rule, then enable the Push notifications option. See [channel rules](/docs/channels#rules) for details.
1717
5. Install [Android Studio](https://developer.android.com/studio).
18-
6. A real Android device or an emulator with Google Play Services installed (required for FCM).
18+
6. Use a real Android device or an emulator with Google Play Services installed (required for FCM).
1919

2020
### (Optional) Install Ably CLI <a id="install-cli"/>
2121

@@ -116,7 +116,7 @@ object AblyHelper {
116116
return instance ?: synchronized(this) {
117117
instance ?: run {
118118
val options = ClientOptions().apply {
119-
key = "{{API_KEY}}"
119+
key = "{{API_KEY}}" // Use token authentication in production
120120
clientId = "push-tutorial-client"
121121
}
122122
AblyRealtime(options).also {
@@ -241,7 +241,7 @@ Register the service in `AndroidManifest.xml` inside the `<application>` element
241241
```
242242
</Code>
243243

244-
Now add push activation and deactivation to your `MainActivity.kt`. The Ably Android SDK sends activation results as local broadcasts, so register a `BroadcastReceiver` to handle them:
244+
Now add push activation and deactivation to your `MainActivity.kt`. The Ably Android SDK activates push asynchronously and the result arrives via Android's broadcast system, so register a `BroadcastReceiver` to handle it (the following imports cover all steps in this guide):
245245

246246
<Code>
247247
```kotlin
@@ -250,12 +250,17 @@ import android.content.Context
250250
import android.content.Intent
251251
import android.content.IntentFilter
252252
import android.os.Build
253+
import android.os.Bundle
253254
import android.util.Log
255+
import android.widget.Button
256+
import android.widget.ScrollView
257+
import android.widget.TextView
254258
import androidx.appcompat.app.AppCompatActivity
255259
import androidx.localbroadcastmanager.content.LocalBroadcastManager
256260
import io.ably.lib.types.AblyException
257261
import io.ably.lib.types.ErrorInfo
258262
import io.ably.lib.realtime.CompletionListener
263+
import io.ably.lib.util.IntentUtils
259264
import com.google.gson.JsonObject
260265
import io.ably.lib.types.Message
261266
import io.ably.lib.types.MessageExtras
@@ -274,28 +279,23 @@ class MainActivity : AppCompatActivity() {
274279
override fun onReceive(context: Context, intent: Intent) {
275280
when (intent.action) {
276281
"io.ably.broadcast.PUSH_ACTIVATE" -> {
277-
val error = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
278-
intent.getParcelableExtra("error", ErrorInfo::class.java)
279-
} else {
280-
@Suppress("DEPRECATION")
281-
intent.getParcelableExtra("error")
282-
}
282+
val error = IntentUtils.getErrorInfo(intent)
283283
if (error != null) {
284284
Log.e(TAG, "Push activation failed: ${error.message}")
285285
updateStatus("Push activation failed: ${error.message}")
286286
} else {
287-
val deviceId = realtime.device().id
288-
Log.d(TAG, "Push activated. Device ID: $deviceId")
289-
updateStatus("Push activated. Device ID: $deviceId")
287+
try {
288+
val deviceId = realtime.device().id
289+
Log.d(TAG, "Push activated. Device ID: $deviceId")
290+
updateStatus("Push activated. Device ID: $deviceId")
291+
} catch (e: Exception) {
292+
Log.e(TAG, "Push activated but failed to get device ID: ${e.message}")
293+
updateStatus("Push activated")
294+
}
290295
}
291296
}
292297
"io.ably.broadcast.PUSH_DEACTIVATE" -> {
293-
val error = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
294-
intent.getParcelableExtra("error", ErrorInfo::class.java)
295-
} else {
296-
@Suppress("DEPRECATION")
297-
intent.getParcelableExtra("error")
298-
}
298+
val error = IntentUtils.getErrorInfo(intent)
299299
if (error != null) {
300300
Log.e(TAG, "Push deactivation failed: ${error.message}")
301301
} else {
@@ -335,9 +335,9 @@ class MainActivity : AppCompatActivity() {
335335
```
336336
</Code>
337337

338-
Now you are ready to receive push notifications.
338+
Your app is now configured to receive push notifications once activated.
339339

340-
## Step 3: Receive push notifications <a id="step-3"/>
340+
## Step 3: Subscribe and test push notifications <a id="step-3"/>
341341

342342
Push notifications delivered while your app is in the background are handled automatically by the FCM SDK and displayed as system notifications. For foreground handling, your `PushNotificationService.onMessageReceived()` method (from Step 2) displays a notification via `NotificationManager`.
343343

@@ -384,18 +384,17 @@ Also add a realtime channel subscription to receive messages while the app is in
384384
fun subscribeToRealtime(channelName: String) {
385385
val channel = realtime.channels.get(channelName)
386386
channel.subscribe { message ->
387-
runOnUiThread {
388-
appendToLog("Received message: ${message.name} - ${message.data}")
389-
}
387+
Log.d(TAG, "Received message: ${message.name} - ${message.data}")
390388
}
391389
}
392390
```
393391
</Code>
394392

395-
Sending push notifications using `deviceId` or `clientId` requires the `push-admin` capability for your API key. Use this method for testing purposes.
396-
In a production environment, you would typically send push notifications from your backend server (by posting messages with `push` `extras` field to a channel).
393+
<Aside>
394+
Sending push notifications using `deviceId` or `clientId` requires the `push-admin` capability for your API key. Use this method for testing purposes. In a production environment, you would typically send push notifications from your backend server (by posting messages with `push` `extras` field to a channel).
395+
</Aside>
397396

398-
To test push notifications, use the Ably CLI to send a push to your client ID:
397+
Now use the Ably CLI to test sending a push notification to your client ID:
399398

400399
<Code>
401400
```shell
@@ -416,7 +415,7 @@ ably push publish --device-id <your-device-id> \
416415
```
417416
</Code>
418417

419-
For sending pushes via a channel, we need some actual UI to subscribe to this channel. So, let's build one.
418+
To send push notifications via a channel, you first need a UI to subscribe to the channel.
420419

421420
## Step 4: Build the UI <a id="step-4"/>
422421

@@ -526,16 +525,15 @@ class MainActivity : AppCompatActivity() {
526525
override fun onReceive(context: Context, intent: Intent) {
527526
when (intent.action) {
528527
"io.ably.broadcast.PUSH_ACTIVATE" -> {
529-
val error = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
530-
intent.getParcelableExtra("error", ErrorInfo::class.java)
531-
} else {
532-
@Suppress("DEPRECATION")
533-
intent.getParcelableExtra("error")
534-
}
528+
val error = IntentUtils.getErrorInfo(intent)
535529
if (error != null) {
536530
updateStatus("Push activation failed: ${error.message}")
537531
} else {
538-
updateStatus("Push activated. Device ID: ${realtime.device().id}")
532+
try {
533+
updateStatus("Push activated. Device ID: ${realtime.device().id}")
534+
} catch (e: Exception) {
535+
updateStatus("Push activated")
536+
}
539537
}
540538
}
541539
"io.ably.broadcast.PUSH_DEACTIVATE" -> {
@@ -657,7 +655,7 @@ Build and run your app on an Android device or emulator. Tap **Activate Push** a
657655

658656
### Send push via channel <a id="step-4-send-channel"/>
659657

660-
To test pushes via channel, tap **Subscribe to Channel** in the app and then publish a message to "exampleChannel1" with a `push` `extras` field using Ably CLI:
658+
To test push notifications via channel, tap **Subscribe to Channel** in the app and then publish a message to "exampleChannel1" with a `push` `extras` field using Ably CLI:
661659

662660
<Code>
663661
```shell

0 commit comments

Comments
 (0)