unity项目IOS本地推送消息
unity项目ios客户端实现本地消息推送,需求游戏在后台运行的时候,推送奖励已满。
实现。
1.注册通知:
在application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions中调用注册通知函数registerAPN
- (void)RegisterAPN {
// 注册通知
if (@available(iOS 10.0, *)) { // iOS10 以上
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!error) {
NSLog(@"推送注册成功");
}
}];
} else {// iOS8.0 以上
UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:setting];
}
}
2.检查权限
检查app是否开启通知权限
- (void)CheckUserNotificationEnable{ // 判断用户是否允许接收通知
if (@available(iOS 10.0, *)) {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
if (settings.notificationCenterSetting == UNNotificationSettingEnabled) {
NSLog(@"打开了通知");
if ([PlatformSDKManager Instance].sdkHandler.CustomEvent)
{
[PlatformSDKManager Instance].sdkHandler.CustomEvent("Callback_CheckNotifyEnable:true");
}
}else {
NSLog(@"关闭了通知");
dispatch_sync(dispatch_get_main_queue(), ^{
//刷新UI的代码放到主线程执行
[self ShowAlertView];
});
}
}];
}else {
if ([[UIApplication sharedApplication] currentUserNotificationSettings].types == UIUserNotificationTypeNone){
NSLog(@"关闭了通知");
dispatch_sync(dispatch_get_main_queue(), ^{
[self ShowAlertView];
});
}else {
NSLog(@"打开了通知");
}
}
}
3.添加通知
在需要使用的地方调用这个函数,奖励满足时候,通过lua调用这个函数
- (void)AddLocalNoticeWithID:(NSString *)noticeId Title:(NSString*)title Context:(NSString*)context Time:(int)time{
if (@available(iOS 10.0, *)) {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
// 标题
content.title = title;
//content.subtitle = [dataDict objectForKey:@"context"];
// 内容
content.body = context;
// 声音
content.sound = [UNNotificationSound defaultSound];
content.badge = @1;
int secTime = time;
if(secTime <= 0)
{
return;
}
NSString *identifier = noticeId;
NSTimeInterval time = [[NSDate dateWithTimeIntervalSinceNow:secTime] timeIntervalSinceNow];
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:time repeats:NO];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];
[center addNotificationRequest:request withCompletionHandler:^(NSError *_Nullable error) {
NSLog(@"成功添加推送");
}];
}
4.移除通知
// 移除某一个指定的通知
- (void)RemoveOneNotificationWithID:(NSString *)noticeId {
if (@available(iOS 10.0, *)) {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center getPendingNotificationRequestsWithCompletionHandler:^(NSArray<UNNotificationRequest *> * _Nonnull requests) {
for (UNNotificationRequest *req in requests){
NSLog(@"存在的ID:%@\n",req.identifier);
}
NSLog(@"移除currentID:%@",noticeId);
}];
[center removePendingNotificationRequestsWithIdentifiers:@[noticeId]];
}else {
NSArray *array=[[UIApplication sharedApplication] scheduledLocalNotifications];
for (UILocalNotification *localNotification in array){
NSDictionary *userInfo = localNotification.userInfo;
NSString *obj = [userInfo objectForKey:@"noticeId"];
if ([obj isEqualToString:noticeId]) {
[[UIApplication sharedApplication] cancelLocalNotification:localNotification];
}
}
}
}
// 移除所有通知
- (void)RemoveAllNotification {
if (@available(iOS 10.0, *)) {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center removeAllPendingNotificationRequests];
}else {
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}
}
5.跳转到设置界面
// 如果用户关闭了接收通知功能,该方法可以跳转到APP设置页面进行修改
- (void)GoToAppSystemSetting {
dispatch_async(dispatch_get_main_queue(), ^{
UIApplication *application = [UIApplication sharedApplication];
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if ([application canOpenURL:url]) {
if (@available(iOS 10.0, *)) {
if ([application respondsToSelector:@selector(openURL:options:completionHandler:)]) {
[application openURL:url options:@{} completionHandler:nil];
}
}else {
[application openURL:url];
}
}
});
}
其中在customAppcontroller(定制UnityAppcontroller)实现<UNUserNotificationCenterDelegate>代理,并且实现下面两个方法
#pragma mark - iOS10-UNUserNotificationCenterDelegate
// 弹出通知
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
{
//当游戏处于前台时候,本地推送的设置 ,此处可设置不显示本地推送
completionHandler(UNNotificationPresentationOptionSound);
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler
{
//当点击本地推送横栏时,进行的操作,这里是把角标红点进行置空
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}
差不多就是这些
参考:https://www.jianshu.com/p/9b1fa25a0712

浙公网安备 33010602011771号