No sound in remote push notifications

Greetings,

in the past I did my best to get notification sounds for push notifications working but with no success.

On the client side I'm using WindowsAzure.Messaging (https://components.xamarin.com/gettingstarted/azure-messaging) to handle push notifications on iOS and Android. Everything works fine except the sound.

I'm sending notifications from PHP with the following code:

if ($plattform == "apple")
    {
        $alert = '{
        "aps":{
            "alert":{
                "title":"Neue Abholer vorhanden",
                "subtitle":"Es liegen ' . $_POST['anzahl_abholer'] . ' neue Abholer bereit.",
                "body":"Hier tippen um Vegvisir zu öffnen",
                "sound":"default"
            },
            "badge":'. $_POST['anzahl_abholer'] . '
        }
    }';
    $notification = new Notification($plattform, $alert);
    $hub->sendNotification($notification, $gcm_id);
    }
    else if ($plattform == "gcm")
    {
        $message = '{"data":{"message":"Es liegen ' . $_POST['anzahl_abholer'] . ' neue Abholer bereit!" }}';
        $notification = new Notification("gcm", $message);
        $hub->sendNotification($notification, $gcm_id);
    }

And locally on iOS:

FinishedLaunching:

        if (UIDevice.CurrentDevice.CheckSystemVersion (8, 0)) {
                        var pushSettings = UIUserNotificationSettings.GetSettingsForTypes(
                        UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound,
                        new NSSet());

                    UIApplication.SharedApplication.RegisterUserNotificationSettings (pushSettings);
                             UIApplication.SharedApplication.RegisterForRemoteNotifications ();
                         } else {
                        UIRemoteNotificationType notificationTypes = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound;
                    UIApplication.SharedApplication.RegisterForRemoteNotificationTypes (notificationTypes);
                         }

public override void ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo)
        {
            ProcessNotification(userInfo, false);
        }

        void ProcessNotification(NSDictionary options, bool fromFinishedLaunching)
        {
            // Check to see if the dictionary has the aps key.  This is the notification payload you would have sent
            if (null != options && options.ContainsKey(new NSString("aps")))
            {
                //Get the aps dictionary
                NSDictionary aps = options.ObjectForKey(new NSString("aps")) as NSDictionary;

                string alert = string.Empty;

                //Extract the alert text
                // NOTE: If you're using the simple alert by just specifying
                // "  aps:{alert:"alert msg here"}  ", this will work fine.
                // But if you're using a complex alert with Localization keys, etc.,
                // your "alert" object from the aps dictionary will be another NSDictionary.
                // Basically the JSON gets dumped right into a NSDictionary,
                // so keep that in mind.
                try
                {
                    if (aps.ContainsKey(new NSString("alert")))
                    alert = (aps[new NSString("alert")] as NSString).ToString();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }


                //If this came from the ReceivedRemoteNotification while the app was running,
                // we of course need to manually process things like the sound, badge, and alert.
                if (!fromFinishedLaunching)
                {
                    //Manually show an alert
                    if (!string.IsNullOrEmpty(alert))
                    {
                        UIAlertView avAlert = new UIAlertView("Notification", alert, null, "OK", null);
                        avAlert.Show();
                    }
                }
            }
         }

Best Answer

  • MarioHenkelMarioHenkel US ✭✭
    Accepted Answer

    I found the issue on my own (shame on me...). The bug was located in the PHP part which sends the notification.

    It should read:

    "aps":{
                            "alert":{
                                "title":"Auftrag angenommen",
                                "subtitle":"Auftrag mit der ID ' . $_POST['auftragsID'] . ' wurde vom Fahrer angenommen und wird nun bearbeitet.",
                                "body":"Hier tippen um Vegvisir zu öffnen"
                            },
                            "badge":'. $_POST['anzahl_abholer'] . ',
                            "sound":"default"
                        }
    

    instead of

        "aps":{
                    "alert":{
                        "title":"Neue Abholer vorhanden",
                        "subtitle":"Es liegen ' . $_POST['anzahl_abholer'] . ' neue Abholer bereit.",
                        "body":"Hier tippen um Vegvisir zu öffnen",
                        "sound":"default"
                    },
                    "badge":'. $_POST['anzahl_abholer'] . '
                }
    

    Take note of the location of the "sound" parameter. It shouldn't be part of the alert itsself but of the "aps". Case closed.

