xcode 12 firebase notification when app in background how to do a reload if the app have a opened webview - xcode

I implemented firebase notification in Xcode 12 via cocoapod and basic step by step... notification ARE working all nice and good, even when app in background.
this tutorial : https://www.appcoda.com/firebase-push-notifications/
the only thing I need is this condition : when App in background and user hit the notification, it opens the APP, but I want the webview to reload.
i think it would be in this part of the code
// [START receive_message]
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
// TODO: Handle data of notification
// With swizzling disabled you must let Messaging know about the message, for Analytics
// Messaging.messaging().appDidReceiveMessage(userInfo)
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
// Print full message.
print(userInfo)
completionHandler(UIBackgroundFetchResult.newData)
}
but what code and I add to reload the view ?

// [START receive_message]
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
// TODO: Handle data of notification
// With swizzling disabled you must let Messaging know about the message, for Analytics
// Messaging.messaging().appDidReceiveMessage(userInfo)
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: (messageID)")
}
// Print full message.
print(userInfo)
completionHandler(UIBackgroundFetchResult.newData)
}

Related

IQKeyboardManager unexpected behavior

i have a simple chat view that contain table view, text field and a send button
i'm using IQKeyboardManager to handle keyboard appearance but the unexpected behavior is that when i click on send button the keyboard disappear that behavior don't happen on chat apps like whatsApp the keyboard remain appear'
how to handle this behavior to be like whatsApp
Update : here is the send button code
#IBAction func sendPressed(_ sender: AnyObject) {
//TODO: Send the message to Firebase and save it in our database
if (messageTextfield.text?.isEmpty)!{
showAlert(alertTitle: "", alertMessage: "can't send empty Message", actionTiltle: "Ok")
}
else{
messageTextfield.isEnabled=false
sendButton.isEnabled=false
let messageDB=Database.database().reference().child("Messages")
let dictionary:[String:String]=["Sender":(Auth.auth().currentUser?.email)!,"MessageBody":messageTextfield.text!]
messageDB.childByAutoId().setValue(dictionary)
messageTextfield.text=""
messageTextfield.isEnabled=true
sendButton.isEnabled=true
}
}
The issue is with isEnable property used isUserInteration property instead of that
#IBAction func sendPressed(_ sender: AnyObject) {
//TODO: Send the message to Firebase and save it in our database
if (messageTextfield.text?.isEmpty)!{
showAlert(alertTitle: "", alertMessage: "can't send empty Message", actionTiltle: "Ok")
}
else{
messageTextfield.isUserInteractionEnabled=false
sendButton.isUserInteractionEnabled=false
let messageDB=Database.database().reference().child("Messages")
let dictionary:[String:String]=["Sender":(Auth.auth().currentUser?.email)!,"MessageBody":messageTextfield.text!]
messageDB.childByAutoId().setValue(dictionary)
messageTextfield.text=""
messageTextfield.isUserInteractionEnabled=true
sendButton.isUserInteractionEnabled=true
}
}

WKWebview: Handle the button click event from app

Can I handle the button click event of WKWebView from my app.
Simply I have opened a facebook login page in my WKWebView. Is it possible to handle the event of that login button from my app?
You can use the navigationDelegate of the WKWebView to listen to that action. The delegate method you should implement is:
Swift
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Swift.Void)
{
let request = navigationAction.request; // Inspect this request to see if it's from Facebook
let policy = WKNavigationActionPolicyAllow or WKNavigationActionPolicyCancel depending if you want the request to continue firing or not
decisionHandler( policy );
}
Objective-C
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
NSURLRequest *request = navigationAction.request; // Inspect this request to see if it's from Facebook
WKNavigationActionPolicy policy = WKNavigationActionPolicyAllow or WKNavigationActionPolicyCancel depending if you want the request to continue firing or not
decisionHandler( policy );
}

Add buttons to dynamic notification interface

