iOS与WatchOS通知

前言:

这里的通知都是以远程通知为例子,iOS通知设置在AppDelegate中,其设置好了以后WatchOS会直接复制其在iOS上的通知内容和处理按钮。但是事后的逻辑代码是分开的。通过本文你可以达到的结果如下图:


iOS与WatchOS通知_第1张图片

当然WatchOS上的通知也会是有两个按钮。
借鉴:iOS 玩转推送通知
效果图

iOS与WatchOS通知_第2张图片

写在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,点击左侧的箭头

iOS与WatchOS通知_第3张图片
1

右侧会有其属性:

iOS与WatchOS通知_第4张图片
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);
    
}

这里的按钮identifier也是与iOS中注册时候同步的

你可能感兴趣的:(iOS与WatchOS通知)