I am searching for way to implement silent local push notifications. I want to send silent notification to user when that user is out of range.
Asked
Active
Viewed 3,639 times
2
-
There is no facility to send a silent local notification. – Paulw11 May 11 '16 at 11:40
-
When you say silent, what do you mean? Do you want to avoid sound? Please explain this further, this question is too broad. – dokun1 May 11 '16 at 11:42
-
@dokun1 I am trying send notification which will work in background like Remote silent notifications. User will not see those notifications in notification center. – Ruturaj Desai May 11 '16 at 11:55
1 Answers
6
Solved. While creating local notification don't set following values.
notification.alertBody = message;
notification.alertAction = @"Show";
notification.category = @"ACTION";
notification.soundName = UILocalNotificationDefaultSoundName;
Just crate local notification like this:
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [NSDate date];
NSTimeZone* timezone = [NSTimeZone defaultTimeZone];
notification.timeZone = timezone;
notification.applicationIconBadgeNumber = 4;
[[UIApplication sharedApplication]scheduleLocalNotification:notification];
This will send local notification and will only display IconBadgeNumber as 4. No notification will be shown in notification center when app is in background.
Updated for iOS10 (UNUserNotificationCenter)
In AppDelegate
@import UserNotifications;
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
UNAuthorizationOptions options = UNAuthorizationOptionAlert + UNAuthorizationOptionSound + UNAuthorizationOptionBadge;
[center requestAuthorizationWithOptions:options
completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!granted) {
NSLog(@"Something went wrong");
}
}];
In ViewController
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
UNMutableNotificationContent *content = [UNMutableNotificationContent new];
//content.title = @"Don't forget";
//content.body = @"Buy some milk";
//content.sound = [UNNotificationSound defaultSound];
content.badge = [NSNumber numberWithInt:4];
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:15 repeats:NO];
NSString *identifier = @"UniqueId";
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier
content:content trigger:trigger];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (error != nil) {
NSLog(@"Something went wrong: %@",error);
}
}];
This will send a silent notification after 15 sec with badge count as 4.

Ruturaj Desai
- 116
- 1
- 6
-
Hi @Ruturaj Desai : thanks its working, but i need similar for iOS 10 using UNUserNotificationCenter. any idea please. – Ammaiappan Jun 29 '17 at 11:40
-
Hi @Ammaiappan, I just updated my answer for iOS 10 (UNUserNotificationCenter). This might help you. Happy coding. – Ruturaj Desai Jun 30 '17 at 13:11
-
Hi @Ruturaj Desai, I tried same, and its works perfect when app is in foreground, But in background it does not work. Any work around for background? – Abhijeet Barge Aug 28 '17 at 10:23
-