gpt4 book ai didi

iOS 静默本地推送通知 objective-c ?

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:12:30 26 4
gpt4 key购买 nike

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

最佳答案

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

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");
}
}];

在 View Controller 中

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 秒后发送一个静默通知,角标(Badge)计数为 4。

关于iOS 静默本地推送通知 objective-c ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37161265/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com