Google Nearby API on iOS background scanning - ios

I've got the Nearby API set up in my Swift app and I can receive messages when the app is in the foreground. Following the instructions in the docs
I try to include params.allowInBackground = true in the appropriate place but I get an error:
Value of type 'GNSBeaconStrategyParams' has no member 'allowInBackground'
So, I can't do that and my GNSSubscription object looks like this:
subscription = messageManager.subscriptionWithMessageFoundHandler(
messageFoundHandler, messageLostHandler: messageLostHandler,
paramsBlock: { (params: GNSSubscriptionParams!) in
params.deviceTypesToDiscover = .BLEBeacon
params.permissionRequestHandler = { (permissionHandler: GNSPermissionHandler!) in
// TODO: Show custom dialog here, and call permissionHandler after it is dismissed
// show the dialogue
}
params.beaconStrategy = GNSBeaconStrategy(paramsBlock: { (params: GNSBeaconStrategyParams!) in
params.includeIBeacons = true
//params.allowInBackground = true //*** THIS DOESN'T WORK ***
})
})
My messageHandlers look like this:
messageFoundHandler = {[unowned self](message: GNSMessage!) -> Void in
print("Found handler:", message.type, "->", String(data: message.content!, encoding:NSUTF8StringEncoding)!)
if UIApplication.sharedApplication().applicationState != .Active {
let localNotification = UILocalNotification()
localNotification.alertBody = "Message received" + String(data: message.content!, encoding:NSUTF8StringEncoding)!
UIApplication.sharedApplication().presentLocalNotificationNow(localNotification)
}
}
messageLostHandler = {(message: GNSMessage!) -> Void in
print("Lost Handler:", message.type, "->", String(data: message.content, encoding:NSUTF8StringEncoding)!)
}
With this set up and an Eddystone beacon in range I now get notifications in the background! Since this is what I want I should be happy. However, if I leave the device connected to xcode with the app in the background I start to see a stream of messages like this (seems to be roughly every 5 seconds):
2016-07-23 19:35:08.243 Hoc[1269:622746] Can't endBackgroundTask: no background task exists with identifier 2f3, or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug.
I'm using v0.10.0 of NearbyMessages. If anyone can point me in the right direction to get background scanning working reliably on iOS that would be great.

This was caused by an issue with Cocoapods. Even though I used $ pod update and similar approaches to remove/re-added the NearbyMessages cocoaPod from my project, for some reason I couldn't get the latest version (1.0.1) of the Nearby SDK installed. The solution was to manually download the spec from Github and overwrite the files in ~/.cocoapods/repos/master.
Then to make sure I had the latest version installed, I changed my podfile to include pod 'NearbyMessages', '~> 1.0.1' which worked.
Now I can set the appropriate params on the GNSBeaconStrategy object and background scanning for Eddystone beacons works in the background on iOS. :-)
I hope this helps.

Related

IOS 13 doesn't play a notification sound using FirebasePushNotificationPlugin

I use Firebase to push notifications to the users at a certain time. They receive the notification but no alert sound is played. In the settings, the allow sound/notifications are turned on and other IOS13 and other apps play sound.
Version Number of FirebasePushNotificationPlugin Plugin: 3.3.10
Device Tested On: iphone X, OS: 13.4.1
Simulator Tested On: N/A (simulators don't receive notifications)
Version of VS: VS for Mac Community, 8.6.6 (build 11)
Version of Xamarin: Xamarin.IOS 13.18.2.1, Xamarin.Forms v4.6.0.847
AppDelegate.cs:
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
bool fbaseStarted = false;
try
{
// This method does all the UNUserNotificationCenter.Current.RequestAuthorization() code so we don't have to.
FirebasePushNotificationManager.Initialize(options, true);
fbaseStarted = true;
}
catch
{ }
LoadApplication(new App());
if (!fbaseStarted)
{
try
{
FirebasePushNotificationManager.Initialize(options, true);
}
catch { }
}
FirebasePushNotificationManager.CurrentNotificationPresentationOption = UNNotificationPresentationOptions.Badge | UNNotificationPresentationOptions.Alert | UNNotificationPresentationOptions.Sound;
}
Within one of the pages of my code, I subscribe a list of tags (please note that I unsubscribe because the first time the code runs it fails silently if the notifications aren't approved - resulting in the model thinking the notifications was subscribed when it wasn't):
CrossFirebasePushNotification.Current.UnsubscribeAll();
CrossFirebasePushNotification.Current.Subscribe(Constants.NotificationTagsArray);
I keep coming across payload json solutions but unless I am wrong, I don't think that applies to me as I am using Xamarin and the FirebasePushNotificationPlugin. Is there any additional permissions that were added in ios 13 for playing notifications with sound that I have missed?
I have also posted here: https://github.com/CrossGeeks/FirebasePushNotificationPlugin/issues/348 but nobody has been able to assist me yet.
Thanks
The issue actually lies with the sending of the notifications nothing to do with the Xamarin App. The issue resided in the services that sends the notifications to firebase (to then be sent out to the phones).
In the service we were sending a FirebaseNet.Messaging.Message() to the phones:
Message FireBasemessage = new Message()
{
To = "/topics/" + PushNote.Tag,
TimeToLive = 86400,
Priority = MessagePriority.high,
ContentAvailable = true,
Notification = new AndroidNotification()
{
Tag = "/topics/" + PushNote.Tag,
Body = enhancedMessage,
Title = xtitle,
}
,
Data = new Dictionary<string, string>
{
{ "param", PushNote.Tag },
{ "text", enhancedMessage}
}
};
In the AndroidNotification() object required Sound = "default" to be added for it to work. Please note that this works for both Android and IOS notifications despite the fact it is an AndroidNotification object.

