APN custom notification sound issue - ios

I have remote notifications setup and working fine. I however have to play a custom sound when a notification is received (when app is NOT in foreground). I have added a file called customSound.wav to my app bundle and made sure "add to targets" is selected when i drag and drop it into my project (is this the correct way?). Server-side: the same file name has been specified as "sound": "customSound.wav". In my didReceiveRemoteNotification i am printing the userInfo dictionary and it is coming up like this:
aps =
{
alert = "Good Evening Sir";
badge = 1;
"custom_filed1" = 1;
"custom_field2" = "AAA-BBB-CCC";
name = "Sir Hubert";
sound = default;
};
As you can see, sound is still "default". I am aware of the fact that if the sound specified can not be found, the default sound is played- will the sound value in the dictionary also be "default" or it should be the file name that was specified in payload json. Is the order in which these are specified in the payload important. I have tried suggestions from other threads but not working for me. I can not show the json as I do not have access to the system at the moment. Any suggestions as to what I may be doing wrong ?

No your sound key should have the sound file name you want to play.
{
aps =
{
alert = "message";
sound = "sound file name.extension";
badge = 1;
};
}
eg:
{
aps =
{
alert = "message";
sound = "tone.caf";
};
}
I am using this and getting successful custom alert on my notification;
keep sound duration as small as possible. I did with this file format: aiff, caf
if u want to test notification then u can try this app https://itunes.apple.com/us/app/easy-apns-provider-push-notification/id989622350?mt=12
I am not doing any marking of this app but it is good for testing push notification

Related

local notification is not displayed

I'm working on a Xamarion iOS app. I want to display a local notification when an event occurs in my app, Like Outlook does when an email is received.
I'm using the following code, after having received the right to send notification from the user, of course:
var content = new UNMutableNotificationContent();
if (!string.IsNullOrWhiteSpace(value: mySoundFile))
{
content.Sound = UNNotificationSound.GetCriticalSound(mySoundFile);
}
if (!string.IsNullOrEmpty(myChannelDescription))
{
content.ThreadIdentifier = myChannelDescription;
}
content.Title = "MyTitle";
content.Body = "MyText";
var trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(0.1, false);
var request = UNNotificationRequest.FromIdentifier(notification.Id.ToString(), content, trigger);
var center = UNUserNotificationCenter.Current;
center.AddNotificationRequest(request, null);
But the notification is not displayed.
Any help appreciated.
The reason for not displaying the notification could be several things. Try the following solutions:
Make sure that the application has the required rights to send notifications and that the user has enabled them.
Check if the sound file is valid and located in the application package. It may also be necessary to add it to the Info.plist file.
Check if the thread identifier is valid and not repeatable.
Check if the trigger time is valid. Setting 0.1 seconds is very short and may not be enough to display the notification.
If the above solutions do not work, it is worth using debugging tools to more closely examine why the notification is not displayed.
On iOS, you must request permission to use notifications before attempting to schedule them. Just like this, you can try to check if the following code is added to your project:
UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert, (approved, err) =>
{
...
});
You can change your AddNotificationRequest as follows to see if there is an error in the notification:
center.AddNotificationRequest(request, (err) =>
{
if (err != null)
{
throw new Exception($"Failed to schedule notification: {err}");
}
});
For more details, you can refer to the following documents to check some permission issues:
Enhanced User Notifications in Xamarin.iOS | Microsoft
Asking permission to use notifications | Apple Developer
UpDate: If your app is in the foreground. You could try implementing the delegate userNotificationCenter(_:willPresent:withCompletionHandler:) which will be called when a notification arrives while the app is in the foreground. Refer to the following code:
UNUserNotificationCenter.Current.Delegate = new TestDelegate();
public class TestDelegate: UNUserNotificationCenterDelegate
{
public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
{
completionHandler(UNNotificationPresentationOptions.Alert);
}
}

ios 13 push-notification gets cleared as soon as we get when device is locked

when I'm receiving push notification on iOS 13, and device is locked. I' getting pushnotifcation, but it gets cleared immediately without any user interaction.
I'm using .net and following is payload code which I'm using
{to = notification.DeviceToken,
priority = "high",
content_available = true,
notification = new
{
body = notification.Message, //This will be a subtitle..
title = notification.Name, //This will be a title..
},
data = new
{
notificationId = notification.NotificationId,
userId = notification.UserId,
encryptedNotification = Crypto.Encrypt("id", notification.NotificationId.ToString())
}
};
string postbody = JsonConvert.SerializeObject(payload).ToString();
Byte[] byteArray = Encoding.UTF8.GetBytes(postbody);
tRequest.ContentLength = byteArray.Length;
**tRequest.Headers.Add("apns-push-type", "alert");**
**tRequest.Headers.Add("apns-priority", "5");**
}```
Please guide me what wrong in code.
have referred following azure code to implement, but not able to find the cause.
https://learn.microsoft.com/en-us/azure/notification-hubs/push-notification-updates-ios-13
This can happen if you have a code that decreases the app icon's notification badge number. If you do that, make sure you do it only if the application is active. If it is inactive and you decrease the badge number, it might cause the notification to appear and immediately disappear.

Retrieving a deeplink in a notification received while the app was closed

I recently found out that, when my app was closed (I'm talking about it being completely closed, not in the background), any push with deeplink that I open will start the app, but that's it, the deeplink is never taken into account.
After investigating, I realized that when the app is closed and you open it via a push, application:didReceiveRemoteNotification is not called. It is up to the dev to check, in application:didFinishLaunchingWithOptions, the launchOptions dictionary, and look for the value of UIApplicationLaunchOptionsRemoteNotificationKey, meaning that the app was opened via a push.
Here's the description of launchOptions when opening my app in such a way :
Printing description of launchOptions:
{
UIApplicationLaunchOptionsRemoteNotificationKey = {
aps = {
alert = {
"action-loc-key" = "_push-title_";
body = "_push-budy_";
};
"content-available" = 1;
};
azme = {
au = "_deeplink-url_";
ci = "a-1";
dt = b;
pid = 5130;
};
};
}
So, not knowing any other way, I have to check for the presence of a value for UIApplicationLaunchOptionsRemoteNotificationKey in the launchOptions, then get the value for the key "azme", and the value for the key "au", which seems like a totally wrong way to do it, and the open the URL as I normally do.
I could not find a better/more conventional way to do this, is there one ?
Thanks for the help !

Can't Get GCM Notification from background ios sdk

I can't get GCM notification from background, Although can receive it on foreground such as these:
{
aps = {
alert = {
body = fffff;
title = "\U067e\U06cc\U0627\U0645";
};
badge = 9;
sound = default;
};
"gcm.message_id" = "0:1448218309944532%075c2cd9075c2cd9";
}
Any help?
To get notifications in background, as per apple documentation
"Remote notifications" option needs to be enabled in Background modes under Capabilities in the target settings.
and normally when you receive any notification at that time application:didReceiveRemoteNotification: this method called.
and if you want to call this method in background, then you should sent a silent pushnotification. and for that you have to add content-available key with value 1 into the notification payload.
you JSON response contain that key like below example
{
aps = {
"content-available" : 1,
sound : ""
};
}

is there a way to let the user choose the sound for local notification?

I have an app that has 64 local notifications with repeat intervals. So I can't add more notifications, or otherwise, I'd need the user to open the app again for a reschedule.
I was wondering if I'm able to let the user choose the file with in this code:
notif.soundName = #"xxx.wav";
so they can choose xyz.wav instead?
Yes, you can let the user choose the sound between some that are already in your project or you could also let him take a song from the music library.
Update:
If the files are already in your Xcode project, you can create a NSString or a int value to store the user choose. You create a button that let the user choose a sound. You connect this button to change the string or int value. Then you create and if statement that check this string or int and change properly the UILocalnotification sound.
int soundNumber;
- (IBAction)chooseSoundName {
soundNumber = 1;
}
- (void)loadSoundName {
if(SoundNumber == 1) {
localNotification.soundName = #"first.wav";
}
}
Then you call the loadSoundName method (or you could integrate it into another method).
Hope it helps

Resources