I have noticed that when I select a push notification for my app from notification center, that notification no longer appears in the list of notifications. However, when I receive a notification and tap the banner right away, the app opens as it should, but when I pull down to view the notification center, that notification is still there. I have the following push handling code in my delegate:
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {
[[UIApplication sharedApplication] cancelAllLocalNotifications];
//Presenting view controllers...
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Extract the notification data
NSDictionary *notificationPayload = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
if(notificationPayload)
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}
return YES;
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
application.applicationIconBadgeNumber = 0;
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
// when you tap on any of notification this delegate method will call...
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
[[UIApplication sharedApplication] cancelLocalNotification:notification];
}
You have to use following to remove all notifications
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
[self clearNotifications];
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
[self clearNotifications];
}
- (void)clearNotifications
{
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 1];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}
You can implement this delegate method:
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
[application cancelLocalNotification:notification];
}
Related
I have an app that uses local notification. Until recently everything worked fine, but after iOS 8 was released, my local notifications aren't working when app is not running(removed from processes). This is my code in app delegate where I set my local notifications.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
[[NSNotificationCenter defaultCenter] postNotificationName:localReceived object:self userInfo:notification.userInfo];
// Set icon badge number to zero
application.applicationIconBadgeNumber = 0;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if ([UIApplication instancesRespondToSelector:#selector(registerUserNotificationSettings:)])
{
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:
UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}
else if ([UIApplication instancesRespondToSelector:#selector(registerForRemoteNotificationTypes:)])
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
}
...
// Local notificaiton example. Icon badge
UILocalNotification *locationNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
NSLog(#"locationNotification:%#",locationNotification.alertBody);
if (locationNotification) {
// Set icon badge number to zero
application.applicationIconBadgeNumber = 0;
// call local notification method
[self application:[UIApplication sharedApplication] didReceiveLocalNotification:locationNotification];
}
return YES;
}
When my app is not running, the didFinishLaunchingWithOptions: method is called. In it I call the didReceiveLocalNotification method again where I call postNotificationName. I put the observer in my ViewControllers ViewDidLoad method:
- (void)viewDidLoad
{
...
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(recieveLocalNotification:) name:localReceived object:nil];
...
}
The problem is that the method recieveLocalNotification: is never called when app is not running. It's called every time when app is in background or running. Can anybody tell me what I'm doing wrong here?
Thanks in advance!
-viewDidLoad is called after the view of view controller is instantiated, the view is instantiated after the viewController.view is accessed for the first time.
So you can try call your viewController.view before posting the notification.
We used Pushwhoosh for remote notification. Its working fine but Icon badge count not increased.
Here is code:
- (void) onPushReceived:(PushNotificationManager *)pushManager withNotification:(NSDictionary *)pushNotification onStart:(BOOL)onStart
{
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge)];
PushNotificationManager * pushManager = [PushNotificationManager pushManager];
pushManager.delegate = self;
if ([launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey]) {
PushNotificationManager * pushManager = [PushNotificationManager pushManager];
[pushManager startLocationTracking];
}
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}
How to increase app Icon badge count upon push delivery ?
It is a badge number issue from server side.Check whether the push notification payload contains application badge field and set to values greater than 0.
Because when the application is closed/in Background,the value in notification payloads is used to set badge in default by iOS and not the value in your code.Your code will set badge only when the application is running state.In your case,most probably the badge value passing in payload will be 0.
Got solution. We have option to set notification badge number in push whoosh server.
I want to clear all push notifications of my application, once user selects one of the push notification.
I have seen other threads here which says it's not possible in iOS.
but I have one application downloaded from app store, which does the same thing.
If it is a local notification then to remove badge icon you have to do it like this
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.applicationIconBadgeNumber = 1;
If it is push notification the you can do it by code written below
[UIApplication sharedApplication].applicationIconBadgeNumber=0;
You may call these methods anywhere you want. For example if you want to clear notification at the moment when the app is launched then write it in
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
if your app doesn't use the badge number you have to first set, then reset it to remove it from notification centre.
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
All you need to do is
application.applicationIconBadgeNumber = 0;
in
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions.
EDIT
If you are not closing your app but just sending it to background. Then add this in your below function as well.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
if ( application.applicationState == UIApplicationStateInactive || application.applicationState == UIApplicationStateBackground )
{
//opened from a push notification when the app was on background
application.applicationIconBadgeNumber = 0;
}
}
[[UIApplication sharedApplication] cancelAllLocalNotifications];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];'
I added the above code to didfinishLaunchingWithOptions but when a user taps a notification in his notification center and enters my app the notification does not gets cleared.
Edit:
I also tried adding this to my code:
You Also need to increment then decrement the badge in your
application:didReceiveRemoteNotification: method if you are trying
to clear the message from the message centre so that when a user
enters you app from pressing a notification the message centre will
also clear, ie:
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 1];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
as describes here: iOS application: how to clear notifications? but the notification still won't clear from the notification center
I just Added a Badge number manually to my application and pasted
- (void)applicationDidBecomeActive:(UIApplication *)application
{
application.applicationIconBadgeNumber = 0;
}
To my AppDelegate. For me this works like a charm.
Note that didfinishLaunchingWithOptions and applicationDidBecomeActive are not the same as Mouhammad Lamaa explained. If you paste this to your AppDelegate and tap the notification in notification center it should disapper. If it does not your App maybe creates a new Notification after becoming active?
add this code
- (void)applicationDidBecomeActive:(UIApplication *)application
{
application.applicationIconBadgeNumber = 0;
}
the didfinishlaunchingwithoptions launched at the initial launch of your app. if your app the running in the background, didfinishlaunchingwithoptions will not be launched.
When the user open application from notification action - it launches with
- (BOOL)application:(UIApplication *)app didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UILocalNotification *remoteNotif =
[launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (remoteNotif) {
//handle remote notification
}
....
}
But when the app was in background it calls
- application:didReceiveRemoteNotification:
Also method [[UIApplication sharedApplication] cancelAllLocalNotifications]; cancel registered LOCAL notifications only. Push notifications can't be canceled - they delivered immediately and executed only once.
I would like to know, if it is possible to somehow "wake up" a task that is in the background, to quickly check something on the network.. I think that this could be done with UILocalNotification, however, no matter what I tried, I could not get the didReceiveLocalNotification to do ANYTHING when the app is in the background.. After starting up, I immediately close the app by pressing the Home button (there is a 10 second delay for local notification to fire). This code works PERFECTLY when the app is in the foreground, and just kind of sitting there...
In app delegate header file:
UILocalNotification *localNotif;
For testing, I set up local notification to fire quickly in the appDelegate startup.
localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = [NSDate dateWithTimeIntervalSinceNow:10]; // the date you want the notification to fire.
localNotif.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
NSLog(#"setup the timer for 10 seconds");
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
UIApplicationState state = [application applicationState];
NSLog(#"getting kicked");
if (state == UIApplicationStateInactive) {
// Application was in the background when notification was delivered.
NSLog(#"INACTIVE..");
} else {
NSLog(#"ACTIVE..");
}
}
The user has a couple of choices: #1) Do they want to see a notification for your app. #2) If notifications are enabled for your app, do they want to click on your notification to launch your app. If they do accept notifications and open your notification while your app is in the background, application:didReceiveLocalNotification is called. To be clear, the user has to accept the notification (such as sliding the slider underneath the notification)... otherwise NOTHING is called.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
NSLog(#"%#", notification);
}
If your app has been terminated application:didFinishLaunchingWithOptions: is called -
- (BOOL)application:(UIApplication *)
application didFinishLaunchingWithOptions:
(NSDictionary *)launchOptions {
UILocalNotification *theNotification =
[launchOptions
objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
NSLog(#"%#", theNotification);
return YES;
}