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.
-
There is no facility to send a silent local notification.Paulw11– Paulw112016年05月11日 11:40:08 +00:00Commented May 11, 2016 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– dokun12016年05月11日 11:42:54 +00:00Commented May 11, 2016 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– Ruturaj Desai2016年05月11日 11:55:53 +00:00Commented May 11, 2016 at 11:55
1 Answer 1
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.