Skip to content

[firebase_messaging] Add Kotlin support in README.md. #3457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/firebase_messaging/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 7.0.1
* Add Kotlin support in README.md.

## 7.0.0

* Depend on `firebase_core` and migrate plugin to use `firebase_core` native SDK versioning features;
Expand Down
40 changes: 35 additions & 5 deletions packages/firebase_messaging/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ By default background messaging is not enabled. To handle messages in the backgr

Note: you can find out what the latest version of the plugin is [here ("Cloud Messaging")](https://firebase.google.com/support/release-notes/android#latest_sdk_versions).

1. Add an `Application.java` class to your app in the same directory as your `MainActivity.java`. This is typically found in `<app-name>/android/app/src/main/java/<app-organization-path>/`.
#### Java's support:
2. Add an `Application.java` class to your app in the same directory as your `MainActivity.java`. This is typically found in `<app-name>/android/app/src/main/java/<app-organization-path>/`.

```java
package io.flutter.plugins.firebasemessagingexample;
Expand All @@ -98,19 +99,48 @@ By default background messaging is not enabled. To handle messages in the backgr
}
```

1. In `Application.java`, make sure to change `package io.flutter.plugins.firebasemessagingexample;` to your package's identifier. Your package's identifier should be something like `com.domain.myapplication`.
3. In `Application.java`, make sure to change `package io.flutter.plugins.firebasemessagingexample;` to your package's identifier. Your package's identifier should be something like `com.domain.myapplication`.

```java
package com.domain.myapplication;
```
#### Kotlin's support:
2. Add an `Application.kt` class to your app in the same directory as your `MainActivity.kt`. This is typically found in `<app-name>/android/app/src/main/kotlin/<app-organization-path>/`.
```kotlin
package io.flutter.plugins.firebasemessagingexample

import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback
import io.flutter.plugins.GeneratedPluginRegistrant
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService

class Application : FlutterApplication(), PluginRegistrantCallback {

override fun onCreate() {
super.onCreate()
FlutterFirebaseMessagingService.setPluginRegistrant(this);
}

override fun registerWith(registry: PluginRegistry?) {
io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin.registerWith(registry?.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
}
}
```
3. In `Application.kt`, make sure to change `package io.flutter.plugins.firebasemessagingexample` to your package's identifier. Your package's identifier should be something like `com.domain.myapplication`.

```kotlin
package com.domain.myapplication;
```
---

1. Set name property of application in `AndroidManifest.xml`. This is typically found in `<app-name>/android/app/src/main/`.
4. Set name property of application in `AndroidManifest.xml`. This is typically found in `<app-name>/android/app/src/main/`.

```xml
<application android:name=".Application" ...>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to show from and to for people not familiar with editing AndroidManifest.xml files.

Suggested change
<application android:name=".Application" ...>
<!-- FROM -->
<application android:name="io.flutter.app.FlutterApplication" ... />
<!-- TO -->
<application android:name=".Application" ... />

```

1. Define a **TOP-LEVEL** or **STATIC** function to handle background messages
5. Define a **TOP-LEVEL** or **STATIC** function to handle background messages

```dart
Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) async {
Expand All @@ -131,7 +161,7 @@ By default background messaging is not enabled. To handle messages in the backgr
Note: the protocol of `data` and `notification` are in line with the
fields defined by a [RemoteMessage](https://firebase.google.com/docs/reference/android/com/google/firebase/messaging/RemoteMessage).

1. Set `onBackgroundMessage` handler when calling `configure`
6. Set `onBackgroundMessage` handler when calling `configure`

```dart
_firebaseMessaging.configure(
Expand Down