How to clear a single notification from a list of notifications on clicking? - iphone

Once i click a notification, all the notifications are cleared.In ios, is there any option to clear a single notification after tapping on it? i have received 4 notifications. now i need the clicked notification alone to get cleared and retain the other ones.Can anyone help me on this?
- (void) clearNotifications {
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
{
NSLog(#"Received notification: %#", userInfo);
[self clearNotifications];
}

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
// when you tap on any of notification this delegate method will call...
[[UIApplication sharedApplication] cancelLocalNotification:notification];
}

Related

How to get notification.alertBody in my app from a remote push notification?

I just followed this tutorial Push notification and I successfully implemented the push notification for my iPhone app. Im able to get now the notification Details. However, I wanted to put the notification alertBody on a Label provided for notification alertBody.
I have a code in displaying the notification alertBody from a local Notification. But I know it is different from a push notification because it is used only for local notification.
on my AppDelagate.m
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
NSLog(#"Recieved Notification %#",notif);
NSString *_stringFromNotification = notif.alertBody;
[[NSNotificationCenter defaultCenter] postNotificationName:#"Notification" object:_stringFromNotification];
}
on my ViewController.m
- (void)viewDidLoad{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserverForName:#"Notification" object:nil queue:nil usingBlock:^(NSNotification *note)
NSString *_string = note.object;
//Do something with the string--------
}];
}
It works perfectly on Local Notification but for push notification, It doesn't work. How to Implement this? Need your help please. I need to put the notification alert body at the Label or String.
first of all register for remote notifications in AppDelegate.m in method,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//Invoke APNS.
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
}
And then use following delegate method to recieve remote notification:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(#"Received =%#",userInfo);////userInfo will contain all the details of notification like alert body.
}
Remote notifications run outside the sandbox the app is running in, so you can't capture the notification in the same way as local notifications, i.e. application:didReceiveLocalNotification. However, if the app is launched via the remote notification, you can capture the notification via the application:didFinishLaunchingWithOptions
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UILocalNotification *notification = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
if (notification) {
// do something with the notification.alertBody
} else {
// from the springboard
}
}
If your application is already running when it receives a remote notification, your application delegate's – application:didReceiveRemoteNotification: method will be called; if the application is not currently running and is launched in response to the notification, the remote notification info will be put into the launchOptions dictionary in your – application:didFinishLaunchingWithOptions: method.
The method which you are implementing is for local notification only. If you want to handle push notification then you have to use method
- (void)application:(UIApplication*)application didReceiveRemoteNotification: (NSDictionary*)userInfo{
NSLog(#"Received notification: %#", userInfo);
}
for the same. This method will be called if the app is on background only. If the app is not in background then you can fetch data in the following manner
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
UILocalNotification *notificationData = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
if(!notificationData){
NSLog(#"App launched by tapping on app icon");
}else{
NSLog(#"Notification data -> %#",notificationData);
}
}

Removing UILocalNotification from notification tray programmatically

Is there a way to programmatically remove/dismiss UILocalNotification from Notification Tray.
I am able to cancel the notification which removes the notifications from
[[UIApplication sharedApplication] scheduledLocalNotifications]
Here is what i need to do
I need to dismiss the UILocalNotification from NotificationTray after the action has been performed(ie after the user taps the notification)
EDIT:
I can remove the notifications from the NSNotificationCenter. I want to remove specific notifications from the Notification Tray .Like the user presses the clear button to clear all the notifications belonging to a particular application.
You can cancel all notifications using:
[[UIApplication sharedApplication] cancelAllLocalNotifications];
If you want to remove a particular notification, you can use userinfo of notification object, when you create a local notification add a unique ID to that. Later you can use that ID for removing local notification.
For that you can use the following code:
NSString *notificationId = #"id_to_cancel";
UILocalNotification *notification = nil;
for(UILocalNotification *notify in [[UIApplication sharedApplication] scheduledLocalNotifications])
{
if([[notify.userInfo objectForKey:#"ID"] isEqualToString:notificationId])
{
notification = notify;
break;
}
}
[[UIApplication sharedApplication] cancelLocalNotification:notification];
I believe I had a similar issue. When the app entered the foreground I attempted to clear past notifications to remove any old notifications from the notifications tray.
I did something like this to grab old notifications and remove them:
NSArray *activeNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
NSArray *pastNotifications = [activeNotifications filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"firDate < %#", [NSDate date]]];
for (UILocalNotification *notification in pastNotifications) {
[[UIApplication sharedApplication] cancelLocalNotification:notification];
}
However, it seems that scheduledLocalNotifications does not include locations whose fire date is already past even though they still appear in notification center.
Calling cancelAllLocalNotifications does seem to remove past notifications as well. So we can grab all the current notifications, cancel everything, and then add the ones we're still interested in back.
// Grab all the current notifications
NSArray *activeNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
// Clear all notifications to remove old notifications
[[UIApplication sharedApplication] cancelAllLocalNotifications];
// Add back the still relevant notifications
for (UILocalNotification *notification in activeNotifications) {
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
Additionally we can do some filtering of the notifications before adding them back if some are no longer needed, and we can grab the active notifications when the app becomes active, store them in an instance variable, and only add them back when the app moves to the background
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
will do some trick too
but if you didnot use applicationIconBadgeNumber, it will not work, so trick is set
applicationIconBadgeNumber first :)
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
If the Application is not running, you will be receiving the Local Notification object in the
-applicationDidFinishLaunchingWithOptions:
like:
UILocalNotification *localNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsLocalNotificationKey];
or else you can get it in
(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
Now you can remove it from the Notification Center using
[[UIApplication sharedApplication]
cancelLocalNotification:notificationToCancel];
// deletes a pushnotification with userInfo[id] = id
-(void)deleteLocalPushNotificationWithId:(NSString*)id{
for(UILocalNotification *notification in [[UIApplication sharedApplication] scheduledLocalNotifications]){
if([[notification.userInfo objectForKey:#"id"] isEqualToString:id]){
[[UIApplication sharedApplication] cancelLocalNotification:notification];
}
}
}
// deletes all fired pushnotifications
-(void)clearLocalPushNotifications{
NSArray *activeNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
// Clear all notifications to remove old notifications
[[UIApplication sharedApplication] cancelAllLocalNotifications];
// Add back the still relevant notifications
for (UILocalNotification *notification in activeNotifications) {
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
}
I was fiddling with some code and I was wondering why local notifications are stored in the notification center if the application is in the foreground. It's probably because Apple doesn't know what you are doing with them and honestly doesn't care; so they do their job.
As far as the question is concerned, I do the following:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
if (application.applicationState == UIApplicationStateActive)
{
NSLog(#"active");
// display some foreground notification;
[application cancelLocalNotification:notification];
}
else
{
NSLog(#"inactive");
}
}
So I just read this thread about how to close/remove all the already fired local notifications from the Notification center, if the user opens the app by clicking the app icon, not the notification. But after all of this, the other scheduled local notification should fire in the future.
Here is my easy solution for this, which should be triggered on application did becomeActive:
UIApplication* application = [UIApplication sharedApplication];
NSArray* scheduledNotifications = [NSArray arrayWithArray:application.scheduledLocalNotifications];
application.scheduledLocalNotifications = scheduledNotifications;
I ve tried the [[UIApplication sharedApplication] cancelLocalNotification:notification]; but it did not clear the already fired local notifications from the Notification center (outside of the app).

Can I programmatically clear my app's notifications from the iOS 5 Notification Center?

I would like to remove old notifications that my app has made from the iOS 5 Notification Center. Can I do this? If so, how?
To remove notifications from the Notification Center simply set your icon badge number to zero.
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
This only works if the number changes, so if your app doesn't use the badge number you have to first set, then reset it.
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
A more straightforward method that I use (and doesn't require badges) is to reset the array of scheduled local notifications to itself, as follows:
UIApplication* application = [UIApplication sharedApplication];
NSArray* scheduledNotifications = [NSArray arrayWithArray:application.scheduledLocalNotifications];
application.scheduledLocalNotifications = scheduledNotifications;
This has the effect that any scheduled notifications remain valid, while all "old" notifications that are present in Notification Center are removed. However, it also has the feel of something that might change in a future release of iOS, as I haven't seen any documentation for this behavior.
Of course, if you want to remove all notifications, it's simply the following:
[[UIApplication sharedApplication] cancelAllLocalNotifications];
Yes, you can cancel specific or all local notifications by calling
[[UIApplication sharedApplication] cancelLocalNotification:...];
or
[[UIApplication sharedApplication] cancelAllLocalNotifications];
If you want to clear notifications in swift and iOS 10.0
import UserNotifications
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.removeAllPendingNotificationRequests() // To remove all pending notifications which are not delivered yet but scheduled.
center.removeAllDeliveredNotifications() // To remove all delivered notifications
}
For me it only worked with sending a local notification with only a badge like this:
if([UIApplication sharedApplication].applicationIconBadgeNumber == 0) {
UILocalNotification *singleLocalPush = [[UILocalNotification alloc] init];
singleLocalPush.fireDate = [NSDate dateWithTimeIntervalSinceNow:1];
singleLocalPush.hasAction = NO;
singleLocalPush.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:singleLocalPush];
[singleLocalPush release];
} else {
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}
And in the method
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
I can set the badge to 0 again.

Clearing the badge when received Push Notification

How can I clear the badge which appears on application icon when I receive Push Notification? I want to clear it once user has either tapped on "View" of Push notification alert or has tapped on the app icon.
I suspect you are talking about the SpringBoard's badge:
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]
Badge count set Zero
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]
Cancel all local notifications with this code:
[[UIApplication sharedApplication] cancelAllLocalNotifications];
Cancel one local notification with this line of code:
[[UIApplication sharedApplication] cancelLocalNotification:theNotification];
here theNotification is a UILocalNotification object, so in order to cancel a specific notification, you need to hold on to it's UILocalNotification.
Check this.
For Mac OS X Lion, it's:
[NSApp dockTile].badgeLabel = #"";
(Lion supports badge-type push notifications.)
From Apple's documentation, set the application.applicationIconBadgeNumber to the number you want displayed on the badge. If you set it to 0, it will be cleared.
- (BOOL)application:(UIApplication *)app didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif) {
NSString *itemName = [localNotif.userInfo objectForKey:ToDoItemKey];
[viewController displayItem:itemName]; // custom method
application.applicationIconBadgeNumber = localNotif.applicationIconBadgeNumber-1;
}
[window addSubview:viewController.view];
[window makeKeyAndVisible];
return YES;
}
Reference - Scroll down to the Handling Local and Remote Notifications section just above listing 2.4

How can I make a call, as I recieve a push notification?

I have developed an appointment application. When I receive a push notification, I want to make a call to a particular person, but right now it is just opening the application when I get a push notification.
How do I write a code to make a call to a particular appointment as I get the push notification of that appointment?
For push notification you have to code in appDelegate,
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:#"tel://%#",[userInfo valueForKey:#"phno"]]]];
}
For local notification,
-(void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:#"tel://%#",[notification.userInfo valueForKey:#"phno"]]]];
}
Upon notification, open the dialer app with the given phone number. Note: this will initially launch your app, then quickly switch to the dialer.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"tel://5555555555"]];
}