Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
317 views
in Technique[技术] by (71.8m points)

android - Call onMessage method when the app is in background in flutter

I'm new in flutter and dart. I'm trying to connect my app with FCM. When app is in foreground I create flutterLocalNotificationsPlugin and everything works fine, but I don't how to handle onMessage method when my app is in background. Have somebody any idea how I can resolve it?

FirebaseMessaging firebaseMessaging = new FirebaseMessaging();
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();

@override
void initState() {
 super.initState();

 var androidInitSettings = new AndroidInitializationSettings('mipmap/ic_launcher');
 var iosInitSettings = new IOSInitializationSettings();
 var initSettings = new InitializationSettings(androidInitSettings, iosInitSettings);
 flutterLocalNotificationsPlugin.initialize(initSettings, selectNotification: onSelectNotification);

 firebaseMessaging.configure(
   onLaunch: (Map<String, dynamic> msg) {
     print(" onLaunch called ${(msg)}");
   },
   onResume: (Map<String, dynamic> msg) {
     print(" onResume called ${(msg)}");
   },
   onMessage: (Map<String, dynamic> msg) {
     showNotification(msg);
     print(" onMessage called ${(msg)}");
   },
 );
 firebaseMessaging.requestNotificationPermissions(const IosNotificationSettings(sound: true, alert: true, badge: true));
 firebaseMessaging.onIosSettingsRegistered.listen((IosNotificationSettings setting) {
   print('IOS Setting Registed');
 });
 firebaseMessaging.getToken().then((token) {
   update(token);
 });
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I see you are forcibly showing a notification when the onMessage is triggered, you don't need to do that if the app is in background, the notifications will be automatically created.

The onMessage is triggered when you receive a notification and the app is open, running in foreground. For instance, you have the Gmail app open, and a new e-mail is received, in this case you don't need a notification poping in the notification area. The app might choose to handle it directly, and the onMessage is triggered as soon as the notification is received - which is good so you don't need to keep pooling the server.

The onResume and onLaunch are a bit different - these two events are not triggered when the notification is received. They are only triggered when the user selects/taps the notification from the notification area. So, in both cases, the app is currently hidden, either by not running at all (terminated), or the app is in background - not being shown. In this case, the notification is received in the phone and automatically placed in the notification area (you don't need to code a "showNotification" for that). At this state, the user can see the notification but the app itself it not yet aware of it.

The app will only become aware of the notification when the user selects one of these notifications.

If the app is not running at all, the onLaunch will be triggered when the user taps the notification. It means the app was not running and as a result of the notification it had to 'launch from scratch'.

If the app is in background, the onResume will be triggered when the user selects the notification, resuming the app to the foreground state.

EDIT:

As pointed out by @boformer this only applies for 'Notification' messages. If you're sending 'Data' messages, no notification is created and the messages are delivered only through onMessage. More details in the plugin readme and firebase docs.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...