前言:
這里的通知都是以遠(yuǎn)程通知為例子,iOS通知設(shè)置在AppDelegate中,其設(shè)置好了以后WatchOS會(huì)直接復(fù)制其在iOS上的通知內(nèi)容和處理按鈕。但是事后的邏輯代碼是分開的。通過本文你可以達(dá)到的結(jié)果如下圖:

當(dāng)然WatchOS上的通知也會(huì)是有兩個(gè)按鈕。
借鑒:iOS 玩轉(zhuǎn)推送通知
效果圖

寫在AppDelegate
#pragma mark 注冊遠(yuǎn)程通知
- (void)registerNotification{
UIMutableUserNotificationAction *action1 = [[UIMutableUserNotificationAction alloc]init];
action1.identifier = startEventString;
action1.title = @"Start Record Now";
action1.activationMode = UIUserNotificationActivationModeForeground;//前臺(tái)
action1.authenticationRequired = YES;//開始事件必須解鎖
/*
destructive屬性設(shè)置后,在通知欄或鎖屏界面左劃,按鈕顏色會(huì)變?yōu)榧t色
如果兩個(gè)按鈕均設(shè)置為YES,則均為紅色(略難看)
如果兩個(gè)按鈕均設(shè)置為NO,即默認(rèn)值,則第一個(gè)為藍(lán)色,第二個(gè)為淺灰色
如果一個(gè)YES一個(gè)NO,則都顯示對應(yīng)的顏色,即紅藍(lán)雙色 (CP色)。
*/
action1.destructive = NO;
UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc]init];
action2.identifier = notEventString;
action2.title = @"No I'm Not";
action2.activationMode = UIUserNotificationActivationModeBackground;//后臺(tái)
action1.authenticationRequired = NO;//取消無需解鎖
action2.destructive = YES;
//創(chuàng)建動(dòng)作(按鈕)的類別集合
UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc] init];
//這組動(dòng)作的唯一標(biāo)示(kNotificationCategoryIdentifile為我定義的一個(gè)宏,可自行定義)
category.identifier = @"NoticeIdentifier";
//最多支持兩個(gè),如果添加更多的話,后面的將被忽略
[category setActions:@[action1, action2] forContext:(UIUserNotificationActionContextDefault)];//必須是Default才可以在Watch上出現(xiàn)多個(gè)按鈕
//創(chuàng)建UIUserNotificationSettings,并設(shè)置消息的顯示類類型
UIUserNotificationSettings *uns = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound) categories:[NSSet setWithObject:category]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
[[UIApplication sharedApplication] registerUserNotificationSettings:uns];
}
特別注意 [category setActions:@[action1, action2] forContext:(UIUserNotificationActionContextDefault)];//必須是Default才可以在Watch上出現(xiàn)多個(gè)按鈕
同樣特別特別注意 category.identifier = @"NoticeIdentifier";這里的category identifier要與apns發(fā)來的json中的category相同,而且若想用WatchOS上的通知也與iOS相同,WatchOS上也會(huì)設(shè)置一個(gè)標(biāo)示與這個(gè)category identifier相同,具體的WatchOS的會(huì)在后面介紹。
同樣寫在AppDelegate 注冊遠(yuǎn)程通知后的回調(diào)
#pragma mark RemoteNotification Delegate
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{ //APP在前臺(tái)時(shí)候處理推送消息
NSLog(@"userinfo:%@",userInfo);
NSLog(@"收到推送消息:%@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]);
apnsDic = [NSMutableDictionary dictionaryWithDictionary:userInfo];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Notice" message:@"the Air is changed,Are you cooking?" delegate:self cancelButtonTitle:@"NO" otherButtonTitles:@"Start Record", nil];
[alert show];
}
- (void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void(^)())completionHandler NS_AVAILABLE_IOS(9_0) __TVOS_PROHIBITED{
NSLog(@"responsinfo is %@",identifier);
if ([identifier isEqualToString:notEventString]) {
}
else{
}
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *) error {
NSLog(@"Registfail%@",error);//注冊通知失敗
}
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
NSLog(@"token is %@",deviceToken);//這里的Token就是我們設(shè)備要告訴服務(wù)端的Token碼
}
iOS的遠(yuǎn)程通知介紹完了,接下來介紹WatchOS上的
在Watch APP interface.storyboard里面,會(huì)有通知的這一個(gè)interface,點(diǎn)擊左側(cè)的箭頭

1
右側(cè)會(huì)有其屬性:

2
這里將Name填為我們在iOS代碼中一致的identifier,這樣在apns發(fā)來信息中含有"category":"..."這個(gè)信息后,iOS會(huì)自動(dòng)識(shí)別其通知類型,并將其同步到WatchOS,watchOS只需要name一致,然后可以在通知對應(yīng)的NotificationController中修改各類屬性就好了。
在iOS中點(diǎn)擊通知的按鈕會(huì)有處理回調(diào)函數(shù),同樣在watchOS中也有:
- (void)handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)remoteNotification{
NSLog(@"remote %@ and identifier %@",remoteNotification,identifier);
}