【问题标题】:iOS Silent local push notifications objective c?iOS静默本地推送通知目标c?
【发布时间】:2016-09-06 18:34:19
【问题描述】:

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

【问题讨论】:

  • 无法发送静默本地通知。
  • 当你说沉默时,你是什么意思?你想避免声音吗?请进一步解释,这个问题太宽泛了。
  • @dokun1 我正在尝试发送通知,该通知将在后台运行,如远程静默通知。用户不会在通知中心看到这些通知。

标签: ios push-notification push


【解决方案1】:

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

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

在视图控制器中

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。

【讨论】:

  • 嗨@Ruturaj Desai:感谢它的工作,但我需要使用UNUserNotificationCenter的iOS 10类似的东西。请有任何想法。
  • 嗨@Ammaiappan,我刚刚更新了iOS 10(UNUserNotificationCenter)的答案。这可能会帮助你。编码愉快。
  • 嗨@Ruturaj Desai,我尝试了同样的方法,当应用程序在前台时它的工作完美,但在后台它不起作用。背景有什么解决办法吗?
  • app没有运行会被调用吗?
猜你喜欢
  • 2018-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多