Does anyone know how to receive push-notifications on android devices using Firebase Cloud Messaging? I am using FirebaseMessaging ^8.0.0-dev.14 and am able to receive the notifications on IOS. Here is my main method and the methods for the notifications:
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
// If you're going to use other Firebase services in the background, such as Firestore,
// make sure you call `initializeApp` before using other Firebase services.
await Firebase.initializeApp();
print("Handling a background message: ${message.messageId}");
}
Future<void> channelCreate() async {
const AndroidNotificationChannel channel = AndroidNotificationChannel(
'high_importance_channel', // id
'High Importance Notifications', // title
'This channel is used for important notifications.', // description
importance: Importance.max,
);
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
RemoteNotification notification = message.notification;
AndroidNotification android = message.notification?.android;
// If `onMessage` is triggered with a notification, construct our own
// local notification to show to users using the created channel.
if (notification != null && android != null) {
flutterLocalNotificationsPlugin.show(
notification.hashCode,
notification.title,
notification.body,
NotificationDetails(
android: AndroidNotificationDetails(
channel.id,
channel.name,
channel.description,
icon: android?.smallIcon,
// other properties...
),
));
}
});
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
channelCreate();
runApp(DFGAPP());
}
Here is the other file where I request permissions for the notifications:
class _MentorMainPageState extends State<MentorMainPage> {
@override
void initState() {
super.initState();
final fbm = FirebaseMessaging.instance;
fbm.requestPermission();
}
}
question from:
https://stackoverflow.com/questions/65890061/flutter-firebase-cloud-messaging-push-notifications 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…