You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
4.4 KiB

2 years ago
//
// NotificationService.m
// TXNotificationSerExtension
//
// Created by Apple on 2019/12/19.
// Copyright © 2019 xTT. All rights reserved.
//
#import "NotificationService.h"
//用于消息震动
#import <AudioToolbox/AudioToolbox.h>
#import <UserNotifications/UserNotifications.h>
#import <UIKit/UIKit.h>
@interface NotificationService ()
@property (nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver);
@property (nonatomic, strong) UNMutableNotificationContent *bestAttemptContent;
@end
@implementation NotificationService
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
self.contentHandler = contentHandler;
self.bestAttemptContent = [request.content mutableCopy];
/* 视频推送的
{
"data": {
"badge": 0,
"message": {
"wait_time": 60,
"type": "video",
"uuid": "8AEA9A75-8623-4C96-A4D3-A21522EA86D0",
"device_username": "imei-587039092042812",
"video_type": 4,
"limit_time": 300,
"password": "042812",
"distinguishability_heiger": 160,
"imei": "587039092042812",
"appkey": "48afd90860af02059c6b5096",
"distinguishability_width": 120,
"username": "8AEA9A75-8623-4C96-A4D3-A21522EA86D0",
"timestamp": 1576747673.18447,
"video_id": "201912191727531843999910110145610"
},
"type": "accountMessage"
},
"aps": {
"badge": 0,
"mutable-content": 1,
"alert": {
"body": "设备:587039092042812邀请您通话"
},
"sound": "videoring.caf"
},
"video": 1
}
*/
// Modify the notification content here...
NSDictionary *dic = self.bestAttemptContent.userInfo;
NSDictionary *data = [dic valueForKey:@"data"];
if(data){
NSDictionary *message = [data valueForKey:@"message"];
if(message){
//MARK: 这个线程不会清除掉,会保留的,多个推送会激活同一个主线程
@try {
NSString *type = [NSString stringWithFormat:@"%@",[message valueForKey:@"type"]];
if([type isEqualToString:@"video"]){
NSUserDefaults* userDefault = [[NSUserDefaults alloc] initWithSuiteName:@"group.tongxin"];
if([userDefault valueForKey:@"APPStatus"]){
//APP活着的状态
self.bestAttemptContent.sound = [UNNotificationSound defaultSound];
self.contentHandler(self.bestAttemptContent);
return;
}else{
//self.bestAttemptContent.title = @"APP 死了";
if([[UIDevice currentDevice] systemVersion].doubleValue >= 13){
self.bestAttemptContent.title = self.bestAttemptContent.body;
self.bestAttemptContent.sound = [UNNotificationSound soundNamed:@"videoring.caf"];
[self playVibration];
}
}
}
} @catch (NSException *exception) {
self.bestAttemptContent.body = [NSString stringWithFormat:@"错误 %@",exception.description];
} @finally {
self.contentHandler(self.bestAttemptContent);
}
}
}
self.contentHandler(self.bestAttemptContent);
}
-(void)playVibration{
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);//执行震动
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self playVibration];
});
}
- (void)serviceExtensionTimeWillExpire {
// Called just before the extension will be terminated by the system.
// Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
//如果您的方法花很长时间执行其完成块,则系统会在 单独的线程 上调用此方法,使您有最后机会执行该块。
self.contentHandler(self.bestAttemptContent);
}
@end