iOS无声的本地推送通知目标c?
我正在寻找方式来实施无声的本地推送通知。 当用户超出范围时,我想向用户发送无声通知。
解决了。 创build本地通知时不要设置以下值。
notification.alertBody = message; notification.alertAction = @"Show"; notification.category = @"ACTION"; notification.soundName = UILocalNotificationDefaultSoundName;
只是这样的本地通知:
UILocalNotification *notification = [[UILocalNotification alloc] init]; notification.fireDate = [NSDate date]; NSTimeZone* timezone = [NSTimeZone defaultTimeZone]; notification.timeZone = timezone; notification.applicationIconBadgeNumber = 4; [[UIApplication sharedApplication]scheduleLocalNotification:notification];
这将发送本地通知,并将只显示IconBadgeNumber为4.没有通知将显示在通知中心,当应用程序在后台。
更新了iOS10(UNUserNotificationCenter)
在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"); } }];
在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); } }];
这将在15秒后发送无声通知,徽章数量为4。