4

I am trying to implement custom sound for my push notifications in Ionic application. I copied the sound file to www/ also set plugin options as follows

//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

  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/.

Nikhil
  • 1,166
  • 2
  • 17
  • 35

1 Answers1

6

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

For Android:

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

for 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

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

jcesarmobile
  • 51,328
  • 11
  • 132
  • 176
  • 1
    So on iOS I placed my file (`sound_danger.caf`) in `platforms/ios` and it doesn't work. The server sends `"ios": { "sound": "sound_danger.caf" }` but it plays the default sound instead. Do you know what's the mistake? – 27leaves Aug 23 '16 at 14:29
  • You have to put it inside the xcode project, open the .xcodeproj and drag the sound into it or use a hook to copy it, or a custom plugin that just uses a resource-file tag to copy it – jcesarmobile Aug 23 '16 at 16:17
  • there is no res/raw inside the platform folder in ionic build. Are you perhaps refering to the res/raw of actual installation android folder?? – Sunil Lama Oct 20 '16 at 09:34
  • 2
    But there is a res folder, right? You create the raw folder inside of the res folder – jcesarmobile Oct 20 '16 at 09:41
  • the raw folder is already on the installation path of android but not on the project file of ionic @jcesarmobile – Sunil Lama Oct 20 '16 at 09:53
  • I looked into the documentation. Thanks for your answer, i upvoted you just in case. We just had to create a folder under android/res as a raw and then put in the sound and build it. You had written the answer however, i misunderstood it. Great job thanks – Sunil Lama Oct 20 '16 at 10:04
  • 1
    @Sunil Lama For some reason, my sounds are not happening. Based on your answer above, I'm assuming the full path of the folder the sounds should go in is: platforms/android/res/raw. Is that true? Thanks! – Dicer Mar 29 '17 at 23:33
  • @Dicer, yes the sound should go there, and you are to only use the filename to have that sound playing :) – Sunil Lama Apr 07 '17 at 11:04
  • is custom sound will play for enterprise IOS app? **IOS** "aps": { "alert": "Test sound", "sound": "sub.caf" } – Pritish Apr 28 '17 at 13:49
  • yeah, it should work on enterprise apps to. But you have to add the sub.caf file to you iOS project – jcesarmobile Apr 28 '17 at 14:03