简体   繁体   English

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

[英]Custom sound using ionic push notifications

I am trying to implement custom sound for my push notifications in Ionic application.我正在尝试为 Ionic 应用程序中的推送通知实现自定义声音。 I copied the sound file to www/ also set plugin options as follows我将声音文件复制到 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);

I send the push notification from a node.js server我从 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);
        });

I want to play a custom audio that is stored in www/ .我想播放存储在www/的自定义音频。

With Cordova CLI 7 you can use resource-tag to copy the sounds to the projects http://cordova.apache.org/docs/en/7.x/config_ref/index.html#resource-file使用 Cordova CLI 7,您可以使用资源标签将声音复制到项目http://cordova.apache.org/docs/en/7.x/config_ref/index.html#resource-file

For Android:对于安卓:

<resource-file src="sound.mp3" target="res/wav/sound.mp3" />

for iOS:对于 iOS:

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

Old answer:旧答案:

To play a custom sound, the sound file name has to be passed from the server on the push notification data要播放自定义声音,必须在推送通知数据上从服务器传递声音文件名

On iOS the sound file has to be on the app project, not on www在 iOS 上,声音文件必须在应用程序项目上,而不是在 www

On android the sound file has to be on the res/raw folder, not on 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 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM