Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
84 changes: 84 additions & 0 deletions packages/firebase_messaging/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,90 @@ Note: When you are debugging on Android, use a device or AVD with Google Play se
</intent-filter>
```

#### Optionally handle background messages

>Background message handling is intended to be performed quickly. Do not perform
long running tasks as they may not be allowed to finish by the Android system.
See [Background Execution Limits](https://developer.android.com/about/versions/oreo/background)
for more.
By default background messaging is not enabled. To handle messages in the background:

1. For Android add an Application.java class to your app

```
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;

public class Application extends FlutterApplication implements PluginRegistrantCallback {
@Override
public void onCreate() {
super.onCreate();
FlutterFirebaseMessagingService.setPluginRegistrant(this);
}

@Override
public void registerWith(PluginRegistry registry) {
GeneratedPluginRegistrant.registerWith(registry);
}
}
```
1. Set name property of application in `AndroidManifest.xml`
```
<application android:name=".Application" ...>
```
1. For iOS (Swift)
Copy link

Choose a reason for hiding this comment

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

where to add this code in ios? like android we create an Application class

```
func callback(registry: FlutterPluginRegistry) {
GeneratedPluginRegistrant.register(with: registry)
}

FLTFirebaseMessagingPlugin.setPluginRegistrantCallback(callback)
```

1. Define a top level Dart method to handle background messages
```
Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) {
if (message.containsKey('data')) {
// Handle data message
dynamic data = message['data'];
}

if (message.containsKey('notification')) {
// Handle notification message
dynamic notification = message['notification'];
}

// Or do other work.
}
```
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`
```
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print("onMessage: $message");
_showItemDialog(message);
},
onBackgroundMessage: myBackgroundMessageHandler,
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch: $message");
_navigateToItemDetail(message);
},
onResume: (Map<String, dynamic> message) async {
print("onResume: $message");
_navigateToItemDetail(message);
},
);
```
Note: `configure` should be called early in the lifecycle of your application
so that it can be ready to receive messages as early as possible. See the
example app for a demonstration.

### iOS Integration

Expand Down
10 changes: 8 additions & 2 deletions packages/firebase_messaging/example/ios/Runner/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@

#include "AppDelegate.h"
#include "GeneratedPluginRegistrant.h"
#import <firebase_messaging/FirebaseMessagingPlugin.h>

@implementation AppDelegate

void callback(NSObject<FlutterPluginRegistry>* registry) {
[GeneratedPluginRegistrant registerWithRegistry:registry];
}

- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[GeneratedPluginRegistrant registerWithRegistry:self];
return [super application:application didFinishLaunchingWithOptions:launchOptions];
[GeneratedPluginRegistrant registerWithRegistry:self];
[FLTFirebaseMessagingPlugin setPluginRegistrantCallback:callback];
return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ - (void)setupBackgroundHandling:(int64_t)handle {
NSLog(@"Finished background setup");
}

- (void) startBackgroundRunner {
- (void)startBackgroundRunner {
NSLog(@"Starting background runner");

int64_t handle = [self getCallbackHandle:backgroundMessageCallback];
Expand Down