Skip to content
This repository has been archived by the owner on Sep 4, 2020. It is now read-only.

Custom notification sound in background mode? #17

Closed
quedicesebas opened this issue Jun 18, 2015 · 23 comments
Closed

Custom notification sound in background mode? #17

quedicesebas opened this issue Jun 18, 2015 · 23 comments
Assignees
Labels
Milestone

Comments

@quedicesebas
Copy link

Why I can't the app play a custom sound with the notification when is in background? I'm sending the sound name in the notification...I neither can make this to work in the previous version of the plugin.

I have beep.wav in my www folder. For Android I'm sending 'soundname': 'beep.wav' in the payload, and 'sound': 'beep.wav' for iOS

My code:

App:

var push = PushNotification.init({
    "android": {
        "senderID": "..."//I put here my GCM senrder id
    },
    "ios": {}
});

push.on('registration', function(data) {
    // data.registrationId
    registerNotificationToken(data.registrationId, (window.device.platform == 'android' || device.platform == 'Android') ? 'Android' : 'iOS');
});

push.on('notification', function(data) {
    // data.message,
    // data.title,
    // data.count,
    // data.sound,
    // data.additionalData
    if (isLogedIn()) {

        if (window.device.platform == 'android' || device.platform == 'Android') {

            // if this flag is set, this notification happened while we were in the foreground.
            // you might want to play a sound to get the user's attention, throw up a dialog, etc.
            if ( data.additionalData.foreground )
            {
                var my_media = new Media("/android_asset/www/" + data.sound);
                my_media.play();

                alertify.confirm('<p><strong>' + data.title + '</strong><p/><p>' + data.message + '</p><p>¿Desea verlo?</p>', function () {

                    location.hash = 'available-services\\' + data.additionalData.id;
                });
            }
            // otherwise we were launched because the user touched a notification in the notification tray.
            else {   
                //If app wasn't runing
                if ( e.coldstart ) {

                    serviceId = data.additionalData.id;
                }
                else {

                    location.hash = 'available-services\\' + data.additionalData.id;
                }
            }
        }
        else {

            if ( data.additionalData.alert ) {

                //do something with the push message. This function is fired when push message is received or if user clicks on the tile.
                if (data.additionalData.foreground == 1) {

                    alertify.confirm('<p><strong>' + data.title+ '</strong><p/><p>' + data.message + '</p><p>¿Desea verlo?</p>', function () {

                        location.hash = 'available-services\\' + data.additionalData.id;
                    });
                }
                else {

                    location.hash = 'available-services\\' + data.additionalData.id;
                }
            }

            if ( data.sound ) {

                //play notification sound. Ignore when app is in foreground.
                var snd = new Media(data.sound);
                snd.play();
            }
        }
    }
@macdonst macdonst added the bug label Jun 19, 2015
@quedicesebas
Copy link
Author

Anyone had the opportunity to test it? It's a bug or it's possible I'm doing something wrong?

@macdonst macdonst added this to the Release 1.1.0 milestone Jun 28, 2015
@macdonst
Copy link
Member

macdonst commented Jul 1, 2015

@sebrojas14 root cause of the issue is sounds in the assets folder are private to the app and not accessible from the notification service.

@macdonst
Copy link
Member

macdonst commented Jul 1, 2015

@sebrojas14 So, I've been working on this a bit tonight. The sound file will need to get copied from the asset/www folder to the res/raw folder on Android in order to work. I will look at creating a hook for this soon and test on iOS as well.

@quedicesebas
Copy link
Author

I'm glad to know it, please let me know if you need anything, test on Andorid for example

@macdonst
Copy link
Member

macdonst commented Jul 2, 2015

@sebrojas14 if you pull the latest from this github repo then you can test. You have to put the sound file in the platforms/android/res/raw directory and when you send the push just use it's name, omit the extension.

@quedicesebas
Copy link
Author

I tried it but doesn't work. I put the beep.wav file in platforms/android/res/raw, and if I look into the apk file is in it. In the payload I sent 'soundname': 'beep'. Missing something?

@macdonst
Copy link
Member

macdonst commented Jul 6, 2015

@sebrojas14 sorry, did you pull from the issue17 branch?

@quedicesebas
Copy link
Author

Nooo, my bad. This afternoon I'm gonna try again, I need it so bad. What about iOS?

@macdonst
Copy link
Member

macdonst commented Jul 6, 2015

@sebrojas14 No, my bad. I forgot to mention the branch. I took a look at iOS and it should work but I need to double check where the sound should be packaged on iOS.

@quedicesebas
Copy link
Author

Ok, let me know

@quedicesebas
Copy link
Author

It works on Android (OnePLus One, OxygenOS, Android 5.0.2, using a .wav audio and a .mp3)!! But the sound starts and starts again like half a second later (so is cropped and then it plays all). And in foreground is worst, starts over and over again

@quedicesebas
Copy link
Author

I'm gonna used in an app besides that. Please let me know when you know something about iOS

@macdonst
Copy link
Member

macdonst commented Jul 8, 2015

@sebrojas14 I'm testing on a Nexus 5 and Nexus 6 running Android 5.1.1 and can't reproduce.

@quedicesebas
Copy link
Author

What about iOS?

@macdonst
Copy link
Member

@sebrojas14 swinging back around to iOS tomorrow :)

@macdonst macdonst self-assigned this Jul 24, 2015
@macdonst
Copy link
Member

@sebrojas14 I got a chance to test this and it works. If I do the following to generate a proper sound:

afconvert /System/Library/Sounds/Submarine.aiff ~/Desktop/sub.caf -d ima4 -f caff -v

and put the resulting sub.caf in the root of my Xcode project then send the following push:

{
    "aps": {
        "alert": "test sub", 
        "sound": "sub.caf"
    }
}

I hear the correct sound.

See this link for more info: https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/IPhoneOSClientImp.html#//apple_ref/doc/uid/TP40008194-CH103-SW6

@macdonst
Copy link
Member

Merged the fix

@alexislg2
Copy link
Contributor

What do you mean "put the resulting sub.caf in the root of my Xcode project". In the directory /platforms/ios/ ? or /platforms/ios/project_name/ ? Or inside the xcode project tree?

Documentation is not so clear about it, neither about if we have to pass sound.mp3 or sound as a payload. I'll make a PR for the doc if I got a chance to make it work!

@macdonst
Copy link
Member

@alexislg2 It would be /platforms/ios. If you see the above comment you are supposed to supply an extension. Also, .mp3 may not work. Check the https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/IPhoneOSClientImp.html#//apple_ref/doc/uid/TP40008194-CH103-SW6 link for information on what file types iOS supports.

@alexislg2
Copy link
Contributor

Thanks. In fact what I didn't get was that I have to drag and drop the file
inside the Xcode project
It's working now

On Thu, Sep 10, 2015 at 5:16 PM, Simon MacDonald notifications@github.com
wrote:

@alexislg2 https://github.com/alexislg2 It would be /platforms/ios. If
you see the above comment you are supposed to supply an extension. Also,
.mp3 may not work. Check the
https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/IPhoneOSClientImp.html#//apple_ref/doc/uid/TP40008194-CH103-SW6
link for information on what file types iOS supports.


Reply to this email directly or view it on GitHub
#17 (comment)
.

@dbadb
Copy link

dbadb commented Sep 19, 2016

Here we are in Sep 2016 and this issue raises its ugly head again :-)

I have working files residing here: "www/snd/beep.caf". I'm referencing these files from the APN server with the same relative path in the notification payload. These work on IOS 9 but not on IOS 10.0.1 (from the same build!). Either its a new behavior triggered by compat layers in IOS 10 or its a bug in IOS 10. Here's a thread: http://stackoverflow.com/questions/39400703/ios10-custom-push-notification-sound-not-playing. My impression is that it's not as straightforward as the answer suggests. That is, the claim there is that it's an xcode 8 bug.

I'm not confident that the relative path references should be expected to work or fail (though they work on iOS 9) and welcome any thoughts/comments on this matter.,

@abdohossam5
Copy link

same issue here with ios 10

Using version 1.8.2 of the plugin and testing on iphone 6s. sound was working fine on ios 9 and even on ios 10 when running the app using cordova run ios. The issue only occurs when running with xcode not sure about the store version yet.

@lock
Copy link

lock bot commented Jun 4, 2018

This thread has been automatically locked.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 4, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

No branches or pull requests

5 participants