Silent Local Notification IOS - ios

Can we have a silent local notification in IOS app. Which does some data processing in the background with out the user interacting with it.
What I want to do is create a silent local notification which fires every 8 am in morning and after the user receives it I want to do some data processing and recreate a new one which the user can see with the new data I processed after I saw the first silent local notification.
I am trying to avoid using push notification as much as I can.

You can receive silent notifications in the background on iOS, but you will need a server to actually send the notifications.
To do this you enable the Remote notifications background mode in your target's Capabilities tab:
Then you register for push notifications in application:didFinishLaunchingWithOptions: with
UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeNone categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
Pending the user allowing your app to send push notifications, you will receive the device token:
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken;
If something goes wrong, the failure handler will be called:
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:
You send the deviceToken to your server and tell it to send a silent push notification to that deviceToken at the device's local time of 8AM.
That device will have the following app delegate method called:
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler;
and you will be able to do your data processing.
Easy!
Don't forget to call the completion handler when you're done!

Related

PushNotification in BackGround

This method is called when a push notification is received while the app is in background mode and the user clicks on notification. But I want to call a method when notification comes in background mode without the user to click on the notification.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
NSLog(#"%#",userInfo);
}
For this to happen you need to enable Background mode for remote notifications. You can find it Capabilities section of the project. Besides that your incoming Notification Payload should contain content-available:1 key-value pair.
Then this method will be called immediately without user interaction.
Note: This works if the app is in Background or suspended state. If the app is completely killed or force quit by the user, it will not work.

Can I use didReceiveRemoteNotification to create local notifications?

We are planning to have end to end encryption in our messaging app, so cannot display user message as the APNS push notification message directly. So we are planning to send a silent Push notification, decrypt the message in the iOS App's background mode and then display the plain text message as a local notification
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
Yes, you could event just send the silent notification (using the contentAvailable flag in the payload) but only use the notification to notify your app of a message availability, in which the delegate method then downloads the message from a database on your secure servers then pushes the local notification to the device like you are saying.
That way you aren't even giving apple the opportunity to decrypt your message because it won't even go through their servers.

push notification Handling when app is not running / call a url if push receive the app

i have a question. I have configured my app for development push notifications and all was working fine.
All push notifications arrived the app on the device. But i want to handle the push notification in a special way. If my app is running or in background the function
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
will work fine. But is the app killed the function doesn't work. I need a idea to handle incomming push notifications whether if the app is running, in background or killed. If a push was received the app must call a callback URL. if there any way to realize this?
for example i send the following apn string:
{"aps":{"content-available":1,"alert":"This","badge":1},"callback":"https://www.xxxxxxxxx.com/sf/daniel_push/985270815/12323453534534/?device(id)=1111111111&s="}
At the end all what i want is to call the callback url if the app becomes a push notification.
Try this in didFinishLaunching:
NSDictionary *userData = [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (userData) {
// handle notification
}

Push to wake a backgrounded iOS 8 app does not get any data

There's a new feature in iOS 8: using a push notification to wake an app, which also lets the app refresh data in the background. How do I use this feature?
The push notification arrives with a value of 1 for the key 'content-available'. However, no data is received if the app is not already running in background. Why not?
There are couple of things here,
1.U need to check Remote Notification on under Background mode of the target in Capabilities section.
2.U need to implement below method,
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler

Get push notification data when application is in background IOS

Hi I am developing small IOS application in which I am using push notification. So in my case I am able get notifications and I am also able to access data for notification. Data from notification mean title, description etc. So in my case I am not saving my notification at my server side. I want to save those locally. For that what I want as soon as notification come I want to save that data locally. I am able to access my data when App is in foreground but I am not able to access my notification data when app is in background. I want to access the data of notification so that I can save it. For notification I did following things:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[application registerForRemoteNotificationTypes:
UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeSound];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
return YES;
}
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)newDeviceToken {
}
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {
for (NSString *key in [userInfo allKeys])
{
NSString *data = [userInfo objectForKey:key];
NSLog(#"inside did register for notification .... %# ---- > %#",key,data);
}
}
My requirement is simple I want access to notification when my app is in background. Any one is here who already did this? Need Help. Thank you.
In iOS, the app cannot access it's push notification until the user taps on the push notification from the notification center.
Once the push notification is tapped and the app loads/becomes active, only then will you be able to access the push notification.
FYI:
When the app is in background and a push notification is recieved.
After the user taps on the push notification:
the contents will be accessible in the -didReceiveRemoteNotification: method.
When the app is not open and a push notification is received.
After the user taps on the push notification
the contents will be accessible in the -didFinishLaunchingWithOptions: method in it's launchOptions parameter.
Also... push notifications aren't 100% reliable. They may or may not be delivered (although them not being delivered is pretty rare in my observation but worth pointing out none-the-less)
There is a way in iOS 7.0 and later where you can send notifications and access its content without user tapping it.
So, the notification payload which you send has a parameter called content-available. You can view the payload parmeters in the following link:
https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html
So when you set the content available to 1 and deliver the notification, iOS calls the below function even if the app is not in the background or foreground(must have remote notification enabled)
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
Then you can use the userInfo dictionary to fetch your notification data.

Resources