iOS NSNotificationCenter and push notifications - ios

By default, when a push (remote) notification is received, is a NSNotification posted to the default NSNotification center?
I know that - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo is called, but I'd like another class to know that a push notification was received.
Do I have to post a NSNotification by myself or is it automatically posted?

Related

Silent Local Notification 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!

iOS: Detect app start via notification press

is there a way to detect if my App is started from a notification being displayed on the home screen?
When my App is going into background I schedule a UILocalNotification. When the user presses this notification at the home screen, the app is starting again. I want to get this information that my app was started again from pressing the notification.
I already tried out what was mentioned in Send notification to user from app via notification API but the app does not call the method:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
Thanks for your help.
The delegate method you are implementing is for remote notifications. In order to get the user info for the local notifications scheduled by your app you have to implement:
- (void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification
Apple Documentation has all the information you need on local notifications.
Add these lines to didFinishLaunchingWithOptions function in your app delegate
NSDictionary *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (localNotif)
{
//your code here
}

Remote notification is not called when app is in background

Although this was asked in several places I haven't seen one answer that helped me.
I am trying to get a push notification and tried the following functions:
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
The issue:
I am only able to receive the remote notification if the app is in foreground. If it's in background, nothing gets invoked.
I am using iOS8.
The following background modes are checked: "Background fetch","Remote notification"
Can anyone help me with that?
Its pretty deep in the documenation, but in your JSON payload, you need to set the 'content-available' to 1.
https://developer.apple.com/library/mac/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html
If your payload doesn't have an "alert" or "content-available" then you won't get a notification in your app delegate.

Getting push notification payload when user opens app manually after push has been received in the background

I am using iOS7 and I am trying to determine if I can get the JSON payload in the following situation.
I have background mode "remote-notifications" enabled
The push notification is received while the app is terminated
The app is launched manually from the icon not from the notification center
When I launch the app from the icon itself after the notification has been received I do not get the push in the launch options from
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
and the following method does not get called either when app is manually launched from the icon
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
I finally figured out how you can get this!
As of iOS 7 you can get it!
Basically, you need to configure your application for background remote notifications.
So, in your info.plist file:
For required backgrounds - set it to app downloads content from push notifications.
In the AppDelegate.m file, you need to implement this method:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
See this for how to implement that: didReceiveRemoteNotification: fetchCompletionHandler: open from icon vs push notification
For your push notifications, you must have 'content-available': 1, as part of the push notification. This is what tells the application that there is new content before displaying the alert.
See this page for more information on background remote notifications: http://developer.xamarin.com/guides/cross-platform/application_fundamentals/backgrounding/part_3_ios_backgrounding_techniques/updating_an_application_in_the_background/

Is this possible to handle which iOS notification is clicked on the notification centre?

I would like to know, if the user click the notification in the notification centre, whether the application can detect which notification they are clicking and how I can handle it? Thanks.
Yes. In your app delegate, the notification details will be received in the method - (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
The userInfo will have the payload that you sent with your push notification. It recommended that when you send a push notification that you include identifying information in the payload (such as an object id) to figure out which push notification was selected/received.

Resources