为什么由UILocalNotification生成的UIAlert显示“选项”而不是动作标题?

时间:2015-06-13 04:45:53

标签: ios objective-c notifications apple-watch

我正在尝试设置Apple Watch应用程序(显示抽认卡),在新问题到期时触发通知。如果iPhone已打开但应用程序在后台,则会触发UIAlert,然后单击操作按钮(“查看”)将打开该应用程序。如果iPhone被锁定,通知会在Apple Watch上闪烁一瞥,只需一瞥“查看问题”按钮即可打开Apple Watch应用程序。

它有效,但iPhone上的UIAlert有一个烦人的“选项”按钮,要求你点击“选项”打开另一个带有两个动作按钮(“查看”和“查看问题”)的UIAlert,其中任何一个都将打开iPhone应用程序。以下是首先取消所有先前通知的相关代码(因为应用程序允许您在计时器完成之前回答新问题,此时计时器被重置)。然后设置要在iPhone上显示的通知;最后为Apple Watch设置通知。

// Cancel previous notifications
[[UIApplication sharedApplication] cancelAllLocalNotifications];

// Set up iPhone notification
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:(int)dateLatency];
localNotification.timeZone = [NSTimeZone defaultTimeZone];

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSInteger intVal = [prefs integerForKey:@"timeLag"];

localNotification.alertBody = [NSString stringWithFormat:@"%ld-%d", (long)intVal, (int)dateLatency];
localNotification.alertAction = [NSString stringWithFormat:@"View"];
localNotification.soundName = UILocalNotificationDefaultSoundName;

localNotification.alertLaunchImage = nil;

localNotification.category = @"qDue";
localNotification.userInfo = [self createAPNS:@"999"]; // function that constructs userInfo json structure    

// Set up apple watch notification
NSMutableSet *categories = [[NSMutableSet alloc] init];
UIMutableUserNotificationAction *viewQuestion = [[UIMutableUserNotificationAction alloc] init];
viewQuestion.title = @"View Question";
viewQuestion.identifier = @"viewQuestion";
viewQuestion.activationMode = UIUserNotificationActivationModeForeground;
viewQuestion.authenticationRequired = false;

UIMutableUserNotificationCategory *questionCategory = [[UIMutableUserNotificationCategory alloc] init];
questionCategory.identifier = @"qDue";
[questionCategory setActions:@[viewQuestion] forContext:UIUserNotificationActionContextDefault];

[categories addObject:questionCategory];

UIUserNotificationType notificationType = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:notificationType categories:categories];

[[UIApplication sharedApplication] registerUserNotificationSettings:settings];

// Schedule it with the app
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

我可以通过删除苹果手表通知来摆脱“选项”按钮和额外的UIAlert,但是苹果手表上没有通知。

有人可以告诉我为什么出现“选项”按钮,以及如何摆脱它?

0 个答案:

没有答案