'flutter: on background message handler is not being called
I have implemented firebase cloud messaging and firebase functions to my app and used flutter local notifications and added a showNotifications function to make a channel ID and change its importance and priority so i get notifications as heads up (banner), but for some reason the onbackgroundmessagehandler callback is not being called when i send a message from firestore,
here's my code:
Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) async {
print('background message handler');
return await _TabsScreenState()._showNotification(message);
}
class TabsScreen extends StatefulWidget {
static const String id = '/tabs screen';
@override
_TabsScreenState createState() => _TabsScreenState();
}
class _TabsScreenState extends State<TabsScreen> {
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
FirebaseMessaging _fcm = FirebaseMessaging();
Firestore _fireStore = Firestore.instance;
void _onSaved() async {
String myToken = await _fcm.getToken();
if (myToken != null) {
var tokenRef = _fireStore.collection('tokens').document(myToken);
print(myToken);
await tokenRef.setData({
'token': myToken,
'createdAt': DateTime.now(),
'platform': Platform.operatingSystem,
});
}
}
Future _showNotification(Map<String, dynamic> message) async {
print('show notifications');
var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
'channel id',
'channel name',
'channel desc',
importance: Importance.Max,
priority: Priority.High,
);
var platformChannelSpecifics =
new NotificationDetails(androidPlatformChannelSpecifics, null);
await flutterLocalNotificationsPlugin.show(
0,
message['notifications']['title'],
'hi',
platformChannelSpecifics,
payload: 'default sound',
);
}
Future selectNotification(String payload) async {
await flutterLocalNotificationsPlugin.cancelAll();
}
void initState() {
var initializationSettingsAndroid =
AndroidInitializationSettings('@mipmap/ic_launcher');
var initializationSettings =
InitializationSettings(initializationSettingsAndroid, null);
flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: selectNotification);
_onSaved();
_fcm.configure(
onBackgroundMessage: myBackgroundMessageHandler,
onMessage: (Map<String, dynamic> message) async {
print("On Message: $message");
print(message['data']['message']);
showDialog(
context: context,
builder: (ctx) {
return AlertDialog(
content: ListTile(
title: Text(message['data']['message']),
),
actions: <Widget>[
FlatButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text('Ok'),
)
],
);
},
);
},
onResume: (Map<String, dynamic> resume) async {
print("on resume: $resume");
},
onLaunch: (Map<String, dynamic> launch) async {
print("on launch: $launch");
});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("My App"),
);
}
}
here is my andoird manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tamataapp">
<!-- io.flutter.app.FlutterApplication is an android.app.Application that
calls FlutterMain.startInitialization(this); in its onCreate method.
In most cases you can leave this as-is, but you if you want to provide
additional functionality it is fine to subclass or reimplement
FlutterApplication and put your custom class here. "io.flutter.app.FlutterApplication" -->
<application
android:name=".Application"
android:label="tamataapp"
android:icon="@mipmap/ic_launcher">
<activity
android:name=".MainActivity"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="default_notification_channel_id”>Channel ID"/>
<!-- Specifies an Android theme to apply to this Activity as soon as
the Android process has started. This theme is visible to the user
while the Flutter UI initializes. After that, this theme continues
to determine the Window background behind the Flutter UI. -->
<meta-data
android:name="io.flutter.embedding.android.NormalTheme"
android:resource="@style/NormalTheme"
/>
<!-- Displays an Android View that continues showing the launch screen
Drawable until Flutter paints its first frame, then this splash
screen fades out. A splash screen is useful to avoid any visual
gap between the end of Android's launch screen and the painting of
Flutter's first frame. -->
<meta-data
android:name="io.flutter.embedding.android.SplashScreenDrawable"
android:resource="@drawable/launch_background"
/>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
<action android:name="FLUTTER_NOTIFICATION_CLICK" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
android:name="flutterEmbedding"
android:value="2" />
<service
android:name=".java.MyFirebaseMessagingService"
android:exported="false">
</service>
</application>
</manifest>
and heres my index.js code:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
var newData;
exports.messageTrigger = functions.firestore.document('messages/{messagesId}').onCreate(async (snapshot, context) => {
if (snapshot.empty) {
console.log('No Devices');
return;
}
newData = snapshot.data();
const deviceIdTokens = await admin
.firestore()
.collection('tokens')
.get();
var tokenm = [];
for (var one of deviceIdTokens.docs) {
tokenm.push(one.data().token);
}
const payload = {
data: {
click_action: 'FLUTTER_NOTIFICATION_CLICK',
message: newData.message,
}
};
try {
const response = await admin.messaging().sendToDevice(tokenm, payload);
console.log('Notification sent successfully');
} catch (err) {
console.log(err);
}
});
Solution 1:[1]
Based on my experience, onBackgroundMessage
not called because one "or all" of these points:
1- onBackgroundMessage
does not work if you send a notification
in JSON, so delete notification
from your JSON to make onBackgroundMessage work.
If notifications
set in your JSON, you will see default notification in mobile as a heads up (banner), and onBackgroundMessage
will not run.
Update from 2022: on last version of FCM, this problem solved, so you can keep
notifications
in your JSON...
2- In many cases onBackgroundMessage
not called in debugging mode, try test it on release, first run flutter clean
then run flutter run --release
3- In many cases you need to set notification priority
to high
in JSON when send it from server.
Solution 2:[2]
What version of firebase_messaging are you using ?
From pub page https://pub.dev/packages/flutter_local_notifications:
Compatibility with firebase_messaging
Previously, there were issues that prevented this plugin working properly with the firebase_messaging plugin. This meant that callbacks from each plugin might not be invoked. Version 6.0.13 of firebase_messaging should resolve this issue so please bump your firebase_messaging dependency and follow the steps covered in firebase_messaging's readme file.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Danaru |