iOS Enable Sound for Push Notification Sound FCM
up vote
1
down vote
favorite
I'm trying to enable sound for my Firebase push notifications and I'm not sure if there is code in the App Delegate which I need to implement, or if the code in my index.js is wrong.
I thought there was something I needed to import in AppDelegate related to sound, but all the guides I've found for implementing push notifications only have the basic code where [options] contains the only thing related to the notification's sound.
index.js Code:
var notification = {
notification: {
title: conversation.conversationName,
body: user.username + ': ' + message.text,
sound: 'default'
},
topic: topic
}
App Delegate Code: Function called in didFinishLaunchingWithOptions.
import UIKit
import Firebase
import UserNotifications
private func attemptRegisterForNotifications(application: UIApplication) {
Messaging.messaging().delegate = self
UNUserNotificationCenter.current().delegate = self
let options: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().getNotificationSettings { (settings) in
if settings.authorizationStatus == .authorized {
// Push notifications already authorized, do nothing
print ("push notifications authorized")
} else if settings.authorizationStatus == .notDetermined {
// User hasn't specified notification status
UNUserNotificationCenter.current().requestAuthorization(options: options, completionHandler: { (granted, error) in
if let error = error {
print ("Failed to request authorization:", error)
return
}
guard granted else {return}
DispatchQueue.main.async {
application.registerForRemoteNotifications()
}
})
} else if settings.authorizationStatus == .denied {
// User has denied notifications
UNUserNotificationCenter.current().requestAuthorization(options: options, completionHandler: { (granted, error) in
if let error = error {
print ("Failed to request authorization:", error)
return
}
let alertController = UIAlertController(title: "Enable Push Notifications", message: "Enable push notifications for optimal chat experience", preferredStyle: .alert)
let settingsAction = UIAlertAction(title: "Settings", style: .default) { (_) -> Void in
guard let settingsUrl = URL(string: UIApplication.openSettingsURLString) else {
return
}
if UIApplication.shared.canOpenURL(settingsUrl) {
UIApplication.shared.open(settingsUrl, completionHandler: { (success) in
})
}
}
let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
alertController.addAction(cancelAction)
alertController.addAction(settingsAction)
alertController.preferredAction = settingsAction
DispatchQueue.main.async {
self.window?.rootViewController?.present(alertController, animated: true, completion: nil)
}
})
}
}
}
javascript ios firebase firebase-cloud-messaging
add a comment |
up vote
1
down vote
favorite
I'm trying to enable sound for my Firebase push notifications and I'm not sure if there is code in the App Delegate which I need to implement, or if the code in my index.js is wrong.
I thought there was something I needed to import in AppDelegate related to sound, but all the guides I've found for implementing push notifications only have the basic code where [options] contains the only thing related to the notification's sound.
index.js Code:
var notification = {
notification: {
title: conversation.conversationName,
body: user.username + ': ' + message.text,
sound: 'default'
},
topic: topic
}
App Delegate Code: Function called in didFinishLaunchingWithOptions.
import UIKit
import Firebase
import UserNotifications
private func attemptRegisterForNotifications(application: UIApplication) {
Messaging.messaging().delegate = self
UNUserNotificationCenter.current().delegate = self
let options: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().getNotificationSettings { (settings) in
if settings.authorizationStatus == .authorized {
// Push notifications already authorized, do nothing
print ("push notifications authorized")
} else if settings.authorizationStatus == .notDetermined {
// User hasn't specified notification status
UNUserNotificationCenter.current().requestAuthorization(options: options, completionHandler: { (granted, error) in
if let error = error {
print ("Failed to request authorization:", error)
return
}
guard granted else {return}
DispatchQueue.main.async {
application.registerForRemoteNotifications()
}
})
} else if settings.authorizationStatus == .denied {
// User has denied notifications
UNUserNotificationCenter.current().requestAuthorization(options: options, completionHandler: { (granted, error) in
if let error = error {
print ("Failed to request authorization:", error)
return
}
let alertController = UIAlertController(title: "Enable Push Notifications", message: "Enable push notifications for optimal chat experience", preferredStyle: .alert)
let settingsAction = UIAlertAction(title: "Settings", style: .default) { (_) -> Void in
guard let settingsUrl = URL(string: UIApplication.openSettingsURLString) else {
return
}
if UIApplication.shared.canOpenURL(settingsUrl) {
UIApplication.shared.open(settingsUrl, completionHandler: { (success) in
})
}
}
let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
alertController.addAction(cancelAction)
alertController.addAction(settingsAction)
alertController.preferredAction = settingsAction
DispatchQueue.main.async {
self.window?.rootViewController?.present(alertController, animated: true, completion: nil)
}
})
}
}
}
javascript ios firebase firebase-cloud-messaging
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm trying to enable sound for my Firebase push notifications and I'm not sure if there is code in the App Delegate which I need to implement, or if the code in my index.js is wrong.
I thought there was something I needed to import in AppDelegate related to sound, but all the guides I've found for implementing push notifications only have the basic code where [options] contains the only thing related to the notification's sound.
index.js Code:
var notification = {
notification: {
title: conversation.conversationName,
body: user.username + ': ' + message.text,
sound: 'default'
},
topic: topic
}
App Delegate Code: Function called in didFinishLaunchingWithOptions.
import UIKit
import Firebase
import UserNotifications
private func attemptRegisterForNotifications(application: UIApplication) {
Messaging.messaging().delegate = self
UNUserNotificationCenter.current().delegate = self
let options: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().getNotificationSettings { (settings) in
if settings.authorizationStatus == .authorized {
// Push notifications already authorized, do nothing
print ("push notifications authorized")
} else if settings.authorizationStatus == .notDetermined {
// User hasn't specified notification status
UNUserNotificationCenter.current().requestAuthorization(options: options, completionHandler: { (granted, error) in
if let error = error {
print ("Failed to request authorization:", error)
return
}
guard granted else {return}
DispatchQueue.main.async {
application.registerForRemoteNotifications()
}
})
} else if settings.authorizationStatus == .denied {
// User has denied notifications
UNUserNotificationCenter.current().requestAuthorization(options: options, completionHandler: { (granted, error) in
if let error = error {
print ("Failed to request authorization:", error)
return
}
let alertController = UIAlertController(title: "Enable Push Notifications", message: "Enable push notifications for optimal chat experience", preferredStyle: .alert)
let settingsAction = UIAlertAction(title: "Settings", style: .default) { (_) -> Void in
guard let settingsUrl = URL(string: UIApplication.openSettingsURLString) else {
return
}
if UIApplication.shared.canOpenURL(settingsUrl) {
UIApplication.shared.open(settingsUrl, completionHandler: { (success) in
})
}
}
let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
alertController.addAction(cancelAction)
alertController.addAction(settingsAction)
alertController.preferredAction = settingsAction
DispatchQueue.main.async {
self.window?.rootViewController?.present(alertController, animated: true, completion: nil)
}
})
}
}
}
javascript ios firebase firebase-cloud-messaging
I'm trying to enable sound for my Firebase push notifications and I'm not sure if there is code in the App Delegate which I need to implement, or if the code in my index.js is wrong.
I thought there was something I needed to import in AppDelegate related to sound, but all the guides I've found for implementing push notifications only have the basic code where [options] contains the only thing related to the notification's sound.
index.js Code:
var notification = {
notification: {
title: conversation.conversationName,
body: user.username + ': ' + message.text,
sound: 'default'
},
topic: topic
}
App Delegate Code: Function called in didFinishLaunchingWithOptions.
import UIKit
import Firebase
import UserNotifications
private func attemptRegisterForNotifications(application: UIApplication) {
Messaging.messaging().delegate = self
UNUserNotificationCenter.current().delegate = self
let options: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().getNotificationSettings { (settings) in
if settings.authorizationStatus == .authorized {
// Push notifications already authorized, do nothing
print ("push notifications authorized")
} else if settings.authorizationStatus == .notDetermined {
// User hasn't specified notification status
UNUserNotificationCenter.current().requestAuthorization(options: options, completionHandler: { (granted, error) in
if let error = error {
print ("Failed to request authorization:", error)
return
}
guard granted else {return}
DispatchQueue.main.async {
application.registerForRemoteNotifications()
}
})
} else if settings.authorizationStatus == .denied {
// User has denied notifications
UNUserNotificationCenter.current().requestAuthorization(options: options, completionHandler: { (granted, error) in
if let error = error {
print ("Failed to request authorization:", error)
return
}
let alertController = UIAlertController(title: "Enable Push Notifications", message: "Enable push notifications for optimal chat experience", preferredStyle: .alert)
let settingsAction = UIAlertAction(title: "Settings", style: .default) { (_) -> Void in
guard let settingsUrl = URL(string: UIApplication.openSettingsURLString) else {
return
}
if UIApplication.shared.canOpenURL(settingsUrl) {
UIApplication.shared.open(settingsUrl, completionHandler: { (success) in
})
}
}
let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
alertController.addAction(cancelAction)
alertController.addAction(settingsAction)
alertController.preferredAction = settingsAction
DispatchQueue.main.async {
self.window?.rootViewController?.present(alertController, animated: true, completion: nil)
}
})
}
}
}
javascript ios firebase firebase-cloud-messaging
javascript ios firebase firebase-cloud-messaging
edited Nov 11 at 1:13
Frank van Puffelen
220k25361387
220k25361387
asked Nov 11 at 0:26
Eric
4617
4617
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244743%2fios-enable-sound-for-push-notification-sound-fcm%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown