firebase 通知在IOS中不起作用且没有声音当收到通知但振动起作用时

de90aj5v  于 2023-03-24  发布在  iOS
关注(0)|答案(1)|浏览(160)

我已经创建了Flutter应用程序使用firebase的通知后,firebase为iOS和Android的集成。
通知仅适用于Android,但没有声音
当通知从Firebase控制台发送到Android时,它可以工作,但不适用于iOS,iOS物理设备中没有通知
问题是iOS设备,我无法弄清楚如何解决这个问题
请指导我该怎么做,如果我有共享任何文件,请让我知道我必须共享的文件

[✓] Flutter (Channel stable, 2.5.3, on macOS 12.1 21C52 darwin-arm, locale en-IN)
[✓] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1)
[✓] Xcode - develop for iOS and macOS
[✓] Chrome - develop for the web
[✓] Android Studio (version 2021.1)
[✓] Connected device (3 available)
   • No issues found!

main.dart

import....

const iOSLocalizedLabels = false;

/// Create a AndroidNotificationChannel for heads up notifications
AndroidNotificationChannel channel;

/// Initialize the FlutterLocalNotificationsPlugin package.
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;

//Device Information Plugin
DeviceInfoPlugin deviceInfo=DeviceInfoPlugin();

// Global Key for Navigating during Notifications
GlobalKey<NavigatorState> navigatorKey=GlobalKey<NavigatorState>();

Map phoneInformation={};

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.
  ///For supporting iOS 14+ phone with new updates
  await Firebase.initializeApp();
  print('Handling a background message ${message.messageId}');
}

Future<void> main() async
{
  WidgetsFlutterBinding.ensureInitialized();
  FlutterAppBadger.removeBadge();
  await Firebase.initializeApp();

  // Set the background messaging handler early on, as a named top-level function
  FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
  if (!kIsWeb)
  {
    channel = const AndroidNotificationChannel(
      'high_importance_channel', // id
      'High Importance Notifications', // title
      'This channel is used for important notifications.', // description
      importance: Importance.high,
    );
    flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
    /// Create an Android Notification Channel.
    /// We use this channel in the `AndroidManifest.xml` file to override the
    /// default FCM channel to enable heads up notifications.
    await flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()?.createNotificationChannel(channel);
    /// Update the iOS foreground notification presentation options to allow
    /// heads up notifications.
    await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
        alert: true,
        badge: true,
        sound: true,
    );
    ///Added new
  await FirebaseMessaging.instance.requestPermission(
      alert: true,
      announcement: false,
      badge: true,
      carPlay: false,
      criticalAlert: false,
      provisional: false,
      sound: true,
    );
    /// to check the authorization of the application
    print('User granted permission');
  }

  if(Platform.isIOS)
    {
      IosDeviceInfo iosDeviceInfo=await deviceInfo.iosInfo;
      Map<String,String> data=
      {
        "name":iosDeviceInfo.name,
        "isPhysicalDevice":iosDeviceInfo.isPhysicalDevice.toString(),
        "uniqueId":iosDeviceInfo.identifierForVendor
      };
      phoneInformation=data;
      print("----------- $data");
    }
  else
    {
      AndroidDeviceInfo androidDeviceInfo=await deviceInfo.androidInfo;
      Map<String,String> data=
      {
        "name":androidDeviceInfo.device,
        "isPhysicalDevice":androidDeviceInfo.isPhysicalDevice.toString(),
        "uniqueId":androidDeviceInfo.androidId,
      };
      phoneInformation=data;
      print("-----------$data");
    }
  runApp(MyApp());
}

class MyApp extends StatelessWidget{}

AppDelegate.swift

