使用离子推送通知自定义声音

时间:2016-02-12 00:25:53

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

我正在尝试为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 个答案:

答案 0 :(得分: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" />

for 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