During the initial app installation everything works as exptected: application:didRegisterForRemoteNotificationsWithDeviceToken: is called after the prompt *"APP Would Like To Send You Push Notifications"* is accepted or rejected. But if the app is deleted, and reinstalled again I'm getting a weird behaviour: that delegate method is called BEFORE user had a chance to accept or reject the prompt, right after it appears...
In my app I need to perform some UI action after a user accepts or rejects that prompt, and I don't see any other option other than using the above delegate method (and it seems not to work after re-install)
Any help is greatly appreciated.
Related
I tested:
UIControl().sendAction(#selector(URLSessionTask.suspend), to: UIApplication.shared, for: nil)
which is for putting app on background and it works.
How do I put app back on foreground?
I tried:
UIControl().sendAction(#selector(URLSessionTask.resume), to: UIApplication.shared, for: nil)
But eventually it crashes...
Thank you
Update:
Since you've indicated that you're looking for any technical solution, even those not compatible with the App Store or Apple's terms, this should be possible using the Private API LSApplicationWorkspace: openApplicationWithBundleID. Try something like this:
Create a .h file and set up an interface to the LSApplicationWorkspace class and list the required method. You will need to #import "PrivateHeaders.h" in your bridging header.
//
// PrivateHeaders.h
//
#ifndef PrivateHeaders_h
#define PrivateHeaders_h
#interface LSApplicationWorkspace : NSObject
- (bool)openApplicationWithBundleID:(id)arg1;
#end
#endif /* PrivateHeaders_h */
You should then be able to call this function and pass in the Bundle Identifier of your app as an string.
//
// SomeClass.swift
//
import MobileCoreServices
let workspace = LSApplicationWorkspace()
/**
Launch an App given its bundle identifier
- parameter bundleIdentifier: The bundle identifier of the app to launch
- returns: True if app is launched, otherwise false
*/
func openApp(withBundleIdentifier bundleIdentifier: String) -> Bool {
// Call the Private API LSApplicationWorkspace method
return workspace.openApplication(withBundleID: bundleIdentifier)
}
Original:
What you are doing is likely a violation of the iOS Human Interface Guidelines (although the "Don’t Quit Programmatically" is no longer specifically defined), so as the comments have said, it is not suited to the App Store. Regardless, once your app is suspended in this way, I don't expect that there is a way to resume it programmatically, unless you can hook into a Background Operation to run URLSessionTask.resume, but I have not tested it and am unsure whether it can work.
Apps can be launched (and hence brought into the foreground) programmatically from another app or today extension by using a Custom URL Scheme, or via a Push Notification. It isn't possible to launch the app from the Background Operation via a URL Scheme, since it is part of the UIKit framework, which must be run in the main thread.
In summary, I think your best option is to try to use a Notification. This just means that the user will need to click on the notification to bring your app back into the foreground.
Closing/opening the app should be done explicitly by the user. Any other way of closing or opening the app is not supported by Apple and will be rejected when uploaded to app store. iOS Human Interface Guideline states:
Don’t Quit Programmatically
Never quit an iOS application
programmatically because people tend to interpret this as a crash.
However, if external circumstances prevent your application from
functioning as intended, you need to tell your users about the
situation and explain what they can do about it. Depending on how
severe the application malfunction is, you have two choices.
*Display
an attractive screen that describes the problem and suggests a
correction. A screen provides feedback that reassures usersthat
there’s nothing wrong with your application. It puts usersin control,
letting them decide whether they want to take corrective action and
continue using your application or press the Home button and open a
different application
*If only some of your application's features are
not working, display either a screen or an alert when people activate
the feature. Display the alert only when people try to accessthe
feature that isn’t functioning
Just as a follow up to Jordan's excellent answer I want to give an explanation for why your code works in the first place and why that alone will get your app rejected, even without any functionality to make it active again and bring it to the foreground.
As maddy pointed out in a comment, you're basically calling a method from UIApplication's private API. This works due to the Objective-C runtime's dynamic linking. You might wonder "But I am using Swift, what does that have to do with Objective-C?" The answer lies in #selector mechanism. A Selector is basically just a symbol that the Objective-C runtime looks up in a table to get a method it invokes (for you). This is why it's technically not correct to say you "call a method" when you do something like myObjectInstance.someMethod(). The correct way to phrase that would be to "send a message" to the object, because that's what is happening in the runtime. The target-action mechanism is build around that. The sendAction(_: Selector?, to: Any?) method does the same thing. So in effect your code does the following:
Get the symbol that corresponds to URLSessionTask's suspend() method.
Tell the shared instance of UIApplication to invoke the method that it has for that symbol.
Now usually that would result in a crash with the typical "unknown selector sent to instance..." error message. But here, by sure coincidence UIApplication also has a method for that instance (or rather, the runtime also has one of its methods listed in its table for that symbol). You kind of "found" a method that is not declared in its public header. You successfully circumvented a compile-time check for this and invoke a method that is part of a private API. This is explicitly forbidden in the Apple Developer Program License Agreement
Besides all that, I would strongly advise against trying to design an app that way in the first place. As maddy pointed out it's also likely considered to violate the HIGs. Even if you're not trying to do anything malicious and properly explain the feature in your app's description, that won't make Apple let it slide (I assume). Personally, as a user, I'd also find it annoying if the app did something the system already has a specific mechanic for in a different manner, at least in terms of app's coming to background and foreground.
I don't think it can be done without user interaction
The option is you can generate a push notification to tell the user to bring the application to foreground
When the operating system delivers push notification and the target application is not running in the foreground, it presents the notification.
If there is a notification alert and the user taps or clicks the action button (or moves the action slider), the application launches and calls a method to pass in the local-notification object or remote-notification payload.
I am trying to get actionable notifications working and I have gotten the actions to display when the notification is expanded, but I cannot get the delegate function to be called when I select an action. I am declaring self.notificationCenter.delegate = self in application(_:didFinishLaunchingWithOptions:), and, if I'm understanding correctly, when an action is selected it should call userNotificationCenter(_:didReceive:withCompletionHandler:), passing in the UNNotificationResponse object. But that method never gets triggered in my code. Any ideas why that may be happening?
The method you have mentioned above gets called as soon as you receive notification.
The method which is executed after clicking on action on notification is,
application:handleActionWithIdentifier:forRemoteNotification:completionHandler:
I figured out the issue. To handle notifications with content you have to add a service extension as a new target in the project. The last thing I ran was the service extension so the debugger wasn't stopping at breakpoints in the main app. I didn't know service extensions are essentially treated like completely separate apps. Since I was still in the process of working on the content of that method, what was in there was not working and it was hitting the breakpoints, so I thought it was not calling that function like it should.
I followed the hello world example on this page to set up lua-lgi and libnotify, successfully getting a notification that looks and acts the samea s if using os.execute("notify-send..."). Notify-send does not allow user actions, from what I've gathered, so I am attempting to directly use the libnotify library to display a notification with a button. When clicked in the notification, it should open a file. I am able to call the function described here with lua, using (building from the hello world example):
Hello:add_action("button", "Open", function(notification, action, user_data) os.open("gedit tmp") end)
which successfully displays a button with the label "Open" in the notification. However, the callback function is not called, so the file does not open. I also noticed when running the script, it actually finishes executing before the notification has fully appeared, so if the program is not running anymore when the button in the notification is clicked, then that's one reason why the callback isn't being called, if it's working correctly up until that point. This is my first experience with lua-lgi, so I'm not sure how these types of callback functions translate into lua, or if they're even supported, which is probably what I'm really trying to understand here. Any help is appreciated regarding this issue, or insight into an alternative to displaying a notification with a button and callback via other means.
I am working on an app using Monaca where I need to be able to push notifications to users of the app. I tried getting the monaca push back-end working, and that wouldn't work, the app didn't register. I then moved to using the standard cordova plugin, so I can push directly from my servers, without using monaca's back-end. when I call the init function, normally a register event will fire, this works fine when I build the app. However I very much want to get the custom debugger working, I am able to build the custom debugger, and I see the plugin properly installed. Also the init function gets called, as soon as it is called I get the standard IOS security alert to grant the app permission, however the register event is not fired, therefore I am unable to get the device ID to push to the custom debugger app. Has anyone been able to use push notifications with the custom debugger application?
Best,
Peter
(https://github.com/phonegap/phonegap-plugin-push)
P.S, i calls to set the badge count setApplicationIconBadgeNumber works fine in the debugger. I think this is something simple, maybe with the certs? I dont get any errors fired, no notifications no clues using the standard debugger, how can I hunt down what the issue is?
I can get it work using ngCordova, which is AngularJS integration.
http://ngcordova.com/docs/plugins/pushNotifications/
Then, inject it like
var app = ons.bootstrap('myApp', ['onsen', 'ngCordova']);
Get back to me if you need further clarification.
The second time I tried to authorize an app in Facebook it gives me back a window
You have already authorized app name. Press okay to continue.
The question is what is called back/delegate that is called after I press okay, because I somehow need a way to remove the login view controller after this
The callback is the same as when you wasn't authorized. Same process.
Back in your application you don't even have to (and even can't) make the distinction.
In your UIApplicationDelegate, be sure that the - (BOOL)application:handleOpenURL: method calls - (BOOL)handleOpenURL: on the Facebook object.