繁体   English   中英

当应用程序在 IOS 的后台时,如何在新的 firebase 消息(推送通知)上播放自定义铃声?

[英]How to play a custom ringtone on new firebase message(pushed notification) when app is in background for IOS?

应用程序在android上运行正常(通知被推送,设备开始响铃),但在IOS上,应用程序收到通知但再次打开应用程序后铃声开始...

我也设置了功能、后台模式(音频后台获取、远程通知和后台处理)和推送通知

我正在尝试在 onMessage 方法中播放自定义声音...


Firebase 监听器

void firebaseCloudMessagingListeners(BuildContext context) {
    final deliveryCubit = BlocProvider.of<DeliveryCubit>(context);
    final playerService = PlayerService();

    if (Platform.isIOS) getIOSPermission();

    _firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) async {
           debugPrint('FMC -> $message');
           if(appInBackground){
           playerService.playRingtone();
         }
       
      },
      onResume: (Map<String, dynamic> message) async {
        debugPrint('on resume $message');
      },
      onLaunch: (Map<String, dynamic> message) async {

        debugPrint('on launch $message');
      },
    );
  }

播放器服务


import 'package:assets_audio_player/assets_audio_player.dart';
import 'package:path_provider/path_provider.dart' as path_provider;

class PlayerService {
  static PlayerService _instance;
  final assetsAudioPlayer = AssetsAudioPlayer();

  factory PlayerService() => _instance ?? PlayerService._internal();

  PlayerService._internal(){
    _instance = this;
  }

  void playRingtone(){
    assetsAudioPlayer.open(
    Audio("assets/audios/iosRingtone.mp3"),
      showNotification: false,
  );
    assetsAudioPlayer.play();
  }

  void pauseRingtone(){
    assetsAudioPlayer.pause();
  }
}

我试图在 lifeCycleStateChange 上停止播放器...

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    switch(state){
      case AppLifecycleState.resumed:
        socketService.appInBackground = false;
        firebaseService.appInBackground = false;
        playerService.pauseRingtone();
        setState(() {
          appLifecycleState = AppLifecycleState.resumed;
        });
        print("LifecycleState resumed");
        break;
      case AppLifecycleState.inactive:
        print("LifecycleState inactive");
        socketService.appInBackground = true;
        firebaseService.appInBackground = true;
        setState(() {
          appLifecycleState = AppLifecycleState.inactive;
        });
        break;
      case AppLifecycleState.paused:
        socketService.appInBackground = true;
        firebaseService.appInBackground = true;
        print("LifecycleState paused");
        break;
      case AppLifecycleState.detached:
        print("LifecycleState detached");
        break;
    }
  }
{
   “aps” : {
      “badge” : 9
      “sound” : “bingbong.aiff”
   },
   “messageID” : “ABCDEFGHIJ”
}

这是推送通知有效负载中播放声音所必需的。 还要确保您已请求警报和声音以及推送通知的权限。 您的应用程序包中应该存在相同的文件名。 你不需要解析任何东西。

https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/generating_a_remote_notification

您可以为 firebase 推送通知设置自定义通知声音。

自定义声音远程推送通知 iOS 的副本不工作

Go 检查这个https://stackoverflow.com/a/54104535/13375401出来,它工作...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM