iOS10全新推送功能实现代码

 更新时间:2016年09月19日 14:06:57   作者:u012847940  
这篇文章主要为大家详细介绍了iOS10全新推送功能实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

从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内存管理Tagged Pointer使用原理详解

    iOS内存管理Tagged Pointer使用原理详解

    这篇文章主要为大家介绍了iOS内存管理Tagged Pointer使用原理详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-01-01
  • iOS开发常用线程安全锁

    iOS开发常用线程安全锁

    这篇文章主要为大家介绍了iOS开发常用线程安全锁示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-07-07
  • iOS 捕获程序崩溃日志

    iOS 捕获程序崩溃日志

    我们常常会遇到iPhone手机或者iPad平板上运行APP崩溃的问题,有时候打开某个APP,却一下子“闪退”了。对于开发者来说,这个绝对是头疼的问题。那么如何获取到iOS设备崩溃日志呢?这个提供一些简单的方法,共开发者与用户沟通使用。
    2015-06-06
  • IOS 单击手势的添加实现代码

    IOS 单击手势的添加实现代码

    这篇文章主要介绍了IOS 单击手势的添加实现代码的相关资料,需要的朋友可以参考下
    2017-05-05
  • IOS UIView的生命周期的实例详解

    IOS UIView的生命周期的实例详解

    这篇文章主要介绍了IOS UIView的生命周期的实例详解的相关资料,希望通过本文大家能掌握理解这部分内容,需要的朋友可以参考下
    2017-09-09
  • iOS购物分类模块的实现方案

    iOS购物分类模块的实现方案

    这篇文章主要为大家详细介绍了iOS购物分类模块的实现方案,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-02-02
  • IOS判断字符串是否有空格实例

    IOS判断字符串是否有空格实例

    在我们大家日常开发的时候,经常会需要对注册,登录,忘记密码等功能的密码进行判断是否包含空格,下面这篇文章给大家分享了自己封装的一个方法,有需要的可以参考借鉴。
    2016-09-09
  • IOS开发中键盘输入屏幕上移的解决方法

    IOS开发中键盘输入屏幕上移的解决方法

    在IOS开法中经常会遇到键盘遮挡屏幕的事情,经常档住下面的按钮,下面小编给大家分享IOS开发中键盘输入屏幕上移的解决方法,感兴趣的朋友一起看看吧
    2016-10-10
  • IOS提醒用户重新授权打开定位功能

    IOS提醒用户重新授权打开定位功能

    这篇文章主要介绍了IOS提醒用户重新授权打开定位功能的相关资料,需要的朋友可以参考下
    2015-12-12
  • iOS视频压缩存储至本地并上传至服务器实例代码

    iOS视频压缩存储至本地并上传至服务器实例代码

    本篇文章主要介绍了iOS视频压缩存储至本地并上传至服务器实例代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-04-04

最新评论