使用离子推送通知的自定义声音
问题描述:
我试图在离子应用程序中为我的推送通知实现自定义声音。 我的声音文件复制到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/
。
答
随着科尔多瓦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”},但它会播放默认声音。你知道什么是错误吗? – 27leaves
您必须将其放入xcode项目中,打开.xcodeproj并将声音拖入其中或使用挂钩将其复制,或使用资源文件标签复制它的自定义插件 – jcesarmobile
没有res /在离子建立平台文件夹内原始。你是否可能指的是实际安装android文件夹的res/raw? –