前言:
這里的通知都是以遠程通知為例子,iOS通知設置在AppDelegate中,其設置好了以后WatchOS會直接復制其在iOS上的通知內容和處理按鈕。但是事后的邏輯代碼是分開的。通過本文你可以達到的結果如下圖:
當然WatchOS上的通知也會是有兩個按鈕。
借鑒:iOS 玩轉推送通知
效果圖
寫在AppDelegate
#pragma mark 注冊遠程通知
- (void)registerNotification{
UIMutableUserNotificationAction *action1 = [[UIMutableUserNotificationAction alloc]init];
action1.identifier = startEventString;
action1.title = @"Start Record Now";
action1.activationMode = UIUserNotificationActivationModeForeground;//前臺
action1.authenticationRequired = YES;//開始事件必須解鎖
/*
destructive屬性設置后,在通知欄或鎖屏界面左劃,按鈕顏色會變為紅色
如果兩個按鈕均設置為YES,則均為紅色(略難看)
如果兩個按鈕均設置為NO,即默認值,則第一個為藍色,第二個為淺灰色
如果一個YES一個NO,則都顯示對應的顏色,即紅藍雙色 (CP色)。
*/
action1.destructive = NO;
UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc]init];
action2.identifier = notEventString;
action2.title = @"No I'm Not";
action2.activationMode = UIUserNotificationActivationModeBackground;//后臺
action1.authenticationRequired = NO;//取消無需解鎖
action2.destructive = YES;
//創建動作(按鈕)的類別集合
UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc] init];
//這組動作的唯一標示(kNotificationCategoryIdentifile為我定義的一個宏,可自行定義)
category.identifier = @"NoticeIdentifier";
//最多支持兩個,如果添加更多的話,后面的將被忽略
[category setActions:@[action1, action2] forContext:(UIUserNotificationActionContextDefault)];//必須是Default才可以在Watch上出現多個按鈕
//創建UIUserNotificationSettings,并設置消息的顯示類類型
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上出現多個按鈕
同樣特別特別注意 category.identifier = @"NoticeIdentifier";
這里的category identifier要與apns發來的json中的category相同,而且若想用WatchOS上的通知也與iOS相同,WatchOS上也會設置一個標示與這個category identifier相同,具體的WatchOS的會在后面介紹。
同樣寫在AppDelegate 注冊遠程通知后的回調
#pragma mark RemoteNotification Delegate
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{ //APP在前臺時候處理推送消息
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就是我們設備要告訴服務端的Token碼
}
iOS的遠程通知介紹完了,接下來介紹WatchOS上的
在Watch APP interface.storyboard里面,會有通知的這一個interface,點擊左側的箭頭
1
右側會有其屬性:
2
這里將Name填為我們在iOS代碼中一致的identifier,這樣在apns發來信息中含有"category":"..."這個信息后,iOS會自動識別其通知類型,并將其同步到WatchOS,watchOS只需要name一致,然后可以在通知對應的NotificationController中修改各類屬性就好了。
在iOS中點擊通知的按鈕會有處理回調函數,同樣在watchOS中也有:
- (void)handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)remoteNotification{
NSLog(@"remote %@ and identifier %@",remoteNotification,identifier);
}