我正在使用ios 8交互式通知功能的示例应用。就显示通知而言,一切正常。但是当我在通知中选择一个动作时,会调用相应的动作方法,但该方法中的代码不会反映在应用程序中。
我的应用会委派实施通知的部分代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self setupNotification];
// Override point for customization after application launch.
return YES;
}
-(void)setupNotification
{
UIMutableUserNotificationAction *accept =[[UIMutableUserNotificationAction alloc]init];
accept.identifier =@"Accepted";
accept.title=@"Accept";
accept.destructive=NO;
accept.authenticationRequired=YES;
accept.activationMode=UIUserNotificationActivationModeBackground;
UIMutableUserNotificationAction *decline =[[UIMutableUserNotificationAction alloc]init];
decline.identifier =@"Rejected";
decline.title=@"Decline";
decline.destructive=YES;
decline.authenticationRequired=YES;
decline.activationMode=UIUserNotificationActivationModeBackground;
UIMutableUserNotificationCategory *actions =[[UIMutableUserNotificationCategory alloc]init];
[actions setIdentifier:@"customActions"];
[actions setActions:@[decline,accept] forContext:UIUserNotificationActionContextDefault];
NSSet* categories = [NSSet setWithArray:@[actions]];
UIUserNotificationSettings* settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:categories];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)())completionHandler
{
if([notification.category isEqualToString:@"customActions"])
{
ViewController *screen =[[ViewController alloc]init];
if([identifier isEqualToString:@"Rejected"])
{
[screen deleteAction];
}
else if([identifier isEqualToString:@"Accepted"])
{
[screen acceptAction];
}
}
// Important to call this when finished
completionHandler();
}
我的视图控制器
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)deleteAction
{
NSLog(@"delete");
[self.view setBackgroundColor:[UIColor redColor]];
}
-(void)acceptAction
{
NSLog(@"accept");
[self.view setBackgroundColor:[UIColor greenColor]];
}
- (IBAction)sendNotification:(id)sender {
UILocalNotification* notification = [[UILocalNotification alloc] init];
[notification fireDate ];
[notification setAlertBody:@"Notification triggered"];
[notification setCategory:@"customActions"];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
登录方法正在打印,但背景颜色没有变化。帮助我,我需要在现有应用程序中实现此功能。
答案 0 :(得分:0)
在swift中,添加:
func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forLocalNotification notification: UILocalNotification, completionHandler: () -> Void) {
Swift.print(identifier)
}
在AppDelegate中。一旦按下通知中的操作按钮
,它就会调用