2

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 May 11, 2016 at 11:27
3
  • There is no facility to send a silent local notification. Commented 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. Commented 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. Commented May 11, 2016 at 11:55

1 Answer 1

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.

answered May 11, 2016 at 12:22
Sign up to request clarification or add additional context in comments.

4 Comments

Hi @Ruturaj Desai : thanks its working, but i need similar for iOS 10 using UNUserNotificationCenter. any idea please.
Hi @Ammaiappan, I just updated my answer for iOS 10 (UNUserNotificationCenter). This might help you. Happy coding.
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?
Will it be called if app is not running?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.