我正试图在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 /中的自定义音频.
解决方法:
使用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