交互式推送通知 – 隐藏/显示button

我按照本教程在推送通知上显示button。
通过在didFinishLaunchingWithOptions调用registerNotification来注册button。
但是,在less数情况下,我需要显示一个简单的通知没有任何button。 如何显示/隐藏不同通知的button?

为了添加另一种没有button的交互式通知,您将不得不更新UIUserNotificationSettings

创build一个新的通知类别UIMutableUserNotificationCategory没有任何button:

 UIMutableUserNotificationCategory *newNotificationCategory = [[UIMutableUserNotificationCategory alloc] init]; newNotificationCategory.identifier = @"no_button_id"; 

然后,将这个新类别添加到现有的UIUserNotificationSettings

  NSMutableArray *arrNewCategories = [NSMutableArray new]; UIUserNotificationSettings *oldSettings = [[UIApplication sharedApplication]currentUserNotificationSettings]; for (UIMutableUserNotificationCategory *oldCategory in oldSettings.categories) { if (![oldCategory.identifier isEqualToString:newNotificationCategory.identifier]) [arrNewCategories addObject:oldCategory]; } [arrNewCategories addObject:newNotificationCategory]; UIUserNotificationType notificationType = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert; UIUserNotificationSettings *newSettings = [UIUserNotificationSettings settingsForTypes:notificationType categories:[NSSet setWithArray:arrNewCategories]]; [[UIApplication sharedApplication] registerUserNotificationSettings:newSettings]; 

只要确保newNotificationCategory的标识符与您的UILocalNotification的类别相匹配,您不需要任何button。

然后安排通知:

 UILocalNotification* localNotification = [[UILocalNotification alloc] init]; localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:afireDate]; localNotification.alertBody = @"alert body text"; localNotification.category = @"no_button_id"; // Same as category identifier localNotification.timeZone = [NSTimeZone systemTimeZone]; localNotification.soundName = SOUND_FILE; localNotification.repeatInterval = 0; [[UIApplication sharedApplication]scheduleLocalNotification:localNotification]; 

我希望这对你有所帮助。

参数说明。
     userInfo  - >字典值为用户需要。
    标题 - > notitification标题。
     fireDate  - >触发通知date和时间。
    交互式 - >通过标志设置交互式或正常通知。
 -(void)setLocalnotfication:(NSDictionary *)userInfo title:(NSString *)title date:(NSDate *)fireDate Interactive : (BOOL)flag { UILocalNotification *notification = [[UILocalNotification alloc] init]; notification.fireDate = fireDate; notification.alertBody = title; notification.timeZone = [NSTimeZone defaultTimeZone]; notification.soundName = UILocalNotificationDefaultSoundName; if(flag) notification.category = @"Category name"; if([userInfo isKindOfClass:[NSDictionary class]]) notification.userInfo = userInfo; [[UIApplication sharedApplication] scheduleLocalNotification:notification]; } 

我正在使用此代码来创build它。

更快捷

 func setLocalnotfication(userInfo: [NSObject : AnyObject], title: String, date fireDate: NSDate, Interactive flag: Bool) { var notification: UILocalNotification = UILocalNotification() notification.fireDate = fireDate notification.alertBody = title notification.timeZone = NSTimeZone.defaultTimeZone() notification.soundName = UILocalNotificationDefaultSoundName if flag { notification.category = NCI } if (userInfo is NSDictionary.self) { notification.userInfo = userInfo } UIApplication.sharedApplication().scheduleLocalNotification(notification) } 

请参阅网站