I spent a lot of time testing notifications and I can't make them appear on my Apple Watch. I can receive notifications from other apps - my watch is paired, Do Not Disturbe is disabled. Local push notifications appear on my iPhone but not on the Apple Watch. iPhone screen is locked when notification arrives.
I am doing following:
1. Asking permission
2. Scheduling notification
+ (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];
}
I solved my problem by enabling notifications in the Watch app:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.