Push notification opens app automatically when unlocking an iPad - iphone

I have developed the iPad app which uses apple push notification. Push notification delivering works fine in all the scenario except when the iPad device is locked and notification is delivered. In this scenario it behaves weirdly and opens the app when you slide to unlock the device without touching the alert/banner or notification from notification center.
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
{
[self AgendaView];
}
-(void)AgendaView
{
Agenda_Main *agendaview = [[Agenda_Main alloc]init];
[self.navigationcontroller pushViewController:agendaview animated:YES];
}

This is a feature in iOS.. When u unlock the device, the most recent notification app will be opened..

It works that way when first receiving the alert. If you let the device lock again after receiving it, when you unlock it the next time you will go to Springboard

That is just how iOS works. You'll notice that when the alert comes in, the lock screen will only show information for that alert (it will also be centred on the vertical axis).
If you press the lock button to turn the screen off, then the home button to turn it back on you will see that the notification has now stacked to the top of the window, along with any other notifications. If you unlock the device now it will take you to the springboard, and not the app.

Related

How to turn Auto-Lock to Never in Swift?

Is there any Swift command to actually set the Auto-Lock to Never or a specific time period? I want to create a simple app that only has two buttons: one is to set the Auto-Lock to Never and the other one is to set it back to iOS default (1 min).
So when a user open this app and tap the Never button, s/he can open other apps but the iPhone or iPad will never auto lock while running the other apps. If s/he is done with other apps, s/he can open this app again and tap the Default button to set the Auto-Lock back to 1 min.
I understand this can be done from the Settings but I am just curious how I can do it from the backend using Swift.
I am new to Swift, btw.
Thanks much!
So when a user open this app and tap the Never button, s/he can open other apps but the iPhone or iPad will never auto lock while running the other apps
You can't do that. You are sandboxed. You cannot affect what happens to the user while running some other app.
When app is open, try to disable the idleTimer in viewDidLoad
UIApplication.shared.isIdleTimerDisabled = true
When app is closed to open other apps, try to -enable again idleTimer when yourViewController disappears, so put this in viewDidDisappear
UIApplication.shared.isIdleTimerDisabled = true
You need to make use of this API call to set the idle timer disable and enable.
This is in objective-C. Just convert it to swift. The API is available in UIApplication.h
-(void) onApplicationDidActivate:(NSNotification*) notification
{
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];
}
-(void) onApplicationWillDeactivate
{
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];
}

Lock iphone home screen from my app

I want to lock the device home screen programatically from my app. Is it possible in IOS?
In my app, if the user is idle for 1 minute, i want to lock the device. This is the scenario.
Thanks
Jithen
you can lock the phone using bellow code
[UIApplication sharedApplication].idleTimerDisabled = YES;
You can lock your own App by Adding a block view on Appdelegate's window. But locking your device is the responsibility of OS. I think if you somehow implement this then still aplle might reject your app on it.

iOS Push Notification - How to get the notification data when you click on the app icon instead of notification

Similar to this question: How do I access remote push notification data on applicationDidBecomeActive?
But the different is how can you access the notification data when you are inapplicationDidBecomeActive and if you have clicked on the app icon instead of the push notification.
The flow is: If you click on the push notification then didReceiveRemoteNotification will be triggered, but if you click on the original app icon, only applicationDidBecomeActive will be triggered and didReceiveRemoteNotification will not be called.
I am looking for the later case so how can I access the push notification data.
(Both case assuming the app is in background and not killed yet.)
You can't get remote push payload by launching app from homescreen.
If the push data is important for app use, load it from your server after app launched.
#fannheyward answer is absolutely correct. You cannot get payload when application is launched by tapping app icon.
I have an idea, what if you get to know that some notification is pending when app is launched by tapping app icon. With this knowledge your app can fetch payload from your server.
You can set "Badge" in every such notification and on applicationDidBecomeActive you can check [application applicationIconBadgeNumber] > 0 to know that some notification is active. After fetching payload from your server you can set it to 0 like below
[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
Please note: This means your app will have badge displayed over it when notification is received. I am not sure of the behaviour when badge is disabled by user from settings.
If your application target is over iOS7, you can do only if application is alive in backgroud.
In the capabilities settings at Xcode, you should enable Background Modes>Remote notifications, and write below code.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
// save userInfo in NSUserDefaults
completionHandler( UIBackgroundFetchResultNoData );
}
If you want to test it, it will be helpful to use https://github.com/acoomans/SimulatorRemoteNotifications
From the server side, make sure to set content-available property with a value of 1
For this to work I also had to check the background fetch box.
You should get the notification in the launchWithOptions method in your appDelegate something like this:
NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
if(remoteNotif != nil){
//Handle your notification
}

