I m building a iOS application using Swift5. This application is able to receive push message from my php web application using Firebase.
Now I m able to display push message on iPhone when the iphone is locked and the application is not running. Now I can send some type of push message and if the user click on it, the application should be display a view with particular input parameter. But the view is not the same view, can change in base of PushMessage.
For example if you receive push message for whatsup application and click on it, the application show directly the chat of the user and not other chat.
Now this is my code, what can I do to implement it?
func configureNotification() {
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options:[.badge, .alert, .sound]){ (granted, error) in }
}
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil))
UIApplication.shared.registerForRemoteNotifications()
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
Messaging.messaging().token { token, error in
if let error = error {
print("Error fetching FCM registration token: \(error)")
} else if let token = token {
print("FCM registration token: \(token)")
USER_DEFAULT.set(token, forKey: IOS_TOKEN)
}
}
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
// k.iosRegisterId = deviceTokenString
Messaging.messaging().apnsToken = deviceToken
print("APNs device token: \(deviceTokenString)")
// USER_DEFAULT.set(deviceTokenString, forKey: IOS_TOKEN)
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("APNs registration failed: \(error)")
}
// MARK:- Received Remote Notification
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
Messaging.messaging().appDidReceiveMessage(userInfo)
print("main method - \(userInfo)")
if let info = userInfo as? Dictionary<String, AnyObject> {
//let alert1 = info["aps"]!["alert"] as! Dictionary<String, AnyObject>
let title = userInfo["gcm.notification.status"] as! String
hanleNotification(info: info, strStatus: title, strFrom: "Front")
}
completionHandler(UIBackgroundFetchResult.newData)
}
I want to receive push notifications from Firebase into my SwiftUI's app.
I can see my notification and works good but I don't know how to handle action values to do something with data. I read about didReceiveRemoteNotification but didn't work. Do you know any idea of what I'm doing wrong?
Thank you so much...
AppDelegate.swift
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
print(userInfo)
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
print(userInfo)
completionHandler(UIBackgroundFetchResult.newData)
}
This solves my life:
extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: #escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
let url = userInfo["url"] as! String
print(url)
completionHandler()
}
}
I am using firebase push notification iOS app, but in some condition we don't have to show push notification to user , only i have to use the data received along with push notification .
Payload sent from server :
{
to: iosTokenList[i],
notification: {
title: 'Hidden',
body: "Hidden",
Data: {
"Id":Id,
"someList":SomeList,
"status": "inactive"
},
notificationType: "status"
},
Problem
When i am not sending notification.title and notification.body then push notification in not received in ios app. No methods are getting called in appDelegate ,
But when we are adding notification.title=somevalue and notification.body=somevalue , willPresent is called .
AppDelegate
#available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
NotificationParser.parseIncommingMessages(notification: notification)
completionHandler([.alert, .sound])
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
print("PushNotifications : \(userInfo)")
if #available(iOS 10.0, *){
}else{
let dict = userInfo as NSDictionary
if let alert = dict.value(forKeyPath: "aps.alert") as? String{
let alert = UIAlertController(title: appTitle , message: alert + "didReceiveRemoteNotification", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
appDel.window?.rootViewController?.present(alert, animated: true, completion: nil)
}
}
}
How to get push notification in background? I have a problem in Swift4 iOS 11 with push notifications with FCM unable to retrieve notifications in the background only when the application is opened.
This is my coding:
AppDelegate.swift
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate {
var window: UIWindow?
static var DEVICE_ID = String()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.statusBarStyle = .lightContent
FirebaseApp.configure()
if #available(iOS 10.0, *) {
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound], completionHandler: { (success, error) in
print(success)
})
Messaging.messaging().delegate = self
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
return true
}
func connectToFcm() {
Messaging.messaging().shouldEstablishDirectChannel = true
if let token = InstanceID.instanceID().token() {
AppDelegate.DEVICE_ID = token
print("*********")
print("Token Instance: \(token)")
print("*********")
}
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .sound])
}
func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
connectToFcm()
}
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
guard let data: [String: Any] = remoteMessage.appData as? [String: Any] else {
return
}
print(data)
}
func applicationDidBecomeActive(_ application: UIApplication) {
UIApplication.shared.applicationIconBadgeNumber = 0
connectToFcm()
}
func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
completionHandler(.newData)
}
}
Example: image send push notification with postman
this source code in AppDelegate.swift:
import UIKit
import UserNotifications
import FirebaseCore
import FirebaseMessaging
import FirebaseInstanceID
struct DataNotif: Codable {
var title: String?
var body: String?
private enum CodingKeys: String, CodingKey {
case title
case body
}
}
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate {
var window: UIWindow?
let gcmMessageIDKey = "message_id"
static var DEVICE_ID = String()
var msg_body = ""
var msg_title = ""
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.statusBarStyle = .lightContent
FirebaseApp.configure()
Messaging.messaging().delegate = self
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,
completionHandler: {_, _ in })
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
return true
}
func connectToFcm() {
Messaging.messaging().shouldEstablishDirectChannel = true
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
if let refreshedToken = InstanceID.instanceID().token() {
AppDelegate.DEVICE_ID = refreshedToken
print("*********")
print("InstanceID token: \(refreshedToken)")
print("*********")
}else{
print("Can't get token device")
}
connectToFcm()
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Failed to register for remote notifications with error: \(error)")
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
print(userInfo)
guard let data: [String: Any] = userInfo as? [String: Any] else {
return
}
let listData = data["notification"] as! String
let jsonData = listData.data(using: .utf8)
do {
let decoder = JSONDecoder()
let dataJson = try decoder.decode(DataNotif.self, from: jsonData!)
msg_body = dataJson.body!
msg_title = dataJson.title!
createNotification(title: msg_title, body: msg_body)
}catch{
print("error")
}
completionHandler(.newData)
}
// messaging
func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
if let token = InstanceID.instanceID().token() {
AppDelegate.DEVICE_ID = token
print("*********")
print("Token Instance: \(token)")
print("*********")
}
connectToFcm()
}
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
print("Received data message: \(remoteMessage.appData)")
guard let data: [String: Any] = remoteMessage.appData as? [String: Any] else {
return
}
print(data)
let listData = data["notification"] as! String
let jsonData = listData.data(using: .utf8)
do {
let decoder = JSONDecoder()
let dataJson = try decoder.decode(DataNotif.self, from: jsonData!)
msg_body = dataJson.body!
msg_title = dataJson.title!
createNotification(title: msg_title, body: msg_body)
}catch{
print("error")
}
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .badge, .sound])
}
func applicationDidBecomeActive(_ application: UIApplication) {
UIApplication.shared.applicationIconBadgeNumber = 0
connectToFcm()
}
func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
if let vc = window?.rootViewController as? HomeController {
vc.scheduleNotification()
}
completionHandler(.newData)
}
func applicationDidEnterBackground(_ application: UIApplication) {
Messaging.messaging().shouldEstablishDirectChannel = false
print("Disconnect FCM")
}
func createNotification(title: String, body: String) {
let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationString(forKey: title, arguments: nil)
content.body = NSString.localizedUserNotificationString(forKey: body, arguments: nil)
content.sound = UNNotificationSound.default()
content.badge = NSNumber(integerLiteral: UIApplication.shared.applicationIconBadgeNumber + 1)
let request = UNNotificationRequest.init(identifier: "pushNotif", content: content, trigger: nil)
let center = UNUserNotificationCenter.current()
center.add(request)
}
}
Firebase provides background and foreground support to Push Notifications. You can easily solve your problem by following steps:
In your AppDelegate.swift import firebase library
import Firebase
import FirebaseInstanceID
import FirebaseMessaging
import UserNotifications
Whenever app launch register for push notification service, add the following lines of code into your didFinishLaunchingWithOptions
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
registerForPushNotifications(application: application)
return true
}
Add extension methods of appDelegate to register for remote notification and to get device token from APNS
extension AppDelegate {
func registerForPushNotifications(application: UIApplication) {
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,
completionHandler: {_, _ in })
// For iOS 10 data message (sent via FCM
Messaging.messaging().delegate = self
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let token = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
let savedAPNSToken = UserDefaults.standard.object(forKey: "savedAPNSToken") as? String
if savedAPNSToken != token {
UserDefaults.standard.set(token, forKey: "savedAPNSToken")
UserDefaults.standard.synchronize()
Messaging.messaging().apnsToken = deviceToken
}
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print(error.localizedDescription)
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
completionHandler(UIBackgroundFetchResult.newData)
}
}
Use the following methods of notificationCenter to handle notification in the foreground and background states :
// MARK: - UNUserNotificationCenterDelegate
#available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {
// Receive displayed notifications for iOS 10 devices.
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
completionHandler([.alert])
}
/// Handle tap on the notification banner
///
/// - Parameters:
/// - center: Notification Center
/// - response: Notification response
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: #escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
completionHandler()
}
Firebase token renewel:
// MARK: - MessagingDelegate
extension AppDelegate : MessagingDelegate {
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
// Note: This callback is fired at each app startup and whenever a new token is generated.
let savedFCMToken = UserDefaults.standard.object(forKey: "savedFCMToken") as? String
if savedFCMToken != fcmToken {
UserDefaults.standard.set(fcmToken, forKey: "savedFCMToken")
UserDefaults.standard.synchronize()
// Update FCMToken to server by doing API call...
}
}
}
I am not able to find out the issue in your code but I am posting the steps how it should be done :-
Register for Push notifications
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
self.registerForPushNotifications(application)
return true
}
func registerForPushNotifications(_ application: UIApplication){
Messaging.messaging().delegate = self
if #available(iOS 10, *) {
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
// Enable or disable features based on authorization.
}
center.delegate=self
application.registerForRemoteNotifications()
}
else{
let settings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
}
}
2.Then handle your didRegisterForRemoteNotificationsWithDeviceToken delegate :-
func application(_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken
}
Handle your Firebase Messaging delegate :-
extension AppDelegate: MessagingDelegate {
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
let token = fcmToken
// you can send your token to Server, from where you'll get the Push Notification
}
}
4.Then handle your UNUserNotificationCenterDelegate :-
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
// this is called when application is Foreground and push arrives we show alert, sound, here.
completionHandler([.alert, .sound])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Swift.Void){
let pushDictionary = response.notification.request.content.userInfo
// handle your push here
}
I'm using firebase push notification service in my iOS app which is developed using swift 3. Currently im getting notification. But i have configured custom sound for notification alert. What happen is when app is closed im getting a default sound not the cutmized sound. But i want to play the custom sound. How can i do this ?
Here is what i have done:
import UIKit
import UserNotifications
import Firebase
import FirebaseInstanceID
import FirebaseMessaging
import GoogleMaps
import GooglePlaces
import GooglePlacePicker
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
//For FireBase Configs
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
FirebaseApp.configure()
//FireBase Configs
return true
}
func application(_ application: UIApplication,open url: URL,sourceApplication: String?, annotation: Any) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application,open: url as URL!,sourceApplication: sourceApplication,annotation: annotation)
}
//FireBase Notifications Begins
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
//play alert sound
utilityHelper.playAudio(fileName: "udio", fileExtenstion: "mp3")
let json = JSON(userInfo)
NSLog("Received a Notificat`ion: \(json)")
completionHandler(UIBackgroundFetchResult.newData)
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
NSLog("show notification")
completionHandler(UNNotificationPresentationOptions.alert)
}
func application(received remoteMessage: MessagingRemoteMessage) {
}
func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
NSLog("Refreshed Firebase registration token: \(fcmToken)")
}
func application(_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken as Data
NSLog("Register FireBase Token.")
}
//FireBase Notifications End
}
How can i do this, any help would appreciate?
What I recently discovered is that on target under ios9 custom sound doesnt play as well as on device with ios 10.0.*.
So just set your build target either >= 9 or use device with iOS >= 10.1