Urban Airship receivedBackgroundNotification never called - iphone

After reading all the guides, and after checking hundred of articles on the internet, I'm quite sure that the method receivedBackgroundNotification is never called.
Everything works perfect, but when the app is in background, a notification is shown and this method never is called. Seems to be impossible to get it working.
Assuming all the normal operations and the basic configuration is well done and is working, what can I do to intercept and manage background push notifications with this library?
I will appreciate a lot any help.

Make sure you have the following configured:
Remote notifications background mode enabled in the target's capabilities
Background app refreshed is enabled on your test device
Assuming you are trying to use the UAPushNotificationDelegate, make sure you either have automatic setup enabled or you are forwarding all the proper methods to UA SDK.
Apple will only wake up your application if you send the push notification with content-available=1 in the payload. The option is exposed in the composer as "background processing" or you can set it in the iOS overrides when using the push api.

In Urban Airship iOS SDK v.13.4.0 if func receivedBackgroundNotification(_ notificationContent: UANotificationContent, completionHandler: #escaping (UIBackgroundFetchResult) -> Void) is not called you can handle notifications in func receivedNotificationResponse(_ notificationResponse: UANotificationResponse, completionHandler: #escaping () -> Void) if you display notifications as alerts. Remember that you should only handle the notifications with actionIdentifier == UNNotificationDefaultActionIdentifier (the user opened the app from the notification interface).
This is an example of UAPushNotificationDelegate implementation:
extension MyPushNotificationDelegate: UAPushNotificationDelegate {
public func receivedForegroundNotification(_ notificationContent: UANotificationContent, completionHandler: #escaping () -> Void) {
completionHandler()
}
public func receivedBackgroundNotification(_ notificationContent: UANotificationContent, completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
completionHandler(.newData)
}
public func receivedNotificationResponse(_ notificationResponse: UANotificationResponse, completionHandler: #escaping () -> Void) {
guard notificationResponse.actionIdentifier == UNNotificationDefaultActionIdentifier else {
completionHandler()
return
}
someFunc() {
completionHandler()
}
}
public func extend(_ options: UNNotificationPresentationOptions = [], notification: UNNotification) -> UNNotificationPresentationOptions {
[.alert, .badge, .sound]
}
}

Related

watchOS: How to respond to app being opened via an intent?

I've successfully added a StartWorkoutIntent to my watch app for when the action button is pressed, however I need to be notified when the app was opened via the intent. Apple's docs say to implement this handle function below which I have in my Extension Delegate, however when you press the action button it is never called?
func handle(startWorkout intent: INStartWorkoutIntent,
completion: #escaping (INStartWorkoutIntentResponse) -> Void) {
print("WE are called!")
// Let the app start the workout.
let response = INStartWorkoutIntentResponse(code: .continueInApp,
userActivity: nil)
completion(response)
}
This never gets called either:
func handle(_ userActivity: NSUserActivity) {
if userActivity.activityType == "Start Workout" {
print("Handle Start Workout called")
}
}

Still receives INStartAudioCallIntent when drop support for iOS 12(INStartCallIntent)

I've meet a strange issue when dropping the support for iOS 12. When handle the user activity from AppDelegate continue userActivity, Although we drop INStartAudioCallIntent and INStartVideoCallIntent for it is deprecated in iOS 13, we still receive the above 2 intents from native contact card. But actually we want to handle INStartCallIntent instead.
Anyone knows why this happens for my debug version is 14.6, thanks.
Adding Intent as an app extension to handle INStartCallIntentHandling made my AppDelegate receive INStartCallIntent!
It can be implemented like this:
File > New -> Target -> Intent
Add INStartCallIntent to Supported Intents
Implement the IntentHandler (In the intent extension) like this:
class IntentHandler: INExtension, INStartCallIntentHandling {
override func handler(for intent: INIntent) -> Any {
return self
}
func handle(intent: INStartCallIntent, completion: #escaping (INStartCallIntentResponse) -> Void) {
let userActivity = NSUserActivity(activityType: NSStringFromClass(INStartCallIntent.self))
let response = INStartCallIntentResponse(code: .continueInApp, userActivity: userActivity)
completion(response)
}
}
The INStartCallIntent will now be called in the AppDelegate:
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: #escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
if let intent = userActivity.interaction?.intent {
if let intent = intent as? INStartCallIntent {
// The correct INStartCallIntent
}
}
}

LocalNotification alert interference