Answers

  • N_BauaN_Baua INMember ✭✭✭✭✭

    Hi @MarioHenkel,

    Never did tried, however I think in Xamarin, You can use the Beep with DependencyService and use on each platform.

    OR Play your own audio file.
    https://github.com/tkowalczyk/SimpleAudioForms/tree/master/SimpleAudioForms

    Hope it helps.

  • MarioHenkelMarioHenkel USMember ✭✭

    Thanks for your reply. Possible that your approaches will work but I would prefer to get it to work "out of the box" without any hacks

  • N_BauaN_Baua INMember ✭✭✭✭✭
    edited August 2017

    @MarioHenkel said:
    Thanks for your reply. Possible that your approaches will work but I would prefer to get it to work "out of the box" without any hacks

    Ok, use Beep (on iOS I guess they use SystemSound class) which is in Xamarin box, works good with DependencyService, supported on all devices(at least I know yeah :smile:).
    And BTW a line additional for playing a mid file wouldn't hurt (or may be just a little).

  • MarioHenkelMarioHenkel USMember ✭✭
    Accepted Answer

    I found the issue on my own (shame on me...). The bug was located in the PHP part which sends the notification.

    It should read:

    "aps":{
                            "alert":{
                                "title":"Auftrag angenommen",
                                "subtitle":"Auftrag mit der ID ' . $_POST['auftragsID'] . ' wurde vom Fahrer angenommen und wird nun bearbeitet.",
                                "body":"Hier tippen um Vegvisir zu öffnen"
                            },
                            "badge":'. $_POST['anzahl_abholer'] . ',
                            "sound":"default"
                        }
    

    instead of

        "aps":{
                    "alert":{
                        "title":"Neue Abholer vorhanden",
                        "subtitle":"Es liegen ' . $_POST['anzahl_abholer'] . ' neue Abholer bereit.",
                        "body":"Hier tippen um Vegvisir zu öffnen",
                        "sound":"default"
                    },
                    "badge":'. $_POST['anzahl_abholer'] . '
                }
    

    Take note of the location of the "sound" parameter. It shouldn't be part of the alert itsself but of the "aps". Case closed.

  • tomaszBDtomaszBD Member ✭✭
    edited March 24

    I had a nightmare with iOS12 and sounds for a long time, so I thought I'll drop in a line in case someone might be running into similar problem. I have followed all the magical tutorials and all, checked the Apple docs and still did not get any sounds when notification was delivered to the device - using Azure Notification Hub.

    I wasted days looking at forums, examples, samples, and the sorts, pulling my hear out a bit, reviewing the code of the app and the likes, hopefully this will save someone a few grey hairs...

    What I found is that if my payload was formatted in a human readable form on the Test Send APNS interface, the message wouldn't arrive or would arrive, with no sound (no matter what iOS version), badge wouldn't change the value!

        {
            "aps": {
                "alert": {
                    "title": "title",
                    "subtitle": "sub title",
                    "body": "body"
                },
                "badge": 8,
                "sound": "default"
            }
        }
    

    Now, compressing this JSON into

    {"aps":{"alert":{"title":"title","subtitle":"sub title","body":"body"},"badge":8,"sound":"default"}}

    worked for my iOS12 (iPhone7) and iOS10 (iPhone5s) testing devices !!!

    Now for fun, stick a space between "sound": and "default" and you will not get a sound in iOS10, but iOS12 device would play it on notification arrival... go figure...

  • DR_nathouDR_nathou BEMember ✭✭

    This work fine on Android 5+ And iOS(12) only iOS 12 has been tested. but I'm pretty confident, iOS is reliable.
    So writing "mynotificationsound.wav" is fine for both

    Sounds must be in

    • Resources/raw as AndroidResource For Project.Droid
    • Resources as BundleResource for Project.iOS

      {
                  "notification_target": {
                      "type": "user_ids_target",
                      "user_ids": [
                          "p-aadlamo"
                      ]
                  },
                  "notification_content": {
                      "name": "NotificationId 0e9c6ae7-401a-42fd-9b84-f19b5925aae5",
                      "title": " BLABLA",
                      "body": "BLABLA",
                      "custom_data": {
                          "sound": "coins.wav",
                          "badge": "1",
                          "color": "#CC0000",
                          "Entity": "DealEntity"
                      }
                  }
              }
      

    UWP, WPF, TIZEN, and GTK not tested

Sign In or Register to comment.