iOS10全新推送功能实现代码_IOS

从iOS8.0开始推送功能的实现在不断改变,功能也在不断增加,iOS10又出来了一个推送插件的开发(见最后图),废话不多说直接上代码: 

#import <UserNotifications/UserNotifications.h>

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 // Override point for customization after application launch.

 /* APP未启动,点击推送消息的情况下 iOS10遗弃UIApplicationLaunchOptionsLocalNotificationKey,使用代理UNUserNotificationCenterDelegate方法didReceiveNotificationResponse:withCompletionHandler:获取本地推送
 */
// NSDictionary *localUserInfo = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
// if (localUserInfo) {
// NSLog(@"localUserInfo:%@",localUserInfo);
// //APP未启动,点击推送消息
// }
 NSDictionary *remoteUserInfo = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
 if (remoteUserInfo) {
 NSLog(@"remoteUserInfo:%@",remoteUserInfo);
 //APP未启动,点击推送消息,iOS10下还是跟以前一样在此获取
 }
 [self registerNotification];
 return YES;
}

 

注册推送方法的改变:

新增库 #import <UserNotifications/UserNotifications.h>  推送单列UNUserNotificationCenter 等API 

- (void)registerNotification{
 /*
 identifier:行为标识符,用于调用代理方法时识别是哪种行为。
 title:行为名称。
 UIUserNotificationActivationMode:即行为是否打开APP。
 authenticationRequired:是否需要解锁。
 destructive:这个决定按钮显示颜色,YES的话按钮会是红色。
 behavior:点击按钮文字输入,是否弹出键盘
 */
 UNNotificationAction *action1 = [UNNotificationAction actionWithIdentifier:@"action1" title:@"策略1行为1" options:UNNotificationActionOptionForeground];
 /*iOS9实现方法
 UIMutableUserNotificationAction * action1 = [[UIMutableUserNotificationAction alloc] init];
 action1.identifier = @"action1";
 action1.title=@"策略1行为1";
 action1.activationMode = UIUserNotificationActivationModeForeground;
 action1.destructive = YES;
 */

 UNTextInputNotificationAction *action2 = [UNTextInputNotificationAction actionWithIdentifier:@"action2" title:@"策略1行为2" options:UNNotificationActionOptionDestructive textInputButtonTitle:@"textInputButtonTitle" textInputPlaceholder:@"textInputPlaceholder"];
 /*iOS9实现方法
 UIMutableUserNotificationAction * action2 = [[UIMutableUserNotificationAction alloc] init];
 action2.identifier = @"action2";
 action2.title=@"策略1行为2";
 action2.activationMode = UIUserNotificationActivationModeBackground;
 action2.authenticationRequired = NO;
 action2.destructive = NO;
 action2.behavior = UIUserNotificationActionBehaviorTextInput;//点击按钮文字输入,是否弹出键盘
 */

 UNNotificationCategory *category1 = [UNNotificationCategory categoryWithIdentifier:@"Category1" actions:@[action2,action1] minimalActions:@[action2,action1] intentIdentifiers:@[@"action1",@"action2"] options:UNNotificationCategoryOptionCustomDismissAction];
 // UIMutableUserNotificationCategory * category1 = [[UIMutableUserNotificationCategory alloc] init];
 // category1.identifier = @"Category1";
 // [category1 setActions:@[action2,action1] forContext:(UIUserNotificationActionContextDefault)];

 UNNotificationAction *action3 = [UNNotificationAction actionWithIdentifier:@"action3" title:@"策略2行为1" options:UNNotificationActionOptionForeground];
 // UIMutableUserNotificationAction * action3 = [[UIMutableUserNotificationAction alloc] init];
 // action3.identifier = @"action3";
 // action3.title=@"策略2行为1";
 // action3.activationMode = UIUserNotificationActivationModeForeground;
 // action3.destructive = YES;

 UNNotificationAction *action4 = [UNNotificationAction actionWithIdentifier:@"action4" title:@"策略2行为2" options:UNNotificationActionOptionForeground];
 // UIMutableUserNotificationAction * action4 = [[UIMutableUserNotificationAction alloc] init];
 // action4.identifier = @"action4";
 // action4.title=@"策略2行为2";
 // action4.activationMode = UIUserNotificationActivationModeBackground;
 // action4.authenticationRequired = NO;
 // action4.destructive = NO;

 UNNotificationCategory *category2 = [UNNotificationCategory categoryWithIdentifier:@"Category2" actions:@[action3,action4] minimalActions:@[action3,action4] intentIdentifiers:@[@"action3",@"action4"] options:UNNotificationCategoryOptionCustomDismissAction];
 // UIMutableUserNotificationCategory * category2 = [[UIMutableUserNotificationCategory alloc] init];
 // category2.identifier = @"Category2";
 // [category2 setActions:@[action4,action3] forContext:(UIUserNotificationActionContextDefault)];

 [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObjects:category1,category2, nil]];
 [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert completionHandler:^(BOOL granted, NSError * _Nullable error) {
 NSLog(@"completionHandler");
 }];
 /*iOS9实现方法
 UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound) categories:[NSSet setWithObjects: category1,category2, nil]];

 [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
 */
 [[UIApplication sharedApplication] registerForRemoteNotifications];

 [UNUserNotificationCenter currentNotificationCenter].delegate = self;
}