Force user to update the app programmatically in iOS

In my iOS app I have enabled force app update feature. It is like this.
If there is a critical bug fix. In the server we are setting the new release version. And in splash screen I am checking the current app version and if its lower than the service version, shows a message to update the app.
I have put 2 buttons "Update now", "Update later"
I have 2 questions
If I click now. App should open my app in the appstore with the button UPDATE. Currently I use the link "http://appstore.com/mycompanynamepvtltd"
This opens list of my company apps but it has the button OPEN, not the UPDATE even there is a new update for my app. whats the url to go for update page?
If he click the button "Update Later" is it ok to close the app programmatically? Does this cause to reject my app in the appstore?
Please help me for these 2 questions
Point 2 : You should only allow force update as an option if you don't want user to update later. Closing the app programmatically is not the right option.
Point 1 : You can use a good library available for this purpose.
Usage in Swift:
Library
func applicationDidBecomeActive(application: UIApplication) {
/* Perform daily (.daily) or weekly (.weekly) checks for new version of your app.
Useful if user returns to your app from the background after extended period of time.
Place in applicationDidBecomeActive(_:)*/
Siren.shared.checkVersion(checkType: .daily)
}
Usage in Objective-C: Library
-(void)applicationDidBecomeActive:(UIApplication *)application {
// Perform daily check for new version of your app
[[Harpy sharedInstance] checkVersionDaily];
}
How it works : It used lookup api which returns app details like link including version and compares it.
For an example, look up Yelp Software application by iTunes ID by calling https://itunes.apple.com/lookup?id=284910350
For more info, please visit link
Don't close the app programmatically. Apple can reject the app. Better approach will be do not allow user to use the app. Keep the update button. Either user will go to app store or close the app by himself.
According to Apple, your app should not terminate on its own. Since the user did not hit the Home button, any return to the Home screen gives the user the impression that your app crashed. This is confusing, non-standard behavior and should be avoided.
Please check this forum:
https://forums.developer.apple.com/thread/52767.
It is happening with lot of people. In my project I redirected the user to our website page of downloading app from app store. In that way if the user is not getting update button in app store, at least the user can use the website in safari for the time being.
To specifically answer your question:
Use this URL to directly open to your app in the app store:
https://apps.apple.com/app/id########## where ########## is your app's 10 digit numeric ID. You can find that ID in App Store Connect under the App Information section. It's called "Apple ID".
I actually have terminate functionality built into my app if it becomes so out of date that it can no longer act on the data it receives from the server (my app is an information app that requires connectivity to my web service). My app has not been rejected for having this functionality after a dozen updates over a couple years, although that function has never been invoked. I will be switching to a static message instead of terminating the app, just to be safe to avoid future updates from being rejected.
I have found that the review process is at least somewhat subjective, and different reviewers may focus on different things and reject over something that has previously been overlooked many times.
func appUpdateAvailable() -> (Bool,String?) {
guard let info = Bundle.main.infoDictionary,
let identifier = info["CFBundleIdentifier"] as? String else {
return (false,nil)
}
// let storeInfoURL: String = "http://itunes.apple.com/lookupbundleId=\(identifier)&country=IN"
let storeInfoURL:String = "https://itunes.apple.com/IN/lookup?
bundleId=\(identifier)"
var upgradeAvailable = false
var versionAvailable = ""
// Get the main bundle of the app so that we can determine the app's
version number
let bundle = Bundle.main
if let infoDictionary = bundle.infoDictionary {
// The URL for this app on the iTunes store uses the Apple ID
for the This never changes, so it is a constant
let urlOnAppStore = NSURL(string: storeInfoURL)
if let dataInJSON = NSData(contentsOf: urlOnAppStore! as URL) {
// Try to deserialize the JSON that we got
if let dict: NSDictionary = try?
JSONSerialization.jsonObject(with: dataInJSON as Data, options:
JSONSerialization.ReadingOptions.allowFragments) as! [String:
AnyObject] as NSDictionary? {
if let results:NSArray = dict["results"] as? NSArray {
if let version = (results[0] as! [String:Any]).
["version"] as? String {
// Get the version number of the current version
installed on device
if let currentVersion =
infoDictionary["CFBundleShortVersionString"] as? String {
// Check if they are the same. If not, an
upgrade is available.
print("\(version)")
if version != currentVersion {
upgradeAvailable = true
versionAvailable = version
}
}
}
}
}
}
}
return (upgradeAvailable,versionAvailable)
}
func checkAppVersion(controller: UIViewController){
let appVersion = ForceUpdateAppVersion.shared.appUpdateAvailable()
if appVersion.0 {
alertController(controller: controller, title: "New Update", message: "New version \(appVersion.1 ?? "") is available")
}
}
func alertController(controller:UIViewController,title: String,message: String){
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Update", style: .default, handler: { alert in
guard let url = URL(string: "itms-apps://itunes.apple.com/app/ewap/id1536714073") else { return }
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url)
}
}))
DispatchQueue.main.async {
controller.present(alertController, animated: true)
}
}
Use appgrades.io. Keep your app focus on delivering the business value and let 3rd party solution do their tricks. With appgrades, you can, once SDK integrated, create a custom view/alert to display for your old versions users asking them to update their apps. You can customize everything in the restriction view/alert to make it appear as part of your app.

