当应用程序处于后台时,如何显示自定义本地通知视图或自定义警报视图?
How can I show a custom Local notification View or an Custom Alert view when app is in Background?
实际上我想在我的应用程序中显示本地通知。但是我想用一些自定义设计来显示这个通知,比如在 it.And 上点击确定和取消按钮,我想执行一些 actions.Please 建议。
提前致谢。
您无法添加完全自定义,但您的通知可以设置 category
属性 来指定应使用哪些通知设置,这些通知设置可以指定要显示哪些按钮以及显示哪些按钮按钮在用户选择它们时显示。
我暗示这个自定义本地通知代码可能对你有帮助
其中 Accept 是您在 appDelegate.m
中获得本地通知委托的标识符
NSString *idetnt = @"Accept";
NSString *idetnt1 = @"Reject";
NSString *idetnt2 = @"Local Notification";
UIMutableUserNotificationAction *notificationAction1 = [[UIMutableUserNotificationAction alloc] init];
notificationAction1.identifier = idetnt;
notificationAction1.title = @"Snooze";
notificationAction1.activationMode = UIUserNotificationActivationModeBackground;
notificationAction1.destructive = NO;
notificationAction1.authenticationRequired = NO;
UIMutableUserNotificationAction *notificationAction2 = [[UIMutableUserNotificationAction alloc] init];
notificationAction2.identifier = idetnt1;
notificationAction2.title = @"Call";
notificationAction2.activationMode = UIUserNotificationActivationModeBackground;
notificationAction2.destructive = YES;
notificationAction2.authenticationRequired = YES;
UIMutableUserNotificationAction *notificationAction3 = [[UIMutableUserNotificationAction alloc] init];
notificationAction3.identifier = @"Reply";
notificationAction3.title = @"Reply";
notificationAction3.activationMode = UIUserNotificationActivationModeForeground;
notificationAction3.destructive = NO;
notificationAction3.authenticationRequired = YES;
UIMutableUserNotificationCategory *notificationCategory = [[UIMutableUserNotificationCategory alloc] init];
notificationCategory.identifier = @"Email";
[notificationCategory setActions:@[notificationAction1,notificationAction2,notificationAction3] forContext:UIUserNotificationActionContextDefault];
[notificationCategory setActions:@[notificationAction1,notificationAction2] forContext:UIUserNotificationActionContextMinimal];
NSSet *categories = [NSSet setWithObjects:notificationCategory, nil];
UIUserNotificationType notificationType = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:notificationType categories:categories];
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:600];
localNotification.alertBody = idetnt2;
localNotification.category = @"Email";
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
当您的应用程序在后台时,此方法会调用
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)())completionHandler
{
if ([identifier isEqualToString:@"Accept"])
{
}
else if ([identifier isEqualToString:@"Reject"])
{
}
else if ([identifier isEqualToString:@"Reply"])
{
}
if(completionHandler != nil)
completionHandler();
}
当您的应用程序在前台时,此方法会调用
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)localNotification
{
}
转到功能选项并单击远程通知
实际上我想在我的应用程序中显示本地通知。但是我想用一些自定义设计来显示这个通知,比如在 it.And 上点击确定和取消按钮,我想执行一些 actions.Please 建议。
提前致谢。
您无法添加完全自定义,但您的通知可以设置 category
属性 来指定应使用哪些通知设置,这些通知设置可以指定要显示哪些按钮以及显示哪些按钮按钮在用户选择它们时显示。
我暗示这个自定义本地通知代码可能对你有帮助 其中 Accept 是您在 appDelegate.m
中获得本地通知委托的标识符 NSString *idetnt = @"Accept";
NSString *idetnt1 = @"Reject";
NSString *idetnt2 = @"Local Notification";
UIMutableUserNotificationAction *notificationAction1 = [[UIMutableUserNotificationAction alloc] init];
notificationAction1.identifier = idetnt;
notificationAction1.title = @"Snooze";
notificationAction1.activationMode = UIUserNotificationActivationModeBackground;
notificationAction1.destructive = NO;
notificationAction1.authenticationRequired = NO;
UIMutableUserNotificationAction *notificationAction2 = [[UIMutableUserNotificationAction alloc] init];
notificationAction2.identifier = idetnt1;
notificationAction2.title = @"Call";
notificationAction2.activationMode = UIUserNotificationActivationModeBackground;
notificationAction2.destructive = YES;
notificationAction2.authenticationRequired = YES;
UIMutableUserNotificationAction *notificationAction3 = [[UIMutableUserNotificationAction alloc] init];
notificationAction3.identifier = @"Reply";
notificationAction3.title = @"Reply";
notificationAction3.activationMode = UIUserNotificationActivationModeForeground;
notificationAction3.destructive = NO;
notificationAction3.authenticationRequired = YES;
UIMutableUserNotificationCategory *notificationCategory = [[UIMutableUserNotificationCategory alloc] init];
notificationCategory.identifier = @"Email";
[notificationCategory setActions:@[notificationAction1,notificationAction2,notificationAction3] forContext:UIUserNotificationActionContextDefault];
[notificationCategory setActions:@[notificationAction1,notificationAction2] forContext:UIUserNotificationActionContextMinimal];
NSSet *categories = [NSSet setWithObjects:notificationCategory, nil];
UIUserNotificationType notificationType = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:notificationType categories:categories];
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:600];
localNotification.alertBody = idetnt2;
localNotification.category = @"Email";
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
当您的应用程序在后台时,此方法会调用
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)())completionHandler
{
if ([identifier isEqualToString:@"Accept"])
{
}
else if ([identifier isEqualToString:@"Reject"])
{
}
else if ([identifier isEqualToString:@"Reply"])
{
}
if(completionHandler != nil)
completionHandler();
}
当您的应用程序在前台时,此方法会调用
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)localNotification
{
}
转到功能选项并单击远程通知