1.0.1 • Published 11 months ago

@umun-tech/notification v1.0.1

Weekly downloads
-
License
-
Repository
-
Last release
11 months ago

#Firebase Clouse Messaging

##Successful attempt using capacitor 2.4.5 & push-notifications 0.3.10

followed this blog post https://devdactic.com/push-notifications-ionic-capacitor/

and video https://youtu.be/YUr8pw0nO7Y

Worked flawlessly for ios and android

##Unsuccessfull try with capacitor - 3.0.0 & push-notifications 1.0.0

Follow these:

https://capacitorjs.com/docs/guides/push-notifications-firebase

https://firebase.google.com/docs/cloud-messaging/ios/client#method_swizzling_in_firebase_messaging

All capacitor dependencies should be cli and core

above ^3.0.0

Only capacitory/push-notifcations is ^1.0.0

These should be installed in both root folder and the library.

capacitor native-bridge.js bug -> https://capacitorjs.com/docs/v2/android/troubleshooting

  1. Install the push notification dependency npm install @capacitor/push-notifications

  2. Install capacitor if not already installed

    capacitor.config.json will be there is capacitor is installed

  3. Add this to the capacitor.config.json

"PushNotifications": { "presentationOptions": ["badge", "sound", "alert"] }

in the "plugins" object

  1. Register a new Identifier at developer.apple.com

Enable push notifications in that

  1. Add a new certificate

Services> Apple Push Notification Service SSL

  1. Download this certificate. double click the .cert file. It will add the cert to keychain

To create a certificate open keychain, create a request

  1. Follow https://capacitorjs.com/docs/guides/push-notifications-firebase to update the pod file

  2. From xcode, add google plist to the app folder

In This app delegate file in app/app

  • asks for permission of notifications
  • notifications are receieved only when app is not open
  • due the the last method- fcm token is visible in xcode terminal

problem is that somehow capacitor plugin is not engaging and error is thrown when a function of PushNotifications is called

import UIKit import Capacitor import Firebase

@UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    FirebaseApp.configure()
    
    // [START set_messaging_delegate]
    Messaging.messaging().delegate = self
    // [END set_messaging_delegate]
    // Register for remote notifications. This shows a permission dialog on first run, to
    // show the dialog at a more appropriate time move this registration accordingly.
    // [START register_for_notifications]
    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 applicationWillResignActive(_ application: UIApplication) {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}

func applicationDidEnterBackground(_ application: UIApplication) {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(_ application: UIApplication) {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(_ application: UIApplication) {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(_ application: UIApplication) {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
    // Called when the app was launched with a url. Feel free to add additional processing here,
    // but if you want the App API to support tracking app url opens, make sure to keep this call
    return ApplicationDelegateProxy.shared.application(app, open: url, options: options)
}

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
    // Called when the app was launched with an activity, including Universal Links.
    // Feel free to add additional processing here, but if you want the App API to support
    // tracking app url opens, make sure to keep this call
    return ApplicationDelegateProxy.shared.application(application, continue: userActivity, restorationHandler: restorationHandler)
}


override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesBegan(touches, with: event)

    let statusBarRect = UIApplication.shared.statusBarFrame
    guard let touchPoint = event?.allTouches?.first?.location(in: self.window) else { return }

    if statusBarRect.contains(touchPoint) {
        NotificationCenter.default.post(name: .capacitorStatusBarTapped, object: nil)
    }
}

}

extension AppDelegate : MessagingDelegate { // START refresh_token func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) { print("Firebase registration token: (String(describing: fcmToken))")

let dataDict:[String: String] = ["token": fcmToken ?? ""]

// NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict) // TODO: If necessary send token to application server. // Note: This callback is fired at each app startup and whenever a new token is generated.

NotificationCenter.default.post(name: .capacitorDidRegisterForRemoteNotifications, object: fcmToken)

} // END refresh_token

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
  NotificationCenter.default.post(name: .capacitorDidFailToRegisterForRemoteNotifications, object: error)
}

}