addUIInterruptionMonitor(withDescription:handler:) not working on iOS 10 or 9

The following tests works fine on iOS 11. It dismisses the alert asking permissions to use the locations services and then zooms in in the map. On iOS 10 or 9, it does none of this and the test still succeeds
func testExample() {
let app = XCUIApplication()
var handled = false
var appeared = false
let token = addUIInterruptionMonitor(withDescription: "Location") { (alert) -> Bool in
appeared = true
let allow = alert.buttons["Allow"]
if allow.exists {
allow.tap()
handled = true
return true
}
return false
}
// Interruption won't happen without some kind of action.
app.tap()
removeUIInterruptionMonitor(token)
XCTAssertTrue(appeared && handled)
}
Does anyone have an idea why and/or a workaround?
Here's a project where you can reproduce the issue: https://github.com/TitouanVanBelle/Map
Update
Xcode 9.3 Beta's Changelogs show the following
XCTest UI interruption monitors now work correctly on devices and simulators running iOS 10. (33278282)
let springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard")
let allowBtn = springboard.buttons["Allow"]
if allowBtn.waitForExistence(timeout: 10) {
allowBtn.tap()
}
Update .exists to .waitForExistence(timeout: 10), detail please check comments.
I had this problem and River2202's solution worked for me.
Note that this is not a fix to get the UIInterruptionMonitor to work, but a different way of dismissing the alert. You may as well remove the addUIInterruptionMonitor setup. You'll need to have the springboard.buttons["Allow"].exists test anywhere the permission alert could appear. If possible, force it to appear at an early stage of the testing so you don't need to worry about it again later.
Happily the springboard.buttons["Allow"].exists code still works in iOS 11, so you can have a single code path and not have to do one thing for iOS 10 and another for iOS 11.
Incidentally, I logged the base issue (that addUIInterruptionMonitor is not working pre-iOS 11) as a bug with Apple. It has been closed as a duplicate now, so I guess they acknowledge that it is a bug.
I used the #River2202 solution and it works better than the interruption one.
If you decide to use that, I strongly suggest that you use a waiter function. I created this one in order to wait on any kind of XCUIElement to appear:
Try it!
// function to wait for an ui element to appear on screen, with a default wait time of 20 seconds
// XCTWaiter was introduced after Xcode 8.3, which is handling better the timewait, it's not failing the test. It uses an enum which returns: 'Waiters can be used with or without a delegate to respond to events such as completion, timeout, or invalid expectation fulfilment.'
#discardableResult
func uiElementExists(for element: XCUIElement, timeout: TimeInterval = 20) -> Bool {
let expectation = XCTNSPredicateExpectation(predicate: NSPredicate(format: "exists == true"), object: element)
let result = XCTWaiter().wait(for: [expectation], timeout: timeout)
guard result == .completed else {
return false
}
return true
}

swift nearby api doesn't find beacons