I have enabled UserNotifications in my app and it all works great, apart from a bug in the very beginning (First install). Local Notification requires to ask user for permission to send notifications and it comes as an alert on first install, where user chooses his/hers options ("Allow", "Don't Allow"). The problem is that this notification request is called in "applicationDidFinishLaunchingWithOptions" method in AppDelegate and it gets cut off by another alert, which is my LocalAuthorization(TouchID) alert initiated in viewDidLoad. Is there a way to put all those alerts in some kind of a queue, so they are fired one after another and not over each other? Or, to somehow tell viewDidLoad alert to wait for AppDelegate alert to finish showing? Any input is welcome. Thanks.
extension ViewController: UNUserNotificationCenterDelegate {
//for displaying notification when app is in foreground
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
//If you don't want to show notification when app is open, do something here else and make a return here.
//Even you you don't implement this delegate method, you will not see the notification on the specified controller. So, you have to implement this delegate and make sure the below line execute. i.e. completionHandler.
completionHandler([.alert, .badge, .sound])
}
// For handling tap and user actions
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
switch response.actionIdentifier {
case "action1":
print("Action First Tapped")//here you can your alert
case "action2":
print("Action Second Tapped")//here you can your alert
default:
break
}
completionHandler()
}
}
Move UNUserNotification authorization request from AppDelegate to viewDidLoad and call other alerts in completion block.

Sirikit and parent app communication in ios 10 : Handoff

I am using Sirikit to integrate with my payment domain app where I need to interact with the app. I read Apple documentation, they asked to use common frameworks.
Is it possible to use handoff? if yes then how?
How can I call the other viewController which is in parent app from sirikit?
I will really appreciate for any help. Thanks
Every user activity object needs a type and we need to define those types in the
info.plist of the application
For Eg.
let userActivity = NSUserActivity(activityType: "uaType")
userActivity.title = "uaTitle"
userActivity.userInfo = ["uaKey": "uaValue"]
You can now access this activity object in AppDelegate as follows
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: #escaping ([Any]?) -> Void) -> Bool {
switch userActivity.activityType {
case "uaType":
//Write your logic to access uaKey & uaValue here
return true
default: return false
}
}
Check SiriKit Programming Guide,
To use handoff, You can create intent response object with NSUserActivity object. While creating NSUserActivity object assign userInfo property to appropriate object that you want,
let userActivity = NSUserActivity()
userActivity.userInfo = ["myKey": myAnyObject]
You will get this NSUserActivity object in parent application AppDelegate,
optional public func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: ([AnyObject]?) -> Swift.Void) -> Bool
Here you can get userInfo object which you can compare as per your requirement and call appropriate viewController.

watchOS2 app and iPhone app communication

In the watchOS1, we had a method “openParentApplication”. This method communicated with the phone application even when it wasn’t running in foreground or background and fetched a reply immediately. I need something similar for watchOS2. I want my watch application to communicate immediately with the phone app even if my iPhone application is not running. Methods like updateApplicationContext:error:, sendMessage:replyHandler:errorHandler: and transferUserInfo: are not helpful in this scenario.
Please can someone suggest me a better approach to achieve this?
Actually sendMessage:replyHandler:errorHandler: is doing exactly what you are asking for. As long as your watch is connected to your phone it immediately gets a response to the message. This is working when the app is in the foreground, in the background or not running at all.
Here is how you set it up:
In the WatchExtension:
Setup the session. Typically in your ExtensionDelegate:
func applicationDidFinishLaunching() {
if WCSession.isSupported() {
let session = WCSession.defaultSession()
session.delegate = self
session.activateSession()
}
}
And then send the message when you need something from the app:
if WCSession.defaultSession().reachable {
let messageDict = ["message": "hello iPhone!"]
WCSession.defaultSession().sendMessage(messageDict, replyHandler: { (replyDict) -> Void in
print(replyDict)
}, errorHandler: { (error) -> Void in
print(error)
}
}
In the iPhone App:
Same session setup, but this time also set the delegate:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
...
if WCSession.isSupported() {
let session = WCSession.defaultSession()
session.delegate = self
session.activateSession()
}
}
And then implement the delegate method to send the reply to the watch:
func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) {
replyHandler(["message": "Hello Watch!"])
}
This works whenever there is a connection between the Watch and the iPhone. If the app is not running, the system starts it in the background. So, basically it just works like openParentApplication(_:reply:)