165 lines
7.4 KiB
Objective-C
165 lines
7.4 KiB
Objective-C
//
|
||
// GiGaLocalNotificationManager.m
|
||
// GIGA
|
||
//
|
||
// Created by lianxiang on 2018/8/24.
|
||
// Copyright © 2018年 com.giga.ios. All rights reserved.
|
||
//
|
||
|
||
#import "GiGaLocalNotificationManager.h"
|
||
|
||
@implementation GiGaLocalNotificationManager
|
||
|
||
+(GiGaLocalNotificationManager*)localNotifiationCenter{
|
||
static GiGaLocalNotificationManager *center = nil;
|
||
static dispatch_once_t once;
|
||
dispatch_once(&once, ^{
|
||
center = [[self alloc] init];
|
||
});
|
||
return center;
|
||
}
|
||
|
||
-(void)sendLocalNotification:(NSString *)alertBoday fireTimeInterval:(NSTimeInterval )timeInterval alertAction:(NSString *)alertAction withIdentifier:(NSString *)identifier{
|
||
|
||
if (@available(iOS 10.0, *)) {
|
||
|
||
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
|
||
center.delegate = self;
|
||
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
|
||
content.title = alertAction;
|
||
content.body = alertBoday;
|
||
//content.sound = [UNNotificationSound defaultSound];
|
||
content.sound = [UNNotificationSound soundNamed:@"4481.wav"];
|
||
//content.sound = UNNotificationSound
|
||
//通知附件 音<10M p3 p4 ,视频<50M pmeg mpeg4 图片<5M
|
||
|
||
NSURL *imageUrl = [[NSBundle mainBundle] URLForResource:@"MaskTime" withExtension:@"png"];
|
||
UNNotificationAttachment *attach = [UNNotificationAttachment attachmentWithIdentifier:@"photo" URL:imageUrl options:nil error:nil];
|
||
|
||
NSURL *audioUrl = [[NSBundle mainBundle] URLForResource:@"4481" withExtension:@"wav"];
|
||
UNNotificationAttachment *attachAudio = [UNNotificationAttachment attachmentWithIdentifier:@"audio" URL:audioUrl options:nil error:nil];
|
||
|
||
NSURL *vedioUrl = [[NSBundle mainBundle] URLForResource:@"emojizone" withExtension:@"mp4"];
|
||
UNNotificationAttachment *vedioAudio = [UNNotificationAttachment attachmentWithIdentifier:@"vedio" URL:vedioUrl options:nil error:nil];
|
||
content.attachments = @[attachAudio];
|
||
|
||
//延迟通知 第一个参数是重复的时间间隔,最小60s,第二个参数是是否重复。
|
||
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:timeInterval repeats:NO];
|
||
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];
|
||
[center addNotificationRequest:request withCompletionHandler:^(NSError *_Nullable error) {
|
||
GILog(@"成功添加推送");
|
||
}];
|
||
|
||
} else {
|
||
// Fallback on earlier versions
|
||
|
||
//ios8
|
||
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
|
||
localNotification.alertBody = alertBoday;
|
||
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:timeInterval];
|
||
localNotification.alertAction = alertAction;
|
||
localNotification.applicationIconBadgeNumber = 1;
|
||
localNotification.soundName = UILocalNotificationDefaultSoundName;
|
||
localNotification.soundName = @"4481.wav";
|
||
//localNotification.alertLaunchImage = @"MaskTime.png";
|
||
localNotification.userInfo = @{identifier:identifier};
|
||
//根据设定时间发送通知
|
||
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
|
||
|
||
}
|
||
}
|
||
|
||
-(void)cancelAllLocalNoitification
|
||
{
|
||
|
||
if (@available(iOS 10.0, *)) {
|
||
|
||
[[UNUserNotificationCenter currentNotificationCenter] removeAllDeliveredNotifications];
|
||
|
||
} else {
|
||
// Fallback on earlier versions
|
||
[[UIApplication sharedApplication] cancelAllLocalNotifications];
|
||
}
|
||
|
||
|
||
}
|
||
|
||
-(void)cancelLocalNitificationByUserInfowithIdentifier:(NSString *)identifier{
|
||
|
||
if (@available(iOS 10.0, *)) {
|
||
[[UNUserNotificationCenter currentNotificationCenter] removeDeliveredNotificationsWithIdentifiers:@[identifier]];
|
||
} else {
|
||
// Fallback on earlier versions
|
||
NSArray *notifiArray = [[UIApplication sharedApplication] scheduledLocalNotifications];
|
||
for (UILocalNotification *local in notifiArray) {
|
||
//将来可以根据UserInfo的值,来查看这个是否是你想要删除的通知
|
||
if (local.userInfo) {
|
||
//删除单个通知
|
||
NSDictionary *useinfo = local.userInfo;
|
||
NSString *timefalg = useinfo[identifier];
|
||
if ([timefalg isEqualToString:identifier] ) {
|
||
[[UIApplication sharedApplication]cancelLocalNotification:local];
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
//将通知传递给前台运行的app
|
||
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
|
||
API_AVAILABLE(ios(10.0)){
|
||
|
||
NSDictionary * userInfo = notification.request.content.userInfo;
|
||
UNNotificationRequest *request = notification.request; // 收到推送的请求
|
||
UNNotificationContent *content = request.content; // 收到推送的消息内容
|
||
NSNumber *badge = content.badge; // 推送消息的角标
|
||
NSString *body = content.body; // 推送消息体
|
||
UNNotificationSound *sound = content.sound; // 推送消息的声音
|
||
NSString *subtitle = content.subtitle; // 推送消息的副标题
|
||
NSString *title = content.title; // 推送消息的标题
|
||
|
||
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
|
||
// 远程推送通知在AppDelegate+ThirdParty 中处理
|
||
NSLog(@"iOS10 前台 收到远程通知:%@", body);
|
||
|
||
} else {
|
||
// 判断为本地通知
|
||
NSLog(@"iOS10 前台 收到本地通知:{\\\\nbody:%@,\\\\ntitle:%@,\\\\nsubtitle:%@,\\\\nbadge:%@,\\\\nsound:%@,\\\\nuserInfo:%@\\\\n}",body,title,subtitle,badge,sound,userInfo);
|
||
|
||
[self showAlert:title message:body];
|
||
[center removeAllPendingNotificationRequests];
|
||
}
|
||
completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert);
|
||
}
|
||
|
||
//将用户对通知响应结果告诉app
|
||
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler
|
||
API_AVAILABLE(ios(10.0)){
|
||
|
||
GILog(@"ios10用户点击通知栏相应");
|
||
[center removeAllPendingNotificationRequests];
|
||
completionHandler();
|
||
}
|
||
//
|
||
-(void)didResaveloaclNitification:(UILocalNotification *)localNitification
|
||
{
|
||
//NSDictionary * userInfo = localNitification.userInfo;
|
||
|
||
NSString *title = localNitification.alertTitle;
|
||
NSString *message = localNitification.alertBody;
|
||
[self showAlert:title message:message];
|
||
GILog(@"用户点击通知栏相应");
|
||
|
||
}
|
||
|
||
-(void)showAlert:(NSString *)title message:(NSString *)message
|
||
{
|
||
[[UIApplication sharedApplication].keyWindow.rootViewController jxt_showAlertWithTitle:title message:message appearanceProcess:^(JXTAlertController * _Nonnull alertMaker) {
|
||
alertMaker.addActionCancelTitle(@"知道了");
|
||
|
||
} actionsBlock:^(NSInteger buttonIndex, UIAlertAction * _Nonnull action, JXTAlertController * _Nonnull alertSelf) {
|
||
|
||
}];
|
||
}
|
||
|
||
@end
|