Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

android - Flutter Push Notifications with FirebaseMessaging ^8.0.0-dev.14

I am trying to send push notifications to the users of my app using FirebaseMessaging ^8.0.0-dev.14 but I am having some trouble. I am able to receive the notifications on IOS but nothing shows up on my Android emulator. I have listeners set up on init but they only see the notification on IOS devices. Has anyone had a similar issue or know how to get the notifications to show on Android? Here is my code:

import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart'
    as locNots;
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:url_launcher/url_launcher.dart';
import '../widgets/mentorActionsButton.dart';
import '../widgets/list_of_events.dart';
import 'userPage.dart';
import 'loginPage.dart';

class MentorMainPage extends StatefulWidget {
  @override
  _MentorMainPageState createState() => _MentorMainPageState();
}

class _MentorMainPageState extends State<MentorMainPage> {
  @override
  void initState() {
    super.initState();
    final fbm = FirebaseMessaging.instance;

    // IOS Configurations
    fbm.setForegroundNotificationPresentationOptions(
        alert: true, badge: true, sound: true);
    fbm.requestPermission();
    FirebaseMessaging.onMessage.listen((RemoteMessage message) {
      print('IOS Listener');
      print('Got a message whilst in the foreground!');
      print('Message data: ${message.data}');

      if (message.notification != null) {
        print('Message also contained a notification: ${message.notification}');
      }
    });

    //Android Configurations
    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();
    flutterLocalNotificationsPlugin
        .resolvePlatformSpecificImplementation<
            AndroidFlutterLocalNotificationsPlugin>()
        ?.createNotificationChannel(channel);
    FirebaseMessaging.onMessage.listen((RemoteMessage message) {
      print('Android Listener');
      RemoteNotification notification = message.notification;
      AndroidNotification android = message.notification?.android;
      print('Android Notification:');
      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,
                  showWhen: false,
                  importance: Importance.max,
                  priority: locNots.Priority.high),
            ));
      }
    });
  }

  var userLoggedIn = true;
  @override
  Widget build(BuildContext context) {
    Code for screen view.....
}
question from:https://stackoverflow.com/questions/65911098/flutter-push-notifications-with-firebasemessaging-8-0-0-dev-14

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

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...