问题描述
我正在寻找实现静默本地推送通知的方法。我希望在用户超出范围时向用户发送静默通知。
I am searching for way to implement silent local push notifications. I want to send silent notification to user when that user is out of range.
推荐答案
已解决。
创建本地通知时不要设置以下值。
Solved.While creating local notification don't set following values.
notification.alertBody = message;
notification.alertAction = @"Show";
notification.category = @"ACTION";
notification.soundName = UILocalNotificationDefaultSoundName;
只需创建这样的本地通知:
Just crate local notification like this:
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.通知中心不会显示通知当应用程序在后台时。
This will send local notification and will only display IconBadgeNumber as 4. No notification will be shown in notification center when app is in background.
已更新为iOS10(UNUserNotificationCenter)
在AppDelegate
In 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中
In 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.
This will send a silent notification after 15 sec with badge count as 4.
这篇关于iOS无声本地推送通知目标c?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!