swiftUI UNUserNotificationCenter notification sound not playing - swift

I'm trying to set my notification system. Here how am I doing.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
application.registerForRemoteNotifications()
}
This is working great, except that the phone who receive the notification never play a sound. Like when I receive a Facebook notification or message notification.
I don't know why this is not working! Does someone got the same issue and/or know how to resolve it?

Related

Please implement -messaging:didReceiveRegistrationToken: to be provided with an FCM token. FLUTTER - IOS

I am integrating Firebase Push notification in my Flutter project. Push notifications are working fine in Android but I am getting following error when trying to run IOS app:-
[FirebaseMessaging][I-FCM002023] The object <Runner.AppDelegate: 0x2837f4b10> does not respond to -messaging:didReceiveRegistrationToken:. Please implement -messaging:didReceiveRegistrationToken: to be provided with an FCM token.
Following is my AppDelegate Code:-
`
import UIKit
import Flutter
import UserNotifications
import Firebase
#UIApplicationMain
#objc class AppDelegate: FlutterAppDelegate, MessagingDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
FirebaseApp.configure()
Messaging.messaging().delegate = self
GeneratedPluginRegistrant.register(with: self)
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
`
Trying to integrate Firebase Notification in FLutter App and expecting Push notification in IOS.
Basically, you need to implement didReceiveRegistrationToken so that the app will generate FCM token. You can check out this similar post.

Problems Notifications Firebase IOS

I've followed Apple's suggestions to implement push notifications with Firebase, but I can't get notifications. I have created the APNS key in firebase and I have activated the notifications in the app. In Siginig & Capabilities I have added "Push Notification" "Background Modules" activating Remote notifiactions. This is the code in AppDelegate:
import UIKit import FirebaseCore import FirebaseMessaging
#UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool{
//Firebase
FirebaseApp.configure()
// Push Notifications
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert,
.badge, .sound]
UNUserNotificationCenter.current()
.requestAuthorization(
options: authOptions,
completionHandler: {_, _ in})
application.registerForRemoteNotifications()
Messaging.messaging().subscribe(toTopic:"topic_general")
return true
}
}
extension AppDelegate: UNUserNotificationCenterDelegate{
// Push Notifications Mostrar aunque el movil este activo / Segundo Plano
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandle: #escaping
(UNNotificationPresentationOptions) -> Void){
completionHandle([.alert, .badge, .sound])
}
}
Problems Notifications Firebase IOS
I just looked through what you did and a working version of Firebase Notifications I have running. I would try to share where there are differences
import UIKit
import Firebase
import FirebaseMessaging
import FirebaseCore
Then
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
if #available(iOS 10.0, *) {
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(options: authOptions) { (status, pushError) in
if !status {
print(pushError?.localizedDescription as Any)
}
}
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
Messaging.messaging().delegate = self
return true
}
after which
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
let pushedInfo = notification.request.content.userInfo
print(pushedInfo, "JSON Content of the Push Notification")
}
My ideas as to why you might not be getting the notifications:
Push notifications can only be tested on a physical device. Doesn't work on simulator
It is possible that push notifications is not authorised on the device.
Kindly check these ideas and the code differences. It is highly possible that the issue is somewhere there.
PS: I don't remember why I checked for #available(iOS 10.0, *) the code is a bit old.

Using UNUserNotificationCenter for iOS 10

Attempting to work with Firebase to register for remote notifications however when implementing the following code I get the error:
UNUserNotificationCenter is only available on iOS 10.0 or newer
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
var soundID: SystemSoundID = 0
let soundFile: String = NSBundle.mainBundle().pathForResource("symphony", ofType: "wav")!
let soundURL: NSURL = NSURL(fileURLWithPath: soundFile)
AudioServicesCreateSystemSoundID(soundURL, &soundID)
AudioServicesPlayAlertSound(soundID)
Fabric.with([Twitter.self])
//Firebase configuration
FIRApp.configure()
//Resource code from stackoverflow to create UNUserNotificationCenter
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
// Enable or disable features based on authorization.
}
application.registerForRemoteNotifications()
return true
}
By doing a simple "Fix-it" doesn't resolve my issue by creating an if statement based on the OS version number. What should I be doing or thinking towards this solution for the UserNotifications framework?
For one thing, with the new UNUserNotificationCenter, you only want to register for remote notifications if the user grants permission. The way your code is setup, you're trying to do it regardless of permission which could be one of the reasons. You should do something like this:
import UserNotifications
...
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
}
return true
}
If you need to check if the user has an OS lower than iOS 10.0 - you could try something like this to include the old system:
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
}
} else {
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound | UIUserNotificationType.Alert |
UIUserNotificationType.Badge, categories: nil))
}
Let me know if this works, and if it's what you're trying to accomplish. If not, I'll remove my answer.

Swift : Register notifications from Parse.com doesn't stay enable

I've configured my program to receive notification from Parse and I've enabled it with these lines :
var pushSettings : UIUserNotificationSettings = UIUserNotificationSettings(forTypes: .Alert, categories: nil)
application.registerUserNotificationSettings(pushSettings)
application.registerForRemoteNotifications()
let userNotificationTypes = (UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound);
let settings = UIUserNotificationSettings(forTypes: userNotificationTypes, categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
The declaration is in the
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
from my appDelegate, but when I close my program and relaunch it, all of the parameters are reset and I get them "off"
Does anyone have an idea?

Local notification while app not running

Is it possible to show some kind of a local notification from an app, even if the app isn't running(also not in background)?
For example a daily reminder or something like that. I know that it is possible with push-notifications, but it doesn't fit for my app.
You can easily schedule local notifications, and they will be presented at the scheduled date and time regardless of the app's state.
First you need to get permission from the user to present notifications, like this:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound|UIUserNotificationType.Alert | UIUserNotificationType.Badge, categories: nil))
return true
}
Then you create the notification like this:
var localNotification:UILocalNotification = UILocalNotification()
localNotification.alertAction = "This is"
localNotification.alertBody = "A notification"
localNotification.fireDate = NSDate(timeIntervalSinceNow: 15)
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
Have a look at the Local and Remote Notification Programming Guide.
In AppDelegate, use this function instead
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
application.registerUserNotificationSettings(settings)
return true;
}
Most of the solutions outlined above have all been deprecated.
Use this UNUserNotification APIs instead
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
registerForLocalNotification()
return true
}
func registerForLocalNotification() {
UNUserNotificationCenter.current()
.requestAuthorization(options: [.alert, .sound, .badge]) { [weak self] granted, _ in
print("Permission: \(granted)")
}
}