我正在寻找实现静默本地推送通知的方法。我想在用户超出范围时向用户发送静默通知。
已解决。在创建本地通知时,不要设置以下值。
notification.alertBody = message;
notification.alertAction = @"Sw";
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];
UNAutrizationOptions options = UNAutrizationOptionAlert + UNAutrizationOptionSound + UNAutrizationOptionBadge;
[center requestAutrizationWithOptions:options
completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!granted) {
NSLog(@"Something went wrong");
}
}];
在 ViewController 中
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
UNMutableNotificationContent *content = [UNMutableNotificationContent new];
//content.le = @"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。
本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处
评论列表(80条)