一个优化,如果需要像微信一样可以在推送的时候震动可以使用NotificationService的扩展,大概思路如下

huqngqic 2023-5-19 553

//开始震动 AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); NSDictionary pushDictionary = request.content.userInfo; if([pushDictionary.allKeys containsObject:@"voip"]){ NSInteger voip_type = [pushDictionary[@"voip_type"] integerValue]; //判断是否是需要持续震动的通知类型 if(voip_type == 1){ dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); if (self.vibrationTimer) { dispatch_cancel(self.vibrationTimer); self.vibrationTimer = nil; } self.vibrationTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue); dispatch_time_t start = dispatch_time(DISPATCH_TIME_NOW, 1 NSEC_PER_SEC); uint64_t interval = 1 NSEC_PER_SEC; dispatch_source_set_timer(self.vibrationTimer, start, interval, 0); __block int times = 0; //最多震动20次,或者用户点击了推送的通知,则停止震动 dispatch_source_set_event_handler(self.vibrationTimer, ^{ self.contentHandler(self.bestAttemptContent); AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); times++; NSUserDefaults userDefault = [[NSUserDefaults alloc]initWithSuiteName:@"group.com.yehuoim.demo"]; NSString *status = [userDefault objectForKey:@"status"]; if(times >=20 ||[status isEqualToString:@"1"]){ dispatch_suspend(self.vibrationTimer); dispatch_cancel(self.vibrationTimer); } }); dispatch_resume(self.vibrationTimer); } else { self.contentHandler(self.bestAttemptContent); } } else { self.contentHandler(self.bestAttemptContent); }

最新回复 (0)
返回