Capacitor PushNotification Callback nor being called - capacitor

I'm using the push notification plugin on my application and the call back is not working PushNotifications.addListener( 'registration', ( token: PushNotificationToken ) => ...
Has anyone experienced something like this before?

Found the issue... there is a block of code that needs to be changed on the AppDelegate.swift and Podfile files.
AppDelegate.swift:
Change this:
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
NotificationCenter.default.post(name: Notification.Name(CAPNotifications.DidRegisterForRemoteNotificationsWithDeviceToken.name()), object: deviceToken)
}
for this:
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken
InstanceID.instanceID().instanceID { (result, error) in
if let error = error {
NotificationCenter.default.post(name: Notification.Name(CAPNotifications.DidFailToRegisterForRemoteNotificationsWithError.name()), object: error)
} else if let result = result {
NotificationCenter.default.post(name: Notification.Name(CAPNotifications.DidRegisterForRemoteNotificationsWithDeviceToken.name()), object: result.token)
}
}
}
PodFile:
Add under capacitor_pods:
pod 'Firebase/Messaging'

Related

Function crash in viewController

Hi developers I have this issue with Adobe Audience Manager POD
in my VC I have this
override func viewDidLoad() {
super.viewDidLoad()
configureUI()
ACPAudience.signal(withData: ["ViewedScreen": "ButtonClicked"]) { (response, error) in
if let error = error {
print(error)
} else {
print(response as Any)
}
}
}
every time I run it it crash with this:
dynamic_cast error 2: One or more of the following type_info's has hidden visibility or is defined in more than one translation unit. They should all have public visibility. N20AdobeMarketingMobile6ModuleE, N20AdobeMarketingMobile13ConfigurationE, N20AdobeMarketingMobile22ModuleDetailsInterfaceE.
implementation is correct as far I know
the solution is creating a NSNotification in the app delegate works no crashes or anything weird
code in app delegate is pretty much the same
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
NotificationCenter.default.addObserver(self, selector: #selector(getProfileACP2), name: NSNotification.Name(rawValue: "getProfileACP"), object: nil)
}
#objc private func getProfileACP2(){
ACPAudience.signal(withData: ["ViewedScreen": "ButtonClicked"]) { (response, error) in
if let error = error {
print(error)
} else {
print(response as Any)
}
}
}
then on the VC works like this
override func viewDidLoad() {
super.viewDidLoad()
print("getProfileACP")
print("----------------------------------------------")
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "getProfileACP"), object: nil)
print("----------------------------------------------")
print("getProfileACP")
}
after calling it seems like it takes some time after giving the response is not immediately

error: no such module 'FirebaseInstanceID' on ios

i am trying to run my code and keep telling me
error: no such module 'FirebaseInstanceID'
import FirebaseInstanceID
my code in AppDelegate.swift
import UIKit
import Flutter
import Firebase
import FirebaseAuth
import UserNotifications
import FirebaseInstanceID
#UIApplicationMain
#objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
override func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let firebaseAuth = Auth.auth()
firebaseAuth.setAPNSToken(deviceToken, type: AuthAPNSTokenType.unknown)
}
override func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
let firebaseAuth = Auth.auth()
if (firebaseAuth.canHandleNotification(userInfo)){
print(userInfo)
return
}
}
}
i think is missing in my pod, i did not no how to install it to my pod file.
FirebaseInstanceID is deprecated now, that is why this error is coming. Remove import FirebaseInstanceID from your AppDelegate.swift and instead add import FirebaseMessaging because FCM token is now provided directly on the Messaging instance. Now replace the code of InstanceID in didRegisterForRemoteNotificationsWithDeviceToken with this
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken
Messaging.messaging().token { (token, error) in
if let error = error {
print("Error fetching remote instance ID: \(error.localizedDescription)")
} else if let token = token {
print("Token is \(token)")
}
}
}
updated 19/07/2021
remove the FirebaseInstanceID and install Firebase Messaging latest version..
i later solve the issue by installing Firebase Messaging Plugin
Firebase messaging Plugin

NotificationCenter Post request never gets received

I am trying to post a message to an observable in my app but for some reason it doesn't work at all and the only thing I can attribute it to is installing https://bugfender.com/ can anyone tell me what is wrong with this code or how to track down the root cause as there is no error messages at all
Both snippets have been moved to AppDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
NotificationCenter.default.post(name: Notification.Name("TestPost"), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(onDidReceiveData(_:)), name: Notification.Name("TestPost"), object: nil)
return true
}
and the receiver functions is
#objc func onDidReceiveData(_ notification:Notification) {
// Do something now
print("XXXXXX received")
}
===== Updating with my actual code for custom capacitor plugin =======
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
... code for receiving notification starts here
NotificationCenter.default.post(name: Notification.Name("TestPost"), object: nil)
... code for receiving notification ends here
return true
}
import Foundation
import Capacitor
#objc(myHelpers)
public class myHelpers: CAPPlugin {
public override func load() {
print("PluginLoaded")
let nc = NotificationCenter.default
nc.addObserver(self, selector: #selector(handleSignal), name: Notification.Name("TestPost"), object: nil)
}
#objc func handleSignal()
{
print("XX WE RECEIVED AN EVENT AT handleSignal")
notifyListeners(
"myPluginEvent",
data: [:],
retainUntilConsumed: true
)
}
}
You need to register your observer before you post the notification.

When execute "AudioKit.start()", an error occorent

I have push the recordViewController that record audio by AudioKit from homeViewController which is an emptyViewController,and I have write code in APPDelegate.swift like this:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:[UIApplicationLaunchOptionsKey: Any]? = nil) -> Bool {
do {
try AVAudioSession.sharedInstance().setActive(true)
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
}catch{
}
UIApplication.shared.beginReceivingRemoteControlEvents()
return true
}
then when it push to recordViewController,show this message
'[AVAudioEngineGraph.mm:1266:Initialize: (err =
AUGraphParser::InitializeActiveNodesInOutputChain(ThisGraph,
kOutputChainOptimizedTraversal, *GetOutputNode(),
isOutputChainActive)): error -10875,
RecorderViewController.swift:stetupConfigs():89:AudioKit did not
start!'

Push remote notification

I need create in my app a code that receive a remote information and push it to user when app is on background, I read on web that I need to use didReceiveRemoteNotification on appDelegate, to use remote push notication. I read something about and I need keys and certificates, I do not understand how to use didReceiveRemoteNotification
I need to learn about to push remote notification and how to use. I would like a tutorial or example how create it using swift 2.3.
I used this link and I found it the most helpful
http://www.appcoda.com/push-notification-ios/
I used this app for testing
https://itunes.apple.com/us/app/easy-apns-provider-push-notification/id989622350?mt=12
This is the code I have in my AppDelegate
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
registerForPushNotifications(application)
print(UIDevice.currentDevice().identifierForVendor!.UUIDString)
return true
}
func registerForPushNotifications(application: UIApplication) {
let notificationSettings = UIUserNotificationSettings(
forTypes: [.Badge, .Sound, .Alert], categories: nil)
application.registerUserNotificationSettings(notificationSettings)
}
func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
if notificationSettings.types != .None {
application.registerForRemoteNotifications()
}
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
let tokenChars = UnsafePointer<CChar>(deviceToken.bytes)
var tokenString = ""
for i in 0..<deviceToken.length {
tokenString += String(format: "%02.2hhx", arguments: [tokenChars[i]])
}
print("Device Token:", tokenString)
}
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
print("Failed to register:", error)
}