I'm creating a custom dynamic notification to show the user. Once i receive this notification at didReceiveNotification function at NotificationController I set the interface outlets with the right data. My problem is that i did not realize how can i add a custom button above the default dismiss button, since the notification storyboard doesnt allow buttons insertion and Apple Documentation says that
Do not include buttons, switches, or other interactive controls.
But i saw a lot of watch applications that have their custom actions, as Messages and Facebook Messenger. There is any way to add custom actions to the dynamic interface at watchOS?
You simply cannot add buttons to Dynamic notification interfaces. If you try to do this, you will receive the error
Illegal Configuration: Buttons are not supported in Notification interfaces.
However, you can add system buttons to your notifications other than the Dismiss button. When setting up the categories for the notification center, you can specify custom UNNotificationActions to be added to your notification category.
var categories = Set<UNNotificationCategory>()
let myCategory = UNNotificationCategory(identifier: "MyCategory", actions: [/*your custom actions go here*/], intentIdentifiers: [], options: []) //set up the actions here
categories.insert(myCategory)
center.setNotificationCategories(categories)
Then you can handle the user interactions with these actions (which as displayed as normal buttons on your dynamic notification interface) in your the UNUserNotificationCenterDelegate method, userNotificationCenter(_:didReceive:withCompletionHandler:) like this:
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
switch response.actionIdentifier {
case "Ok":
print("Ok action tapped")
case "Dismiss":
print("Dismiss action tapped")
default:
break
}
completionHandler()
}

iOS 10 push notification when app terminated?

I'm face the problem after upgraded to iOS 10 about push notification (I am using Swift3).
In normal case when application open or application still in background everything work as well (can receive push notification and update data as my logic).
But when application is terminated i can't handle push notification when application become active.
Here is my test case.
Edit Scheme to Wait for executable to be launched.
Double press home button and swipe application up.
Run Xcode wait until "Wait for application to launch" shown.
Test send push notification from server.
Device received push notification.
Start application from application icon.
After that application start and didFinishLaunchingWithOptions being called but launchOptions aways null so i can't handle push notification (But if i open application from notification in notification center or popup notification launchOptions is not null)
Does anybody has any idea to check this problem ?
Thank you in advance.
You need to open the application by tapping the push notification in the notification tray.
When you launch the application from the icon, launchOptions will be nil. Launching from the push notification will provide you launchOptions.
https://developer.apple.com/reference/uikit/uiapplicationdelegate/1622921-application
Try this:
Add delegate on AppDelegate.
UNUserNotificationCenterDelegate
And..
//Called when a notification is delivered to a foreground app.
#available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
print("User Info = ",notification.request.content.userInfo)
completionHandler([.alert, .badge, .sound])
}
//Called to let your app know which action was selected by the user for a given notification.
#available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
print("ActionIdentifier = ",response.actionIdentifier)
print("User Info = ",response.notification.request.content.userInfo)
completionHandler()
}

How to get resolution change event in swift?

I try to make an app, and now i shoud make some changes when screen resolution will change, but i coudn't find how to intercept this event.
Do you have any ideea how can i take that event?
The NSApplicationDidChangeScreenParametersNotification is posted when the configuration of the displays attached to the computer is changed, so
you can register for that notification, e.g. with
NSNotificationCenter.defaultCenter().addObserverForName(NSApplicationDidChangeScreenParametersNotification,
object: NSApplication.sharedApplication(),
queue: NSOperationQueue.mainQueue()) {
notification -> Void in
println("screen parameters changed")
}
Note that there can be various reasons why this notification is
fired, e.g. a change in the dock size (as observed in Cocoa Dock fires NSApplicationDidChangeScreenParametersNotification), so you have to
"remember" the old resolution and compare it with the new resolution.
Swift 4:
The didChangeScreenParametersNotification is posted when the configuration of the displays attached to the computer is changed.
Inside the func applicationDidFinishLaunching() in AppDelegate class or func viewDidLoad() in ViewController class, insert the following code:
NotificationCenter.default.addObserver(forName: NSApplication.didChangeScreenParametersNotification,
object: NSApplication.shared,
queue: OperationQueue.main) {
notification -> Void in
print("screen parameters changed")}
I personally, used it to center the position of my application when switching between the Mac and the external screen.
Here is the updated Swift 3 code:
NotificationCenter.default.addObserver(forName: NSNotification.Name.NSApplicationDidChangeScreenParameters,
object: NSApplication.shared(),
queue: OperationQueue.main) {
notification -> Void in
print("screen parameters changed")
}
Code for Swift 5+
NotificationCenter.default.addObserver(
forName: NSNotification.Name(rawValue: "NSApplicationDidChangeScreenParametersNotification"),
object: NSApplication.shared,
queue: .main) { notification in
self.adjustUIIfNeeded()
}

Resources