iOS Push notification sound problem

David.WelshDavid.Welsh USMember
edited January 2016 in Xamarin.iOS

I'm working on push notifications on my xamarin.forms app and everything is working fine, except the sound for the notification on iOS. Using Push Sharp.

This is my current code:

private void SendToiOsButton_OnClick(object sender, RoutedEventArgs e)
{
    var push = new PushBroker();

    var certificateByteArray = File.ReadAllBytes("certificate.p12");

    const certificatePassword = "password";

    push.RegisterAppleService(new ApplePushChannelSettings(true, certificateByteArray,
        certificatePassword));
    push.QueueNotification(new AppleNotification()
        .ForDeviceToken(MyDeviceTokenTextBox.Text.Replace(" ", string.Empty))
        .WithAlert(MyTextBox.Text)
        .WithSound("default")
        .WithBadge(int.Parse(MyBadgeNumberTextBox.Text))
        .WithCustomItem("message", JsonConvert.SerializeObject(new PushNotification()
        {
            Title = MyTextBox.Text,
            Message = MyTextBox.Text,
            Type = 7
        })));
}
  • I've also tried putting the sound into the .WithCustomItem.
  • I've tried specifying a particular file name such as sms-received1.caf.
  • I've tried using the full path, /System/Library/Audio/UISounds/sms-received1.caf.
  • I've checked that the phone volume is up and that the switch to disable the sound isn't set.

I noticed it doesn't vibrate either. Maybe related.

Any ideas as to where I'm going wrong?

Posts

  • NielsCupNielsCup NLMember ✭✭✭

    1) Try using:

    .WithSound("sms-received1.caf");

    2) Add your sms-received1.caf to your iOS project Resources folder
    3) Right click sms-received1.caf and set build action to "BundleResource"

  • David.WelshDavid.Welsh USMember

    Just got the sound working. Was missing a part on the client side unrelated to this. I didn't need to include a local file in my solution in the end. I was able to provide a full path to the platform stock sound that I wanted.

    Next question. I only get the sound on the app when the app is in the foreground, not when the app is in the background. Anyone got any ideas?

  • NielsCupNielsCup NLMember ✭✭✭

    This may be caused by your iOS version, Apple made some changes since iOS 8.0. Your registration should look something like this:

    if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0)) { UIUserNotificationType userNotificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound; UIUserNotificationSettings settings = UIUserNotificationSettings.GetSettingsForTypes(userNotificationTypes, null); UIApplication.SharedApplication.RegisterUserNotificationSettings(settings); } else { UIRemoteNotificationType notificationTypes = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound; UIApplication.SharedApplication.RegisterForRemoteNotificationTypes(notificationTypes); }

  • CrosaCrosa USMember ✭✭

    Is there any way to get the sound when app is in background( from Firebase Push Notification )?

Sign In or Register to comment.