According to the official tutorial, the iOS application has several main states, such as foreground, background, and suspended state. An app in use whose view page is at the top of the screen is the foreground state.Then, when the home button is pressed or other applications are used, the APP is in the background state. After that, depending on the mobile phone system resources, the suspended APP may be killed at any time. This is the information that can be found online.
Next was the problem I encountered. After being in the background for a long time, I entered the app I developed again and found that I went directly to the last used view page(The picture was attached below. When using the app in the foreground, the username of the currently logged in user was displayed under the avatar. When returning from the background to the foreground, the username under the avatar became empty). This means that the APP has not been killed, but the data on the page is lost, and some of the data in the memory is partially lost too. It feels that the memory related to the app is partially recovered by the system.This makes me very confused.
I did't do any work about the state preservation and restoration process. I did't need to remember the status information of the previous use of the app when I relaunch the app. All I need is to ensure that the memory occupied by the app is fully restored when the app returns to the foreground from the background.
tips:The background in the expression of returning from the background to the foreground mentioned above, indicates the background or suspended state
view when app back to the foreground
here is my code in AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if(launchOptions != nil) {
/*remote message push related code*/
}
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window makeKeyAndVisible];
return YES;
}
#pragma mark - APNS
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
/**/
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler {
/*processing code when receiving a message push*/
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{
/*processing code when receiving a message push*/
}
#pragma mark - application state
- (void)applicationWillResignActive:(UIApplication *)application {
self.isRunningForeground = NO;
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
self.isRunningForeground = YES;
}
- (void)applicationWillTerminate:(UIApplication *)application {
}
My iphone is 6sp running iOS 11. I develop app with ARC.
Related
In IOS,
When app is removed from background and user obtain notification,by tap on that notification it is not triggering
function
$rootScope.push.on('notification', function (data) { });
If app running in background, then by tap of notification it is triggering this function.
Is there any other way to trigger this function when app is not running in background in ios.
Don't know in ionic, but in iOS
you will have to handle this in iOS as below
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSDictionary *pushInfo = launchOptions[ UIApplicationLaunchOptionsRemoteNotificationKey];
[self application:application didReceiveRemoteNotification:pushInfo];
}
AppDelegate.m
- (void)applicationWillResignActive:(UIApplication *)application {
[ScreenState setScreenState:FALSE];
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
[ScreenState setScreenState:FALSE];
}
- (void)applicationWillTerminate:(UIApplication *)application {
[ScreenState setScreenState:FALSE];
}
When I press the Home button or the Standby button, the above code gets triggered, enabling my hackish "ScreenState" static class to monitor whether the screen is active or not.
However, when I don't press anything, the screen shuts down after a few minutes and the above functions do not get called, enabling a very annoying bug to appear to my users...
Anyone has an idea?? This app needs to be in the App Store, so no hacks!
I have a problem during I developing application IOS and I used Parse Push Notification Framework for push remote notification. the problem is when the application is running and the same time the notification has been sent, the application is display modal alert box automatically. So, I don't want the modal alert display. I spent much times on it, and I do research on internet, read documents but no result found, I feel nobody knew about this. Please Help me!
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
UIImage *navBackgroundImage = [UIImage imageNamed:#"nav_bg_new"];
[[UINavigationBar appearance] setBackgroundImage:navBackgroundImage forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor whiteColor]}];
// ****************************************************************************
// Uncomment and fill in with your Parse credentials:
// [Parse setApplicationId:#"your_application_id" clientKey:#"your_client_key"];
// ****************************************************************************
[Parse setApplicationId:#"my_app_id" clientKey:#"my_client_key"];
// Override point for customization after application launch.
[application registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
[currentInstallation setDeviceTokenFromData:deviceToken];
currentInstallation.channels = #[#"global"];
[currentInstallation saveInBackground];
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
if (error.code == 3010) {
NSLog(#"Push notifications are not supported in the iOS Simulator.");
} else {
// show some alert or otherwise handle the failure to register. NSLog(#"application:didFailToRegisterForRemoteNotificationsWithError: %#", error);
}
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
[PFPush handlePush:userInfo];
}
Thanks so much in advance.
If you only want to make sure you DO NOT see notifications when the app is opening, set a BOOL isOpening to TRUE in your application:willFinishLaunchingWithOptions: and set it to FALSE during application:didFinishingLaunchingWithOptions:, then change the behavior of didReceiveRemoteNotification: and the call to PFPush handlePush: accordingly.
You can try this instead:
#property (nonatomic, assign) BOOL isLaunching;
- (void)applicationWillEnterForeground:(UIApplication *)application
{
isLaunching = TRUE;
}
- (void)applicationDidEnterForeground:(UIApplication *)application
{
isLaunching = FALSE;
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
if (!isLaunching) {
//Only fire the push handler if the application isn't active
[PFPush handlePush:userInfo];
}
}
Is there a way to know if the app was started by tapping message in notification center?
I want to make some calls to server only if the app is started by tapping on a message in notification center.
In - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method of the application delegate, you will receive the notification information in the launchOptions dictionary. That way you could get to know that the app was launched from the notification tray.
Yes you can find application launching reason in
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// keys can be UIApplicationLaunchOptionsLocalNotificationKey
NSDictionary *notificationPayload = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
if(notificationPayload)
{
// application launch because of notification
// do some stuff here
}
return YES;
}
You can handle push notification like
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSDictionary *pushNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (pushNotification) {
//Application did started by clicking push notification. Do whatever you want to do
}
....//Your rest code
....
}
Some times application is in active state and still we want to handle push notification than below method will be called
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
//Application did receive push notification. Do whatever you want to do
}
I want to make such function:
When the notification of my app is fired, like the following image:
I swipe the app icon in the bar to the right, and the app should run and show a certain view.
But I don't know how to do it.
In my application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions, I write:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif) {
NSString *objectIDURL = [localNotif.userInfo objectForKey:#"objectIDURI"];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Storyboard" bundle:nil];
EventViewController *eventViewController = [storyboard instantiateViewControllerWithIdentifier:#"EventViewController"];
[eventViewController setEvent:[Event getEventByObjectIDURL:objectIDURL]];
[(UINavigationController*)self.window.rootViewController pushViewController:eventViewController animated:NO];
}
return YES;
}
But after my swiping the icon to the right, my app does not run at all.
Can anyone help?
Plus, I'm using storyboard, I don't know is it relevant.
You never said if this was a local or remote notification, but the messaging works pretty much the same for both. You must keep in mind that the system notifies you differently if the app is running, or if its not running. That is, if you double click the home button and see the app's icon at the bottom, then its "running" (my terminology).
What you need to do is make sure you have all the relevant delegate methods implemented, and please, NSLog every one of them, so you can verify which are being messaged as you test. Copy and paste them from UIApplication.h, so that you don't have a typo (that is, a misspelling, as the system gives you no warnings on these!)
Implement all of the following:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// if launched due to a notification, you will get a non-nil launchOptions
NSLog(#"didFinishLaunchingWithOptions: launchOptions=%#", launchOptions);
...
}
// if using remote notifications
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
// Better get this for remote notifications
NSLog(#"didRegisterForRemoteNotificationsWithDeviceToken: token=%#", deviceToken);
...
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
NSLog(#"YIKES! didFailToRegisterForRemoteNotificationsWithError");
...
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
// if already running, notification came in
NSLog(#"didReceiveRemoteNotification: userInfo=%#", userInfo);
...
}
// if using local notifications
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
// if already running, notification came in
NSLog(#"didReceiveLocalNotification: notification=%#", notification);
...
}