允许用户更改应用程序通知声音

Allow user to change app notification sound

我有一个使用 Firebase Messaging 发送通知的应用程序,并且我的 xcode 项目资源中添加了一组声音,以便在设备上播放。

我从我的服务器向订阅了特定主题的用户发送通知,如下所示:

"apns": {
    "headers": {
        "apns-priority": "10",
    },
    "payload": {
        "aps": {
            "alert": {
                "title": titleMessage,
                "body": bodyMessage,
            },
            "sound": 'alert.wav',
        },
    },
}

现在,当收到通知时,声音 "alert.wav" 在设备上可以正常播放。

我想做的是在我的应用程序中: 我想允许用户从不同的声音集更改通知声音。

示例: 播放声音的选项:设置 1 或设置 2。我会将它们作为具有相同文件名的单独文件夹放在我的应用程序中。

这在 iOS 中可行吗?以及如何在 React Native 中实现它?

我做到了,结果是这样的:

1.在我的应用程序资源 xcode 中添加了声音文件。

刚刚在包含我所有声音的文件夹中添加了一个 link,并且没有 link 每个文件,因为我希望该应用稍后播放来自以下位置的声音:“/Library/Sounds 目录”。

2。使用 rn-fetch-blob 复制声音并替换它们。

通过使用 rn-fetch-blob 我能够创建 /Library/Sounds 目录,然后将声音从MainBundleDir/Library/Sounds,应用程序将在收到通知时寻找声音。

我创建了一个界面允许用户替换这些声音。

//Function: setup sounds
setupSounds(){
    //sounds directory
    const dirs = RNFetchBlob.fs.dirs
    const soundsDir = dirs.CacheDir.replace('Caches','Sounds')

    //Check if has sounds directory
    RNFetchBlob.fs.isDir(soundsDir).then((isDir) => {
        //create directory
        if(!isDir) RNFetchBlob.fs.mkdir(soundsDir)
    })

    //Add the sound file to /Library/Sounds
    RNFetchBlob.fs.writeFile(soundsDir+'/default.wav', dirs.MainBundleDir + '/sounds/jingle.wav', 'uri');
}

现在,如果该应用收到带有 sound: 'default.wav' 的通知,该应用将播放 default.wav,这是 jingle.wav.

的副本