ad localnoti background music
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
//
|
||||
// GiGaLocalNotificationManager.h
|
||||
// GIGA
|
||||
//
|
||||
// Created by lianxiang on 2018/8/24.
|
||||
// Copyright © 2018年 com.giga.ios. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <UserNotifications/UserNotifications.h>
|
||||
|
||||
@interface GiGaLocalNotificationManager : NSObject<UNUserNotificationCenterDelegate>
|
||||
|
||||
/**
|
||||
本地推送管理中心
|
||||
*/
|
||||
+(GiGaLocalNotificationManager*)localNotifiationCenter;
|
||||
|
||||
/**
|
||||
发送本地通知
|
||||
ios 8 后 iOS10 前
|
||||
@param alertBoday 通知显示内容
|
||||
@param timeInterval 设置通知发送时间,单位秒
|
||||
@param alertAction 解锁滑动时事件
|
||||
@param identifier ios 10 即是Identifier iOS8 是userInfo key value 值
|
||||
*/
|
||||
-(void)sendLocalNotification:(NSString *)alertBoday fireTimeInterval:(NSTimeInterval )timeInterval alertAction:(NSString *)alertAction withIdentifier:(NSString *)identifier;
|
||||
|
||||
/**
|
||||
删除当前程序注册的所有通知 ios 8 后 iOS10 前
|
||||
*/
|
||||
-(void)cancelAllLocalNoitification;
|
||||
|
||||
/**
|
||||
删除指定的通知,一般用于取消重复的通知或者还没有被调用的通知,先获取通知,再遍历根据条件去删除(条件是 UserInfo 的值,是发送通知时所携带的参数) ios 8 后 iOS10 前
|
||||
*/
|
||||
-(void)cancelLocalNitificationByUserInfowithIdentifier:(NSString *)identifier;
|
||||
|
||||
/**
|
||||
iOS 8 收到本地通知 iOS10通过 UNUserNotificationCenterDelegate实现
|
||||
*/
|
||||
-(void)didResaveloaclNitification:(UILocalNotification *)localNitification;
|
||||
|
||||
|
||||
//处理通知。。
|
||||
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,164 @@
|
||||
//
|
||||
// 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
|
||||
//通知附件 音<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:@"pomodoSound" withExtension:@"m4a"];
|
||||
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 = @[attach,attachAudio,vedioAudio];
|
||||
|
||||
|
||||
//延迟通知 第一个参数是重复的时间间隔,最小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.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];
|
||||
}
|
||||
completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert);
|
||||
}
|
||||
|
||||
//将用户对通知响应结果告诉app
|
||||
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler
|
||||
API_AVAILABLE(ios(10.0)){
|
||||
|
||||
GILog(@"ios10用户点击通知栏相应");
|
||||
|
||||
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
|
||||
Reference in New Issue
Block a user