Reputation: 95
I’m trying to set a sound for push notification in the project which I am developing but it's not working in the device please tell me, how I can set the alert sound for the notification.
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error)
{
if( !error )
{
[[UIApplication sharedApplication] registerForRemoteNotifications];
NSLog( @"Push registration success." );
}
else
{
NSLog( @"Push registration FAILED" );
NSLog( @"ERROR: %@ - %@", error.localizedFailureReason, error.localizedDescription );
NSLog( @"SUGGESTIONS: %@ - %@", error.localizedRecoveryOptions, error.localizedRecoverySuggestion );
I am using the above code in "Appdelegate.m" in "didFinishLaunch" method can anyone please tell me where I'm going wrong.
Upvotes: 1
Views: 918
Reputation: 189
For remote notifications in iOS, you can specify a custom sound that iOS plays when it presents a local or remote notification for an app. The sound files can be in the main bundle of the client app or in the Library/Sounds folder of the app’s data container.
When you send push notification, just add the name of file in JSON payload. Example:
{
"aps" : {
"alert" : "Hey you got a push notification.",
"badge" : 1,
"sound" : "sound.mp3"
}
}
Just put the file (sound.mp3) inside your project bundle (i.e inside the hierarchy of project) and have Copy items if needed option selected while drag and drop.
Hope this will help.
Upvotes: 2