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();
}
}
}
}