I googled a lot, but I canĀ“t find the answer.
I'm trying to get the content / payload of a push notification while the app is in background.
The method:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
and
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
doesn't work.
If you don't have any background mode for the application (one of audio, gps or voip) the method -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo will be called not instantly but after the user press "Open" button bringing the app to foreground. If he or she decides to skip the notification you can do nothing about it, meaning you need to refresh the state when the application is back to foreground, you can not rely on the push notifications to update the data.
The better option might be to use the badge counter to track the changes, you need to calculate it on the server side. Then if the app in the foreground has the positive badge number you do update the data and reset the counter. However that's not perfect as there's no warranty the notification is delivered - you still need to check the badge counter (or you can say to check the number of changes) manually.
Related
Imagine this: A user sees a notification on their lock screen e.g. "your server is online". Then something changes e.g. the server goes offline. Can I programmatically remove that notification (dismiss it) from the background even after it has been displayed on the lock screen?
Yes you actually can do this, you typically see it in action in messenger apps or social networking apps, for example, in some messenger app that has a web version, if you receive a message and you read it from the web but you already have received the push on your iOS app, when this happens you must send another push without display message, but a tag with value that indicates whatever you want:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
if([[userInfo objectForKey:#"reset"] boolValue]){
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
}
}
This is a silent push notification.
I'm testing this on iOS 8.4 on Xcode simulator and on an iPhone 6. My notifications work fine and fire perfectly. But I can't figure out how to cancel the notifications when the user quits/closes the app. Pressing home button should NOT cancel the notifications and should still fire the notification.
This is what I have tried.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
return YES;
}
[[UIApplication sharedApplication] cancelAllLocalNotifications];
For the below method - it cancels the notifications when user presses Home button but or closes the app which is not what I want.
- (void)applicationDidEnterBackground:(UIApplication *)application
For below method - notifications will not cancel for home button or closing the app.
- (void)applicationWillEnterForeground:(UIApplication *)application
For below method - it does not execute the method at all for home button or closing the app.
- (void)applicationWillTerminate:(UIApplication *)application
I have looked into other similar questions posted on stack overflow but can't seem to get any of those suggestions to work. Please advise.
Once you fire the Notification it will be registered in the OS. OS presents the notification in time. You cant delete the notification after the application is terminated. No method will be called at the time of Termination.
When a notification arrives if your app is in foregroung didReceiveLocalNotification method will be called.
If you are in background DidLaunchwithOption method is called.
If the app is terminated no method is called.
You fire silent notification and present the actual notifications when you receive silent notifications in these methods. You can use userinfo to identify your notifications.
(void)applicationDidEnterBackground:(UIApplication *)application
Transitioning to the background, i.e when Home Button is pressed.
(void)applicationWillEnterForeground:(UIApplication *)application
Called when transitioning out of the background state
(void)applicationWillTerminate:(UIApplication *)application
Called only when the app is running. This method is not called if the app is suspended.
So, you won't be able to cancel notification, when app is about to be terminated. Therefore, you should choose some other mechanism to delete the Local Notifications. Like create a button to cancel all notifications or something like that.
If application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo is triggered in my running app, but I don't want to handle it right now, is it possible to leave it posted in the Notification Center so the user can swipe down later and click on it?
No, active apps get the push notification directly and the notification will not be added to the notification center.
You will have to handle it directly or store it your self for later use.
in method
didReceiveLocalNotification:(UILocalNotification *)notification i need to detect if it is invoked because user click on a notification in Notification Center or not to have appropriate actions.
Is there anyway?
using didReceiveLocalNotification:(UILocalNotification *)notification you cannot check if app is opened from local notification push notification or by directly clicking on the app icon.
But,
what you can do is in method
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions (NSDictionary *)launchOptions
write this code
if ([launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]!=nil) {
// App opened from push notification but app was not in background
}else if ([launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]!=nil){
// App opened from local notification and app was in background
}
and in method
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
you can detect if app is opened from local notification when app was in background
and finally one last method
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
In this method you can check if app is opened from push notification when app was either running or it was in backround
I guess this will help u in knowing if app is opened from notification centre or from somewhere else
When my iOS application is running in the background it responds fine to
- (void)application:(UIApplication *)application didReceiveLocalNotification:
(UILocalNotification *)notification
but when the application is closed it crashes and gives a SIGKILL error.
How can I run a method within the app if it is closed when the notification is received?
When you app is closed then when you get notification than on click of notification - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method is called.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif)
{
// code here.
}
You can't run a method in the app when a local notification is received. The notification can provide any combination of an alert, icon badge number, and a sound (<30 secs).
You can run a method when it comes into the foreground again either through the notification or through other means.
When the app is in the background it will call applicationWillEnterForeground: prior to resuming. You can override this method to handle anything needed after the notification. You can override applicationDidEnterBackground: to determine when your app actually enters the background.
Method application:didReceiveLocalNotification: is called when the app receives a notification but is in the foreground. The alert, icon badge number, and sound will not be triggered when the app is in the foreground.