iOS app badge persists

I have an iPhone app which uses local notification. It is working almost perfectly, although I have a little problem: The red balloon (app badge) from notification doesn't dismiss when I launch the app. What can I do to fix it?
I've already deleted the app from iPhone, but when I compile it again with Xcode, I comes back again.
Try
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
after you have processed your notification.

Push Notifications without alert

Can I get push notifications without alert view?
The idea in what service for game will send notifications as response for change game state (may be game state will store in database), if this is impossible, what you can suggest about how me send new game state to each connected game client as response of changing game state.
Game will be developing for iPad.
Thanks,
Roman
For me #Ajay answer is not correct (sorry).
With push notification you can choose between three user options: text message (alerts), sounds and badges. Every push notification can contain one or more of these options, so you can send for example a notification with sound and badge, but without message and in this case any alert is shown.
Note that you can even pass hidden options in a private dictionary to your application.
Use content-available property:
The aps dictionary can also contain the content-available property. The content-available property with a value of 1 lets the remote notification act as a “silent” notification. When a silent notification arrives, iOS wakes up your app in the background so that you can get new data from your server or do background information processing. Users aren’t told about the new or changed information that results from a silent notification, but they can find out about it the next time they open your app.
https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html
Why not!
You can send a hidden push notification without any alert, banner or sound.
PHP CODE
without a text:
$payload['aps'] = array('badge'=> 0, 'sound' => 'default');
Without text and sound
$payload['aps'] = array('badge'=> 0);
and on your ios end you can make a condition for this:
//Receiving a Push Notification
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSMutableDictionary *pushNotiFicationDict = [userInfo objectForKey:#"aps"];
NSLog(#":%#", pushNotiFicationDict);
if([pushNotiFicationDict objectForKey:#"alert"])
{
// when push notification has text
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:#"MESSAGE" message:[pushNotiFicationDict objectForKey:#"alert"]
delegate:nil
cancelButtonTitle:#"ok"
otherButtonTitles:nil];
[alert show];
}
else
{
// when push notification does not have text
}
}
But i think you can not perform any action if application is not running or running in background.
No, a push notification will always display a notification as it requires user consent to wake up or launch your application. However if your Application is in the foreground and running, the push notification will not appear and your app can handle the message that the push notification has. All of the preceding applies to local notifications aswhile.
I don't know what you mean by game state. But just have your app listen in on a script on your server which will pass information to your app. Edit: Or like I said above if your app is open in the foreground, push notifications won't appear on screen and you can send information that way. However if you want to do it in the background its not possible no matter what unless you are truly multitasking (GPS, VOIP, Music) or you have user consent through push notification.
You can handle notifications in app and store data in DB, or instead show a message but when app is not running NO.
I agree with #MacTeo. You can delivery a push notification with a payload that contains only one attribute (Sound, Alert (or message) and badge.
With your requirement, keep track of the users device on the server when they open and close the app (foreground and background). If something happens then you can check to see the device's state on the server. You could then decide on how to construct a notification (if any is necessary) base on the last reported state of the device.
Just spitballing:
You could, if you wanted to (however, it isn't what the APNs is designed to do), deliver a push notification with only a badge count = 0 and pass some extra dictionary data with it. That would not alert the user and could be handled in the app if it was in the foreground.