代理方法的改变:

 一些本地和远程推送的回调放在了同一个代理方法

#pragma mark -

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings NS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED{
 NSLog(@"didRegisterUserNotificationSettings");
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken NS_AVAILABLE_IOS(3_0){
 NSLog(@"deviceToken:%@",deviceToken);
 NSString *deviceTokenSt = [[[[deviceToken description]
   stringByReplacingOccurrencesOfString:@"<" withString:@""]
  stringByReplacingOccurrencesOfString:@">" withString:@""]
  stringByReplacingOccurrencesOfString:@" " withString:@""];
 NSLog(@"deviceTokenSt:%@",deviceTokenSt);
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error NS_AVAILABLE_IOS(3_0){
 NSLog(@"didFailToRegisterForRemoteNotificationsWithError:%@",error);
}

/*iOS9使用方法
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo NS_DEPRECATED_IOS(3_0, 10_0, "Use UserNotifications Framework's -[UNUserNotificationCenterDelegate willPresentNotification:withCompletionHandler:] or -[UNUserNotificationCenterDelegate didReceiveNotificationResponse:withCompletionHandler:] for user visible notifications and -[UIApplicationDelegate application:didReceiveRemoteNotification:fetchCompletionHandler:] for silent remote notifications"){

}
*/

- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
 NSLog(@"willPresentNotification:%@",notification.request.content.title);

 // 这里真实需要处理交互的地方
 // 获取通知所带的数据
 NSString *notMess = [notification.request.content.userInfo objectForKey:@"aps"];

}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{
 //在没有启动本App时,收到服务器推送消息,下拉消息会有快捷回复的按钮,点击按钮后调用的方法,根据identifier来判断点击的哪个按钮
 NSString *notMess = [response.notification.request.content.userInfo objectForKey:@"aps"];
 NSLog(@"didReceiveNotificationResponse:%@",response.notification.request.content.title);
// response.notification.request.identifier
}

//远程推送APP在前台
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
 NSLog(@"didReceiveRemoteNotification:%@",userInfo);
}

/*
- (void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler NS_DEPRECATED_IOS(8_0, 10_0, "Use UserNotifications Framework's -[UNUserNotificationCenterDelegate didReceiveNotificationResponse:withCompletionHandler:]") __TVOS_PROHIBITED
{

}
*/
/*
// 本地通知回调函数,当应用程序在前台时调用
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification NS_DEPRECATED_IOS(4_0, 10_0, "Use UserNotifications Framework's -[UNUserNotificationCenterDelegate willPresentNotification:withCompletionHandler:] or -[UNUserNotificationCenterDelegate didReceiveNotificationResponse:withCompletionHandler:]") __TVOS_PROHIBITED{
 NSLog(@"didReceiveLocalNotification:%@",notification.userInfo);

 // 这里真实需要处理交互的地方
 // 获取通知所带的数据
 NSString *notMess = [notification.userInfo objectForKey:@"aps"];
 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"本地通知(前台)"
    message:notMess
    delegate:nil
   cancelButtonTitle:@"OK"
   otherButtonTitles:nil];
 [alert show];

 // 更新显示的徽章个数
 NSInteger badge = [UIApplication sharedApplication].applicationIconBadgeNumber;
 badge--;
 badge = badge >= 0 ? badge : 0;
 [UIApplication sharedApplication].applicationIconBadgeNumber = badge;

 // 在不需要再推送时,可以取消推送
 [FirstViewController cancelLocalNotificationWithKey:@"key"];

}

- (void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)())completionHandler NS_DEPRECATED_IOS(8_0, 10_0, "Use UserNotifications Framework's -[UNUserNotificationCenterDelegate didReceiveNotificationResponse:withCompletionHandler:]") __TVOS_PROHIBITED
{
 //在非本App界面时收到本地消息,下拉消息会有快捷回复的按钮,点击按钮后调用的方法,根据identifier来判断点击的哪个按钮,notification为消息内容
 NSLog(@"%@----%@",identifier,notification);
 completionHandler();//处理完消息,最后一定要调用这个代码块
}
*/

 还有推送插件开发: 类似iOS tody widget插件开发

 

本文已被整理到了《iOS推送教程》,欢迎大家学习阅读。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索iOS远程推送
, iOS10全新推送功能
iOS10推送功能
推送功能实现、android推送实现代码、ios推送功能实现、android推送功能实现、app推送功能实现,以便于您获取更多的相关知识。

时间: 2024-09-18 01:12:55

