我跟着this tutorial显示推送通知上的按钮
通过在didFinishLaunchingWithOptions
中调用 registerNotification 来注册按钮
但是,在少数情况下,我需要显示一个没有任何按钮的简单通知。如何显示/隐藏不同通知的按钮?
答案 0 :(得分:3)
要添加其他类型的无按钮的互动式通知,您必须更新UIMutableUserNotificationCategory
。
在没有任何按钮的情况下创建新的通知类别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];
:
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];
只需确保newNotificationCategory的标识符与您的UILocalNotification类别匹配,您不需要任何按钮。
然后安排通知:
- profile
- dashboard
- setting
答案 1 :(得分:2)
我希望这对你有所帮助。
Parameter Description. userInfo --> dictionary value for user needs. title --> notitification Title. fireDate --> trigger notification date and time. Interactive -> pass flag to set Interactive or normal notification.
-(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];
}
我正在使用此代码创建它。
更快捷
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)
}