My app contains push notification functionality and i have done the part of coding to receive push notification in my application.When my app enters to background i am receiving push notifications and when i click push notification button app come to foreground and calling did receive remote notification delegate method.I need to call the same delegate method when my app receives push notification in background but without opening the app by clicking push notification button on the top of the screen.I have written code as below.
if ([application respondsToSelector:#selector(registerUserNotificationSettings:)])
{
UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
UIUserNotificationTypeBadge |
UIUserNotificationTypeSound);
UIUserNotificationSettings *settings = [UIUserNotificationSettings
settingsForTypes:userNotificationTypes
categories:nil];
[application registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];
}
else
{
// Register for Push Notifications, if running iOS version < 8
[application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeSound)];
}
if ([application respondsToSelector:#selector(registerUserNotificationSettings:)]) {
[application registerUserNotificationSettings:[UIUserNotificationSettings
settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge
|UIUserNotificati
onTypeSound categories:nil]];
}
My final intension is to call did receive remote notification when app is in background also.
The didReceiveRemoteNotification delegate won't be invoked when the app is closed or in background.When you tap on the notification,it launches the app and the method will be invoked.You can do required action at this moment like decrementing the icon badge.This is the working flow of push notification in iOS.One more thing is when the app is closed and you are taping on notification,didReceiveRemoteNotification won't be invoked.ApplicationDidFinishLaunching will be called and you can identify there using launchoption dictionary whether its launched from notification
If App receives Notification in background & user will open app without tapping on notification than that notification will not be received at all.
To achieve this you need to try different thing.
Here is an idea that might help you:
When Notification pushed from the server & received successfully in App you can send message through Api that it received.
Now if in above case where notification not received in the app server will not get the successful receive message. So in this case it can be saved at server in pending notifications table.
Now once you open up the device 1 api will check if any pending notifications on the server & if found than will get that in response & after that it will be removed from the server.
Note: Its an idea that is tried earlier & working. You can improve it as per your requirement.
Hope it will work for you.
Related
I know this question looks duplicate but i did not found the exact answer as per my requirement.Doing app related to medical events with Below steps followed
1) connected the app to smart watch.
2) schedule the local notifications i. e when user needs to take
medicine
3) when the schedule time arrives local notification fires and
delegate method is calling (did receive local notification)
4) At the time of firing local notifications i am sending
message(sms) to the user from the app that he has to take certain
medicine
Everything is working fine when the app is in foreground and when the app reaches to background only local notifications are firing no message is received by the user.Because no method is calling when the app is in background.But my whole application use case mostly depends on sending sms.
Is there any solution for this ? Any help will be welcomed !! Thnxx !!
You're going to need to explicitly run your method in a UIBackgroundTask. See example below:
UIApplication * application = [UIApplication sharedApplication];
UIBackgroundTaskIdentifier background_task;
background_task = [application beginBackgroundTaskWithExpirationHandler:^ {
[application endBackgroundTask: background_task];
background_task = UIBackgroundTaskInvalid;
}];
// your method call here
[application endBackgroundTask: background_task];
background_task = UIBackgroundTaskInvalid;
I'm doing something similar and this works for me. Let me know if thats not working for you.
When user get a notification when the app is in background , than he push the notification message in home screen , than when the app is open this one is being called :
//on delegate class
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
{
Than here, i want to perform something , than wait for his delegate here, only than to let the app become active again, how can i eliminate the app from starting, only when i am ready for it ?
In app delegate, I have written some code in the didRecieveLocalNotification Method which firstly determines which local notification was triggered, and then generates a UIAlert once the app re opens after clicking the notification banner.
If my app is closed, the local notification is still received, and clicking on it does re open the app from its terminated state, however the code inside of the didRecieveLocalNotification method is not triggering at all. I can't even get a NSLog to work.
Anything I can do to fix this?
Have a look at https://developer.apple.com/library/ios/documentation/iphone/Reference/UILocalNotification_Class/Reference/Reference.html
You can get this information in application:didFinishLaunchingWithOptions, but only if user taps the local notification.
When the system delivers a local notification, several things can happen, depending on the application state and the type of notification. If the application is not frontmost and visible, the system displays the alert message, badges the application, and plays a sound—whatever is specified in the notification. If the notification is an alert and the user taps the action button (or, if the device is locked, drags open the action slider), the application is launched. In the application:didFinishLaunchingWithOptions: method the application delegate can obtain the UILocalNotification object from the passed-in options dictionary by using the UIApplicationLaunchOptionsLocalNotificationKey key. The delegate can inspect the properties of the notification and, if the notification includes custom data in its userInfo dictionary, it can access that data and process it accordingly. On the other hand, if the local notification only badges the application icon, and the user in response launches the application, the application:didFinishLaunchingWithOptions: method is invoked, but no UILocalNotification object is included in the options dictionary.
If the application is foremost and visible when the system delivers the notification, no alert is shown, no icon is badged, and no sound is played. However, the application:didReceiveLocalNotification: is called if the application delegate implements it. The UILocalNotification instance is passed into this method, and the delegate can check its properties or access any custom data from the userInfo dictionary.
When you app is neither running nor in the background, your notification is received in application:didFinishLaunchingWithOptions: method in your app delegate.
You can use the below code to access the notification object.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif) {
// Show Alert Here
}
}
With iOS 5 and the notification center you can have waiting, stacked up notifications each with their own data and message. Is there a way when your program is launched regularly (without tapping on the notification) to discover the waiting notifications and more importantly the data associated with them?
The other question associated with this is when the push notifications come in and your app is in the background does application:didReceiveLocalNotification: still get called or does it just go and wait till the app is launched and then you're expected to manually handle it with the launch data in theapplication:didFinishLaunchingWithOptions:
My scenario is that I need to update some core-data models with the data attached to the push notifications so I want those changes reflected no matter how they launch the app.
From my experience, there is no way to find out about notifications other than launching the app directly through them (on iOS 5, that would be right after receiving the notification or from the queued notifications present at the Notification Center).
An alternate strategy would be to use icon badges.
If you badge your app icon when receiving notifications, you could check for that number every time the app is launched and then perform the required actions.
When a notification pops up you can either view it or cancel. If the user has canceled notification, you can check for them the next time they go in the app:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if( localNotif ) {
// Do some stuff
application.applicationIconBadgeNumber = 0; // Reset num of notifications on app icon
}
}
This method gets fired if you receive a notification while you are in that specific app, or you choose to acknowledge a local notification:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
BOOL inApp = [application applicationState] == UIApplicationStateActive;
if( inApp ) { // they got this in the app
//Do some stuff in the app
}
else {
// They acknowledged the notification out of the app and here they are
// Do some other stuff
}
application.applicationIconBadgeNumber = 0;
}
That gives you good coverage for all scenarios with local notifications which would be:
Notification is received, user cancels. Check later in didFinishLaunchingWithOptions
Notification is acknowledged, the app is not open. Check in didFinishLaunchingWithOptions
Notification is acknowledged, the app is in the background. didReceiveLocalNotification:
Notification is presented while in the app didReceiveLocalNotification:
I am working on an app that runs in the background with the backgroundmode set to location. In the didUpdateToLocation: method, I want to generate local notification. I want the app to show the notification only when previous notification has been viewed. Another option is to show only the latest notification and dismiss all the previous notifications programmatically (i.e. without user interaction). Please guide me how is it possible?
Try this:
UIApplication *app = [UIApplication sharedApplication];
NSArray *oldNotifications = [app scheduledLocalNotifications];
// Clear out the old notification before scheduling a new one.
if ([oldNotifications count] > 0) {
[app cancelAllLocalNotifications];
}