iOS无声本地推送通知目标c?

Rut*_*sai 2 push push-notification ios

我正在寻找实现静默本地推送通知的方法.我想在用户超出范围时向用户发送静默通知.

Rut*_*sai 6

解决了.创建本地通知时不要设置以下值.

notification.alertBody = message;
notification.alertAction = @"Show";
notification.category = @"ACTION"; 
notification.soundName = UILocalNotificationDefaultSoundName;
Run Code Online (Sandbox Code Playgroud)

只需创建这样的本地通知:

UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [NSDate date];
NSTimeZone* timezone = [NSTimeZone defaultTimeZone];
notification.timeZone = timezone;
notification.applicationIconBadgeNumber = 4;
[[UIApplication sharedApplication]scheduleLocalNotification:notification];
Run Code Online (Sandbox Code Playgroud)

这将发送本地通知,并且仅将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");
                          }
                      }];
Run Code Online (Sandbox Code Playgroud)

在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);
    }
}];
Run Code Online (Sandbox Code Playgroud)

这将在15秒后发送无声通知,徽章数为4.