iOS10全新推送功能实现代码_IOS的相关文章

iOS10实现推送功能时的注意点和问题总结_IOS

1.在项目 target 中,打开Capabilitie -> Push Notifications,并会自动在项目中生成 .entitlement 文件.(很多同学升级后,获取不到 deviceToken,大概率是由于没开这个选项) Capabilitie -> Push Notifications 自动生成 .entitlement 2.确保添加了 UserNotifications.framework,并 import到 AppDelegate,记得实现 UNUserNotificati

iOS本地推送简单实现代码_IOS

本文为大家分解介绍了iOS本地推送代码的三步骤,供大家参考,具体内容如下 第一步:创建本地推送 // 创建一个本地推送 UILocalNotification *notification = [[[UILocalNotification alloc] init] autorelease]; //设置10秒之后 NSDate *pushDate = [NSDate dateWithTimeIntervalSinceNow:10]; if (notification != nil) { // 设置推

解析iOS10中的极光推送消息的适配_IOS

iOS10发布后,发现项目中的极光推送接收消息异常了. 查了相关资料后才发现,iOS10中对于通知做了不少改变.同时也发现极光也很快更新了对应的SDK. 现在就把适配修改的做法分享一下,希望对有需要的童鞋有所帮助. 具体做法如下: 注意:必须先安装Xcode8.0版本. 一.添加相关的SKD,或framework文件 1.添加UserNotification.framework 2.更新jpush的SDK(最新版本:jpush-ios-2.1.9.a)https://www.jiguang.cn

iOS10推送教程详解_IOS

上个月接到一个需求,做ios10的推送,意图冲击AppStore头条.瞬间抓狂,工具都还没有,于是赶紧安装xcodeBeta版,ios10Beta版,然后就开始无尽的查资料,毕竟新功能,毕竟没做过........不过还好,在发布会之前赶出来了,由于本人比较懒,拖到现在才写出来,接下来就是见证奇迹的时刻! 原理 图中,Provider是指某个iPhone软件的Push服务器,这篇文章我将使用.net作为Provider. APNS 是Apple Push Notification Service(

iPhone/iPad开发通过LocalNotification实现iOS定时本地推送功能_IOS

通过iOS的UILocalNotification Class可以实现本地app的定时推送功能,即使当前app是后台关闭状态.  可以实现诸如,设置app badgenum,弹出一个alert,播放声音等等,实现很简单  UILocalNotification *notification=[[UILocalNotification alloc] init]; if (notification!=nil) { NSDate *now=[NSDate new]; notification.fireD

iOS10 推送最新特性研究_IOS

最近在研究iOS10关于推送的新特性, 相比之前确实做了很大的改变,总结起来主要是以下几点:  1.推送内容更加丰富,由之前的alert 到现在的title, subtitle, body  2.推送统一由trigger触发  3.可以为推送增加附件,如图片.音频.视频,这就使推送内容更加丰富多彩  4.可以方便的更新推送内容  import 新框架 添加新的框架 UserNotifications.framework   #import <UserNotifications/UserNotif

iOS推送的那些事_IOS

直接切入主题,讲讲如何模拟推送以及处理推送消息.在进入主题之前,我先说几个关键流程: 1.建Push SSL Certification(推送证书) 2.OS客户端注册Push功能并获得DeviceToken 3.用Provider向APNS发送Push消息 4.OS客户端接收处理由APNS发来的消息 推送流程图: Provider:就是为指定iOS设备应用程序提供Push的服务器.如果iOS设备的应用程序是客户端的话,那么Provider可以理解为服务端(推送消息的发起者)APNs:Apple

iOS自定义推送消息提示框_IOS

看到标题你可能会觉得奇怪 推送消息提示框不是系统自己弹出来的吗? 为什么还要自己自定义呢?  因为项目需求是这样的:最近需要做 远程推送通知 和一个客服系统 包括店铺客服和官方客服两个模块 如果有新的消息推送的时候 如果用户当前不在客服界面的时候  要求无论是在app前台 还是app退到后台 顶部都要弹出系统的那种消息提示框 这样的需求 我们就只能自定义一个在app内 弹出消息提示框   实现步骤如下:  1.我们自定义一个view 为 STPushView 推送消息的提示框view  #imp

html5利用websocket完成的推送功能(tomcat)

插播一条消息,5天后会删掉的 本人东北大学软件学院大三学生,现在正在寻找实习,qq:1021842556 利用websocket和java完成的消息推送功能,服务器用的是tomcat7.0,一些东西是自己琢磨的,也不知道恰不恰当,不恰当处,还请各位见谅,并指出. 程序简单来说,就是客户A可以发送消息给客户B,但有很多可以扩展的地方, 比如 1.如果加入数据库后,A发消息时客户B未上线,服务端将会把消息存在数据库中,等客户B上线后,在将消息取出发送给客户B 2.服务端也可发送消息到任意客户端上.