使用Ionic推送通知实现自定义声音

4
我正在尝试为我的Ionic应用程序实现自定义推送通知声音。我将声音文件复制到了www/目录下,并设置了插件选项如下:
//In app.run
$ionicPush.init({
      "debug": true,
      "onNotification": function(notification){
        $cordovaDialogs.alert(notification.message, 'Notification', 'OK').then(function(){
          console.log(notification);
        });
      }
      "onRegister": function(data) {
        console.info("New device registered with token "+data.token);
      }
      "pluginConfig": {
        "ios": {
          "badge": true,
          "sound": true
         },
         "android": {
           "iconColor": "#343434"
         }
      }
      });

//In my main controller - 
  $scope.saveUserDeviceReg = function(data){
    var ionicUser = Ionic.User.current();
    if(!ionicUser.id){
      ionicUser.id = $scope.user.userId;
    }
    ionicUser.set('name', $scope.user.name);
    ionicUser.set('image', $scope.user.profilePic);
    ionicUser.set('email', $scope.user.email);
    $ionicPush.addTokenToUser(ionicUser);
    ionicUser.save();
    if($scope.user.devices){
      $scope.user.devices[data.token] = true;
      $scope.user.$save().then(function(success){
        console.log("User device saved");
      },function(error){
        console.error("Error saving user device");
      });
    }
    else{
      var devices = {};
      devices[data.token] = true;
      $scope.user.devices = devices;
      $scope.user.$save().then(function(success){
        console.log("User device updated");
      },function(error){
        console.error("Error updating user device");
      });
    }
  };
​
  $ionicPush.register($scope.saveUserDeviceReg);

我从一个node.js服务器发送推送通知

  request({
            url: "https://push.ionic.io/api/v1/push",
            method: "POST",
            json: true,
            body: {
                "tokens":tokens,
                "notification": {
                    "alert": message.from + " : '" + message.text
                }
            },
            headers: {
                'Authorization': 'Basic ' + btoa(IONIC_PRIVATE_API_KEY + ":"),
                'X-Ionic-Application-Id': IONIC_APP_ID
            }
        }, function (error, response, body) {
            console.log(body);
        });

我想播放一个存储在www/目录下的自定义音频。
1个回答

6
使用 Cordova CLI 7,您可以使用 resource-tag 将声音复制到项目中。有关详细信息,请参见http://cordova.apache.org/docs/en/7.x/config_ref/index.html#resource-file
对于 Android:
<resource-file src="sound.mp3" target="res/wav/sound.mp3" />

iOS平台:

<resource-file src="sub.caf"/>

旧版答案:

要播放自定义声音,需要从服务器上推送通知数据时传递声音文件名。

iOS 上,声音文件必须存储在应用项目中,而不能存储在 www 目录下。

Android 上,声音文件必须存储在 res/raw 文件夹中,而不能存储在 www 目录下。

https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/PAYLOAD.md#sound https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/PAYLOAD.md#sound-1


1
在iOS上,我将我的文件(sound_danger.caf)放置在platforms/ios中,但它不起作用。服务器发送了 "ios": { "sound": "sound_danger.caf" } 但是播放的是默认声音。你知道错在哪里吗? - 27leaves
2
但是有一个res文件夹,对吧?你在res文件夹里创建raw文件夹。 - jcesarmobile
我查看了文档。谢谢您的回答,我已经给您点了赞。我们只需要在android/res下创建一个名为"raw"的文件夹,然后把声音放进去并进行构建即可。您已经写了答案,但我理解有误。干得好,谢谢。 - Sunil Lama
1
@Sunil Lama,我的声音出了些问题。根据您上面的回答,我猜测声音应该放在完整路径 platforms/android/res/raw 中,是这样吗?谢谢! - Dicer
@Dicer,是的,声音应该放在那里,你只需要使用文件名来播放那个声音 :) - Sunil Lama
显示剩余5条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接