// import UIKit
// import Flutter
// import Firebase
// @UIApplicationMain
// @objc class AppDelegate: FlutterAppDelegate {
//   override func application(
//     _ application: UIApplication,
//     didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
//   ) -> Bool {
//     FirebaseApp.configure()
//     GeneratedPluginRegistrant.register(with: self)
//     //GeneratedPluginRegistrant.register(with: self)
//    if #available(iOS 10.0, *) {
//     UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
//    }
//     return super.application(application, didFinishLaunchingWithOptions: launchOptions)
//   }
// }


import UIKit
import Flutter
import FirebaseMessaging
import Firebase // Add this import
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate, MessagingDelegate  {


  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    FirebaseApp.configure()
    Messaging.messaging().delegate = self
    GeneratedPluginRegistrant.register(with: self)
    if #available(iOS 10.0, *) {
        // For iOS 10 display notification (sent via APNS)
        UNUserNotificationCenter.current().delegate = self
        let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
        UNUserNotificationCenter.current().requestAuthorization(
                options: authOptions,
                completionHandler: {_, _ in })
    } else {
        let settings: UIUserNotificationSettings =
        UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
        application.registerUserNotificationSettings(settings)
    }

  

    application.registerForRemoteNotifications()
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
    
    //added new comment
    override func application(_ application: UIApplication,
    didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {

       Messaging.messaging().apnsToken = deviceToken
       super.application(application, didRegisterForRemoteNotificationsWithDeviceToken: deviceToken)
     }
}

info.plist

<key>FirebaseAppDelegateProxyEnabled</key>
    <false/>
    <key>FirebaseScreenReportingEnabled</key>
    <false/>

在启动画面

Future<void> setToken(String token) async
  {
    print('FCM Token:--- $token');
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    sharedPreferences.setString("FCM_token", token);
  }
  askPermission() async
  {
  await FirebaseMessaging.instance.requestPermission(
    announcement: true,
    carPlay: true,
    criticalAlert: true,
    badge: true,
    sound: true,
  );


  @override
  void initState()
  {
    // TODO: implement initState
    super.initState();
    FlutterAppBadger.removeBadge();
    askPermission();
    Timer(Duration(microseconds: 1000), () {setState(()=>_Bigger = true);});
    startTime();
    FirebaseMessaging.onMessage.listen((RemoteMessage message)
    {
      RemoteNotification notification = message.notification;
      AndroidNotification android = message.notification?.android;
      if (notification != null && android != null && !kIsWeb)
      {
        flutterLocalNotificationsPlugin.show(
            notification.hashCode,
            notification.title,
            notification.body,
            NotificationDetails(
              android: AndroidNotificationDetails(
                channel.id,
                channel.name,
                channel.description,
                // TODO add a proper drawable resource to android, for now using
                //      one that already exists in example app.
                icon: '@mipmap/ic_launcher',
              ),
            ));

        FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message)
        {
          print('A new onMessageOpenedApp event was published!');
        });
      }
    });
  }

Home.dart

@override
  void initState()
  {
    // TODO: implement initState
    super.initState();
    FirebaseMessaging.onMessage.listen((RemoteMessage message)
    {
      if(message.notification.body.contains("requested")==true && widget!=AppNotificationVC())
        Navigator.pushNamed(navigatorKey.currentState.context,'/AppNotificationVC');

    });

    FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message)
    {
      print('Notification /Message open when click on it');
      if(message.notification.body.contains("requested")==true && widget!=AppNotificationVC())
        Navigator.pushNamed(navigatorKey.currentState.context,'/AppNotificationVC');
    });

    Future.delayed(Duration.zero, () async
    {
     ....
    });
  }

 openNotificationCashpotAPI(String cashpot_id) async {...}

 notificationBadgeAPI() async{...}

  openUsernotificationAPI() async{...}
c3frrgcw

c3frrgcw1#

重要的一点;当您手动发送通知时,也发送声音参数。下面是一个示例通知请求正文:

{
 "to" : "...",
 "notification" : {
     "priority": "high",
     "body" : "Example body",
     "title": "Example title",
     "sound": "default"
 },
 "data" : {
     ...
 }
}

相关问题