I want to use my swift ios app to scan for beacons using google nearby api (not iBeacon api)
I saw Google developer doc, and I took the git sample from the same site.
Here is my code
I have installed the app on a real ios device for the first time
but the found and lost handlers are never called.
I doubled checked the bundle id, the public ios API key (same google console project of the beacon attachment)
but it still doesn't work near a working and registered beacon.
I also have an android app which succeeds in scanning the same beacon.
What else can I check?
I'm missing the "Strategy" piece of code in my swift code.
How can I add this? Why is this missing in the github example?
GNSStrategy *beaconStrategy = [GNSStrategy
strategyWithParamsBlock:^(GNSStrategyParams *params) {
params.includeBLEBeacons = YES;
}];
GNSSubscriptionParams *beaconParams = [GNSSubscriptionParams
paramsWithMessageNamespace:#"com.mycompany.mybeaconservice"
type:#"mybeacontype"
strategy:beaconStrategy];
_beaconSubscription = [_messageManager subscriptionWithParams:beaconParams
messageFoundHandler:myMessageFoundHandler
messageLostHandler:myMessageLostHandler];
in my code:
func startScanning() {
if let messageMgr = self.messageMgr {
// Subscribe to messages from nearby devices and display them in the message view.
subscription = messageMgr.subscriptionWithMessageFoundHandler({[unowned self] (message: GNSMessage!) -> Void in
self.mainViewController.location_text.text = (String(data: message.content, encoding:NSUTF8StringEncoding))
self.mainViewController.startRefreshTimer()
},
messageLostHandler: {[unowned self](message: GNSMessage!) -> Void in
if (self.mainViewController.userState.enrollState == EnrollState.confirmPosition)
{
self.mainViewController.stopRefreshTimer()
self.mainViewController.enrollButtonManager.setSearchingForBeaconsBtn()
}
})
}
}
You'll need to add the GNSStrategy to your subscription, which lets you enable beacon scanning. Try this:
let params: GNSSubscriptionParams = GNSSubscriptionParams.init(strategy:
GNSStrategy.init(paramsBlock: { (params: GNSStrategyParams!) -> Void in
params.includeBLEBeacons = true;
}))
subscription = messageMgr.subscriptionWithParams(params,
messageFoundHandler:{[unowned self] (message: GNSMessage!) -> Void in
self.mainViewController.location_text.text = (String(data: message.content, encoding:NSUTF8StringEncoding))
self.mainViewController.startRefreshTimer()
},
messageLostHandler: {[unowned self](message: GNSMessage!) -> Void in
if (self.mainViewController.userState.enrollState == EnrollState.confirmPosition) {
self.mainViewController.stopRefreshTimer()
self.mainViewController.enrollButtonManager.setSearchingForBeaconsBtn()
}
})
Beacon scanning is off by default because iOS presents the user with the location permission dialog when beacon scanning is first turned on, and this is unacceptable for apps that use the Nearby library but don't scan for beacons.
Thanks for the feedback about the github example not showing how to scan for beacons. I'll see about adding it.

Alamofire request when app in background

Hi I want to fetch some data using Alamofire and a NSTimer
The app works fine on foreground, the timer is set to call the GetEstado function each 30 seconds but when the app enters background the timer is paused and if my server updates something it does not actually updates on the app
Here's my code
func GetEstado(){
Alamofire.request(.POST, "https://myurl/getestado.php", parameters: ["id": id])
.responseString { rta in
if let dataFromString = rta.result.value!.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) {
let json = JSON(data: dataFromString)
for (key,subJson):(String, JSON) in json {
if(key=="e"){
if(subJson.stringValue=="1"){
self.Timer.invalidate()
print("ACTUALIZACION")
let notification = UILocalNotification()
notification.fireDate = NSDate(timeIntervalSinceNow: 5)
notification.alertBody = "UPDATED"
notification.soundName = UILocalNotificationDefaultSoundName
UIApplication.sharedApplication().scheduleLocalNotification(notification)
self.performSegueWithIdentifier("UPDATED", sender: self)
}
else{
print("UPS")
}
}
}
}
}
}
Thanks
I think #Paulw11 answer your question in his comment, you can't use NSTimer in background, Apple has a known list of the task that can be produced in background, you can read more here.
But let think for a moment what you're trying to achieve, let suppose you can do it using NSTimer, then you have your app in background consuming memory and battery in your device constantly to try to get the updates from your server, this is not recommended at all.
It's for this that Apple/Google introduced Apple Push Notifications (Google: Android Push Notifications) to handle that the server notifies the app when it's in background or inactive regarding any task that is programmed in the server, lets say a new mail, a new message in the case of messaging apps, etc.
Then, the option you have is use the service proposed by Apple, you can read more in the following docs:
Notifications
Apple Push Notification Service
I hope this help you.

Resources