我花了很多时间测试通知,但我无法让它们出现在Apple Watch上。我可以接收来自其他应用的通知 - 我的手表已配对,请勿禁用。本地推送通知出现在我的iPhone上,但不出现在Apple Watch上。通知到货时iPhone屏幕被锁定。
我正在做以下事项:
1.请求许可
2.安排通知
+ (void)requestPermission {
UIMutableUserNotificationAction *smallCupAction = [UIMutableUserNotificationAction new];
smallCupAction.title = NSLocalizedString(@"250 mL", @"Notification button");
smallCupAction.identifier = @"add small";
smallCupAction.activationMode = UIUserNotificationActivationModeBackground;
smallCupAction.authenticationRequired = NO;
UIMutableUserNotificationAction *mediumCupAction = [UIMutableUserNotificationAction new];
mediumCupAction.title = NSLocalizedString(@"350 mL", @"Notification button");
mediumCupAction.identifier = @"add medium";
mediumCupAction.activationMode = UIUserNotificationActivationModeBackground;
mediumCupAction.authenticationRequired = NO;
UIMutableUserNotificationAction *largeCupAction = [UIMutableUserNotificationAction new];
largeCupAction.title = NSLocalizedString(@"500 mL", @"Notification button");
largeCupAction.identifier = @"add large";
largeCupAction.activationMode = UIUserNotificationActivationModeBackground;
largeCupAction.authenticationRequired = NO;
UIMutableUserNotificationCategory *waterReminderCategoryDefault = [UIMutableUserNotificationCategory new];
[waterReminderCategoryDefault setActions:@[smallCupAction, mediumCupAction, largeCupAction] forContext:UIUserNotificationActionContextDefault];
waterReminderCategoryDefault.identifier = @"water reminder default";
NSSet *categories = [NSSet setWithObjects: waterReminderCategoryDefault, nil];
UIUserNotificationType types = (UIUserNotificationType) (UIUserNotificationTypeBadge |
UIUserNotificationTypeSound | UIUserNotificationTypeAlert);
UIUserNotificationSettings *mySettings =
[UIUserNotificationSettings settingsForTypes:types categories:categories];
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
}
+ (void)scheduleNotificationWithGender:(NSNumber *)gender date:(NSDate *)date {
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = date;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = // localized text
localNotif.alertAction = // localized text
localNotif.alertTitle = // localized text
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
localNotif.category = @"water reminder default";
NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:gender, kGenderKey, nil];
localNotif.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
答案 0 :(得分:0)
我通过在Watch应用中启用通知解决了我的问题: