Skip to content

Commit bcc6704

Browse files
maratalclaude
andcommitted
fix(fcm-guide): move PushNotificationService to Handle push notifications sub-step
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent a1c22c5 commit bcc6704

1 file changed

Lines changed: 86 additions & 86 deletions

File tree

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

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

Lines changed: 86 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -163,91 +163,7 @@ Register your `Application` class in `AndroidManifest.xml` inside the `<applicat
163163

164164
## Step 2: Set up push notifications <a id="step-2"/>
165165

166-
To receive push notifications on Android, you need to integrate with FCM. Create a `PushNotificationService.kt` file that extends `FirebaseMessagingService`. This service handles new FCM tokens and incoming push messages:
167-
168-
<Code>
169-
```kotlin
170-
import android.app.NotificationChannel
171-
import android.app.NotificationManager
172-
import android.os.Build
173-
import android.util.Log
174-
import androidx.core.app.NotificationCompat
175-
import com.google.firebase.messaging.FirebaseMessagingService
176-
import com.google.firebase.messaging.RemoteMessage
177-
import io.ably.lib.types.RegistrationToken
178-
179-
class PushNotificationService : FirebaseMessagingService() {
180-
181-
companion object {
182-
private const val TAG = "PushService"
183-
private const val CHANNEL_ID = "push_tutorial_channel"
184-
}
185-
186-
override fun onNewToken(token: String) {
187-
super.onNewToken(token)
188-
Log.d(TAG, "New FCM token received")
189-
try {
190-
// Update the FCM token on Ably server
191-
AblyHelper.getInstance().push.getActivationContext().onNewRegistrationToken(RegistrationToken.Type.FCM, token)
192-
} catch (e: Exception) {
193-
Log.e(TAG, "Error updating FCM token with Ably", e)
194-
}
195-
}
196-
197-
override fun onMessageReceived(remoteMessage: RemoteMessage) {
198-
super.onMessageReceived(remoteMessage)
199-
Log.d(TAG, "Message received from: ${remoteMessage.from}")
200-
201-
// Display a notification when the app is in the foreground
202-
remoteMessage.notification?.let { notification ->
203-
showNotification(
204-
notification.title ?: "Push Notification",
205-
notification.body ?: "",
206-
remoteMessage.data
207-
)
208-
}
209-
}
210-
211-
private fun showNotification(title: String, body: String, data: Map<String, String>) {
212-
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
213-
214-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
215-
val channel = NotificationChannel(
216-
CHANNEL_ID,
217-
"Push Tutorial",
218-
NotificationManager.IMPORTANCE_DEFAULT
219-
)
220-
notificationManager.createNotificationChannel(channel)
221-
}
222-
223-
val notification = NotificationCompat.Builder(this, CHANNEL_ID)
224-
.setSmallIcon(android.R.drawable.ic_dialog_info)
225-
.setContentTitle(title)
226-
.setContentText(body)
227-
.setAutoCancel(true)
228-
.build()
229-
230-
notificationManager.notify(System.currentTimeMillis().toInt(), notification)
231-
}
232-
}
233-
```
234-
</Code>
235-
236-
Register the service in `AndroidManifest.xml` inside the `<application>` element:
237-
238-
<Code>
239-
```xml
240-
<service
241-
android:name=".PushNotificationService"
242-
android:exported="false">
243-
<intent-filter>
244-
<action android:name="com.google.firebase.MESSAGING_EVENT" />
245-
</intent-filter>
246-
</service>
247-
```
248-
</Code>
249-
250-
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):
166+
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):
251167

252168
<Code>
253169
```kotlin
@@ -345,7 +261,91 @@ Your app is now configured to receive push notifications once activated.
345261

346262
### Handle push notifications <a id="step-2-handle"/>
347263

348-
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 above) displays a notification via `NotificationManager`.
264+
Create a `PushNotificationService.kt` file that extends `FirebaseMessagingService`. This service handles new FCM tokens and incoming push messages:
265+
266+
<Code>
267+
```kotlin
268+
import android.app.NotificationChannel
269+
import android.app.NotificationManager
270+
import android.os.Build
271+
import android.util.Log
272+
import androidx.core.app.NotificationCompat
273+
import com.google.firebase.messaging.FirebaseMessagingService
274+
import com.google.firebase.messaging.RemoteMessage
275+
import io.ably.lib.types.RegistrationToken
276+
277+
class PushNotificationService : FirebaseMessagingService() {
278+
279+
companion object {
280+
private const val TAG = "PushService"
281+
private const val CHANNEL_ID = "push_tutorial_channel"
282+
}
283+
284+
override fun onNewToken(token: String) {
285+
super.onNewToken(token)
286+
Log.d(TAG, "New FCM token received")
287+
try {
288+
// Update the FCM token on Ably server
289+
AblyHelper.getInstance().push.getActivationContext().onNewRegistrationToken(RegistrationToken.Type.FCM, token)
290+
} catch (e: Exception) {
291+
Log.e(TAG, "Error updating FCM token with Ably", e)
292+
}
293+
}
294+
295+
override fun onMessageReceived(remoteMessage: RemoteMessage) {
296+
super.onMessageReceived(remoteMessage)
297+
Log.d(TAG, "Message received from: ${remoteMessage.from}")
298+
299+
// Display a notification when the app is in the foreground
300+
remoteMessage.notification?.let { notification ->
301+
showNotification(
302+
notification.title ?: "Push Notification",
303+
notification.body ?: "",
304+
remoteMessage.data
305+
)
306+
}
307+
}
308+
309+
private fun showNotification(title: String, body: String, data: Map<String, String>) {
310+
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
311+
312+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
313+
val channel = NotificationChannel(
314+
CHANNEL_ID,
315+
"Push Tutorial",
316+
NotificationManager.IMPORTANCE_DEFAULT
317+
)
318+
notificationManager.createNotificationChannel(channel)
319+
}
320+
321+
val notification = NotificationCompat.Builder(this, CHANNEL_ID)
322+
.setSmallIcon(android.R.drawable.ic_dialog_info)
323+
.setContentTitle(title)
324+
.setContentText(body)
325+
.setAutoCancel(true)
326+
.build()
327+
328+
notificationManager.notify(System.currentTimeMillis().toInt(), notification)
329+
}
330+
}
331+
```
332+
</Code>
333+
334+
Register the service in `AndroidManifest.xml` inside the `<application>` element:
335+
336+
<Code>
337+
```xml
338+
<service
339+
android:name=".PushNotificationService"
340+
android:exported="false">
341+
<intent-filter>
342+
<action android:name="com.google.firebase.MESSAGING_EVENT" />
343+
</intent-filter>
344+
</service>
345+
```
346+
</Code>
347+
348+
Push notifications delivered while your app is in the background are handled automatically by the FCM SDK and displayed as system notifications. The `onMessageReceived()` method handles foreground notifications via `NotificationManager`.
349349

350350
## Step 3: Subscribe to channel push notifications <a id="step-3"/>
351351

0 commit comments

Comments
 (0)