[英]iOS Silent local push notifications objective c?
我正在寻找实现静默本地推送通知的方法。 我想在该用户超出范围时向该用户发送静默通知。
解决了。 创建本地通知时,请勿设置以下值。
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。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.