question

Kim Strasser avatar image
Kim Strasser asked

Push notification template: How can I play a sound when my push notification is received on my device?

The notification is displayed but no sound is played when my push notification is displayed on my iPad Air. I have not yet tried if a sound is played on my Android Samsung tablet but on iOS it definitely doesn't work. Is it possible to enable the default device sound in the push notification template payload? How can I do this? 5516-bildschirmfoto-2023-02-10-um-115630.png

Push Notifications
4 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Neils Shi avatar image Neils Shi commented ·

For the question of how to play a sound when receiving notification on IOS device, you can refer to this post answered by mflorin. Please let me know if this doesn't work.

0 Likes 0 ·
Kim Strasser avatar image Kim Strasser Neils Shi commented ·

I don't know how to use the code from the post with my code. Is it possible to enable the sound in the JSON payload?

 Program.cs:
 public override void FinishedLaunching(UIApplication app)
 {        

 UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound | UNAuthorizationOptions.Alert, (approved, err) => { });
 UNUserNotificationCenter.Current.Delegate = new UserNotificationCenterDelegate();
 }

 using System;
 using Foundation;
 using UserNotifications;
 namespace MyiOSProject
 {
     public class UserNotificationCenterDelegate : UNUserNotificationCenterDelegate
     {
         [Export("userNotificationCenter:willPresentNotification:withCompletionHandler:")]
         public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
         {
         completionHandler(UNNotificationPresentationOptions.Sound | UNNotificationPresentationOptions.Badge | UNNotificationPresentationOptions.Banner | UNNotificationPresentationOptions.List);
         }
     }
 }
0 Likes 0 ·
Neils Shi avatar image Neils Shi Kim Strasser commented ·

To play a sound when receiving notification on IOS device, you should use the API SendPushNotification. This API’s request contains a field called "Package", and you can set Sound/Message in Package. You can refer to the request body below.

 "Recipient": "F8763A987E",
   "Package":{
       "Message":"We miss you",
       "Sound": "default",
       "Title":"Come back"
   }
0 Likes 0 ·
Show more comments

1 Answer

·
Neils Shi avatar image
Neils Shi answered

According to Payload Key Reference, if you want the system to play a sound, you should add the key "sound". You can set it at Notification Template's iOS payload. For more information about Creating the Remote Notification Payload, you can refer to this web.

3 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Kim Strasser avatar image Kim Strasser commented ·

I get this error when I try to save my push notification template:

Android custom payload is invalid.

5546-bildschirmfoto-2023-02-16-um-111621.png

But I don't know what is wrong with the Android payload.

0 Likes 0 ·
Neils Shi avatar image Neils Shi Kim Strasser commented ·

The format of the Android payload is not equivalent to the IOS payload. The Android payload consists of two key-value pairs, "notification" and "data". The "notification" key-value should contain the predefined key-value pairs of the android notification payload and "data" is an object that you can put your custom data into. For more information, you can refer to https://firebase.google.com/docs/cloud-messaging/http-server-ref

0 Likes 0 ·
Kim Strasser avatar image Kim Strasser Neils Shi commented ·

It works if I use this payload: Android: {"notification":{"badge":1,"sound":"default"}}

iOS: {"aps":{"badge":1,"sound":"default"}}

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.