【问题标题】:Custom sound using ionic push notifications使用离子推送通知自定义声音
【发布时间】:2016-02-12 00:25:52
【问题描述】:

我正在尝试在 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/ 中的自定义音频。

【问题讨论】:

    标签: android ios cordova ionic-framework phonegap-pushplugin


    【解决方案1】:

    使用 Cordova CLI 7,您可以使用资源标签将声音复制到项目 http://cordova.apache.org/docs/en/7.x/config_ref/index.html#resource-file

    对于安卓:

    <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

    【讨论】:

    • 所以在 iOS 上,我将文件 (sound_danger.caf) 放在了 platforms/ios 中,但它不起作用。服务器发送"ios": { "sound": "sound_danger.caf" },但它播放的是默认声音。你知道错在哪里吗?
    • 你必须把它放到 xcode 项目中,打开 .xcodeproj 并将声音拖入其中或使用钩子复制它,或者只使用资源文件标签复制的自定义插件它
    • 离子构建的平台文件夹内没有 res/raw。你可能指的是实际安装的android文件夹的res/raw??
    • 但是有一个 res 文件夹,对吧?您在 res 文件夹中创建 raw 文件夹
    • @Sunil Lama 出于某种原因,我的声音没有发生。根据您上面的回答,我假设声音应该进入的文件夹的完整路径是:platforms/android/res/raw。真的吗?谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-24
    • 1970-01-01
    相关资源
    最近更新 更多