Is it potential to know if the app was launched/opened from a push notification? in swiftUI app iOS 15+
Whether it is potential find out how to get that push notification particulars
right here is my App Delegate code:
class AppDelegate: NSObject, UIApplicationDelegate {
@ObservedObject non-public var notificationHandler = PushNotificationHandler()
var viewModel = PendingCommandsViewModel()
var launchNotification: [AnyHashable: Any]?
let gcmMessageIDKey = "gcm.message_id"
func utility(_ utility: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
FirebaseApp.configure()
// Setting Up Cloud Messging..
Messaging.messaging().delegate = self
// Setting Up Notifications...
UNUserNotificationCenter.present().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.present().requestAuthorization(
choices: authOptions,
completionHandler: { _, _ in }
)
utility.registerForRemoteNotifications()
return true
}
func utility(_ utility: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
if let remoteNotification = launchOptions?[.remoteNotification] as? [AnyHashable: Any] {
launchNotification = remoteNotification
}
return true
}
func applicationDidBecomeActive(_ utility: UIApplication) {
if let notificationData = launchNotification{
NotificationCenter.default.publish(identify: .appOpenedOnNotificationClick, object: nil, userInfo: notificationData)
launchNotification = nil
}
}
func utility(_ utility: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: (messageID)")
}
// Print full message.
print(userInfo)
completionHandler(UIBackgroundFetchResult.newData)
}
// must implement this strategies to be able to obtain Notifications...
func utility(_ utility: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Unable to register for distant notifications: (error.localizedDescription)")
}
func utility(_ utility: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Knowledge) {
Messaging.messaging().apnsToken = deviceToken
}
}
// Cloud Messging...
extension AppDelegate: MessagingDelegate{
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
let dataDict: [String: String] = ["token": fcmToken ?? ""]
UserDefaults.normal.set(fcmToken ?? "", forKey: "DeviceToken")
NotificationCenter.default.publish(
identify: Notification.Identify("FCMToken"),
object: nil,
userInfo: dataDict
)
}
}
// Consumer Notifications... [InApp Notifications...]
extension AppDelegate: UNUserNotificationCenterDelegate{
func userNotificationCenter(_ heart: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content material.userInfo
// Do one thing with message ID
if let messageID = userInfo[gcmMessageIDKey] {
print("message ID: (messageID)")
}
// Print full message.
print("Push Notification Response --> (userInfo)")
viewModel.receivedNotification = userInfo
NotificationCenter.default.publish(identify: Notification.Identify("DidReceiveNotification"), object: nil, userInfo: userInfo)
// Change this to your most popular presentation choice
completionHandler([[.banner,.alert, .badge, .sound]])
}
// This perform might be known as proper after person faucet on the notification
func userNotificationCenter(_ heart: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("Did faucet response ---> (response.notification.request.content material.userInfo)")
let selectedNotificationRes = response.notification.request.content material.userInfo
NotificationCenter.default.publish(identify: Notification.Identify("DidTapOnNotification"), object: nil, userInfo: selectedNotificationRes)
print("NOtification tapped")
// onTapReceivedNotificationHanlde()
completionHandler()
}
}
Right here under is my Important app file code:
@essential
struct PrepareApp: App {
var token: String? = UserDefaults.getAccessToken()
@UIApplicationDelegateAdaptor(AppDelegate.self) var delegate
let persistenceController = CoreDataStack.shared
@StateObject var selectedQuestionIndex = SelectedQuestionIndex()
@StateObject non-public var networkMonitor = NetworkMonitor()
@Surroundings(.scenePhase) var scenePhase
var physique: some Scene {
WindowGroup {
SplashView()
.environmentObject(self.networkMonitor)
.environmentObject(self.selectedQuestionIndex)
.atmosphere(.managedObjectContext, persistenceController.container.viewContext)
}
}
}
right here under is my Splash views code:
struct SplashView: View {
@StateObject non-public var viewModel = SplashViewModel()
var launchNotification: [AnyHashable: Any]?
@EnvironmentObject var networkMonitor: NetworkMonitor
@EnvironmentObject var selectedQuestionIndex : SelectedQuestionIndex
@State non-public var showAlert = false
@State non-public var notificationMessage: String = ""
var physique: some View {
let screenWidth = UIScreen.essential.bounds.width
let firstImageWidth = deviceWidth * 0.2
let secondImageWidth = deviceWidth * 0.5
let largeDevicesOffset = (firstImageWidth - secondImageWidth) / 1.5
ZStack {
swap viewModel.splashViewState {
case .loadingSplash:
HStack(spacing: 0) {
if viewModel.showSingleLogo{
Picture("ic_SCOR_Logo")
.resizable()
.body(width: secondImageWidth, top: deviceWidth * 0.2)
Picture("ic_ENXT_Logo")
.resizable()
.body(width: firstImageWidth, top: deviceWidth * 0.2)
}else{
Picture("ic_ENXT_Logo")
.resizable()
.offset(x: viewModel.circleScale > 1 ? (secondImageWidth) : -secondImageWidth )
.body(width: firstImageWidth, top: deviceWidth * 0.2)
Picture("ic_SCOR_Logo")
.resizable()
.offset(x: viewModel.circleScale > 1 ? largeDevicesOffset : secondImageWidth )
.body(width: secondImageWidth, top: deviceWidth * 0.2)
}
}
.fullScreenCover(merchandise: $viewModel.showAppUpdates, content material: { appUpdate in
withAnimation(.none) {
AppUpdateView(appUpdateModel: appUpdate,
onUpdateClick: {
// viewModel.showAppUpdates = nil
viewModel.openAppStore()
//viewModel.onLaterClicked()
}, onLaterClick: {
//viewModel.showAppUpdates = nil
viewModel.onLaterClicked()
})
}
})
.background(
Circle()
.fill(Colour.blueColour)
.scaleEffect(viewModel.circleScale)
.body(width: deviceWidth * 0.5, top: deviceWidth * 0.5))
.onAppear {
withAnimation(Animation.easeInOut(length: 2.0)) {
viewModel.circleScale = 10.0
viewModel.swapImages.toggle()
DispatchQueue.essential.asyncAfter(deadline: .now() + 1.8){
viewModel.showSingleLogo.toggle()
viewModel.checkInAppUpdateAPI(isConnected: self.networkMonitor.isConnected)
}
}
}.transaction({ transaction in transaction.disablesAnimations = true
})
case .noUserLoggedIn:
EntityValidationView()
.environmentObject(self.networkMonitor)
.environmentObject(self.selectedQuestionIndex)
case .userLoggedIn:
CustomTabBar()
.environmentObject(self.networkMonitor)
.environmentObject(self.selectedQuestionIndex)
}
}.ignoresSafeArea()
}
}
and hereBelow is my customized tab bar view code
struct CustomTabBar: View {
@StateObject var viewModel: CustomTabBarViewModel = CustomTabBarViewModel()
@StateObject non-public var pcViewModel = PendingCommandsViewModel()
@StateObject non-public var notificationHandler = PushNotificationHandler()
@EnvironmentObject non-public var networkMonitor: NetworkMonitor
@Surroundings(.scenePhase) var scenePhase
var physique: some View {
AppNavigationBaseView{
ZStack{
VStack(spacing: 0) {
ZStack {
tabContentView()
}.body(maxWidth: .infinity, maxHeight: .infinity)
if viewModel.shouldShowTabBar{
Divider().foregroundColor(Colour.blackColour)
HStack(alignment: .prime) {
TabView(tab: Tab.HOME_VIEW, selectedTab: $viewModel.selectedView){ selectedTab in
if(viewModel.selectedView != selectedTab){
viewModel.selectTabView(tab: selectedTab)
}
// }
}.environmentObject(viewModel)
TabView(tab: Tab.TIME_LINE_VIEW, selectedTab: $viewModel.selectedView){ selectedTab in
if(viewModel.selectedView != selectedTab){
viewModel.viewType = .ET
viewModel.selectTabView(tab: selectedTab)
}
}.environmentObject(viewModel)
TabView(tab: Tab.COURSER_VIEW, selectedTab: $viewModel.selectedView){ selectedTab in
if(viewModel.selectedView != selectedTab){
viewModel.selectTabView(tab: selectedTab)
}
}.environmentObject(viewModel)
TabView(tab: Tab.USER_PROFILE_VIEW, selectedTab: $viewModel.selectedView){ selectedTab in
if(viewModel.selectedView != selectedTab){
viewModel.selectTabView(tab: selectedTab)
}
}.environmentObject(viewModel)
}.padding(.backside,24)
.body(maxWidth: .infinity)
.body(top: viewModel.shouldShowTabBar ? nil : 0)
.padding(.horizontal, 10)
}
}.body(maxWidth: .infinity, maxHeight: .infinity)
}.background(Colour.white)
.transparentNonAnimatingFullScreenCoverWithItem(merchandise: $viewModel.notificationDetailsToShow, content material: { particulars in
CustomPopup(content material: NotificationDetailsPopUpView(notifyDetails: $viewModel.notificationDetailsToShow)){}
})
.onAppear{
viewModel.shouldShowTabBar = true
viewModel.resetNavigation = false
if(viewModel.selectedView == Tab.INIT){
viewModel.selectTabView(tab: viewModel.firstView)
}
}
.onReceive(NotificationCenter.default.writer(for: Notification.Identify("DidReceiveNotification"))) { notification in
if let userInfo = notification.userInfo{
viewModel.ReceivedNotificationHandle(notification: userInfo)
}
}
.onReceive(NotificationCenter.default.writer(for: Notification.Identify("DidTapOnNotification"))) { notification in
if let userInfo = notification.userInfo{
viewModel.onTapReceivedNotificationHanlde(notification: userInfo)
}
}
.onReceive(NotificationCenter.default.writer(for: .appOpenedOnNotificationClick)) { notification in
if let userInfo = notification.userInfo{
viewModel.onTapReceivedNotificationHanlde(notification: userInfo)
}
viewModel.selectTabView(tab: Tab.HOME_VIEW)
}
}.environmentObject(viewModel)
.environmentObject(networkMonitor)
.navigationBarBackButtonHidden(true)
.navigationBarHidden(true)
}
@ViewBuilder
non-public func tabContentView() -> some View {
swap viewModel.selectedView {
case Tab.HOME_VIEW:
HomeView().environmentObject(viewModel)
default:
EmptyView()
}
}
}
Nevertheless, how can get app launched notification particulars in CustomTabBar View?
plase assist me..