iOS实践iOS常用功能和UI效果的实现

iOS 消息推送(UserNotifications)

2017-05-17  本文已影响46人  微笑城_cbe9

iOS 消息推送 

之前没怎么关注 这块 今天要做消息推送  就做个记录 也算是知识的总结啦 

理一理头绪  我们要做这件事 要哪些东西 

1.请求证书 

2.Xcode 打开 接受通知机制 (下面会配图的)

3.NWPusher (测试用的)

其他的兴趣 你们自己了解 我只介绍如何实现消息推送

一 开始下载配置证书

https://developer.apple.com/account/ios/certificate/  (直接进入 证书界面)

上面写的明白 检查一下自己要开发的App ID 在不在里面 

如果没有 自己添加一个 这里也是填写你要用到的id 必须勾选上

创建完成之后  回到刚才的页面 点击刚才创建的 ID 

没有配置证书都是 黄色的  点击edit 进入  勾选上

我们开始请求证书  首先要打开钥匙串🔑

钥匙串访问 -> 证书助理 -> 从证书颁发机构请求证书..

邮箱我瞎写的 忽略 知道放到哪里去了 直接保存就好了

好了 我们继续生成开发环境下的证书

一直点击下一步 你会看到 选择你刚才创建的文件  生成后 直接下载 双击安装就好了 可以在钥匙串里面找到 

相同的方法 请求发布版的证书 这里我就不多重复了

在Keychain Access.app里选定这个新证书(Apple Development Push Services*),导出到桌面,保存为Certificates.p12.

终端上执行 打包证书

openssl pkcs12 -clcerts -nokeys -out cert.pem -in Certificates.p12

openssl pkcs12 -nocerts -out key.pem -in Certificates.p12

openssl rsa -in key.pem -out key.unencrypted.pem

cat cert.pem key.unencrypted.pem > ck.pem

下载 NWPusher 

使用界面就是这样的 很简单,但是对应的正式不要搞错了 下面的token 是苹果返回的 

看代码

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(nonnull NSData *)deviceToken

{

NSLog(@"deviceToken:%@",deviceToken);

NSString *deviceTokenStr = [[[[deviceToken description]

stringByReplacingOccurrencesOfString:@"<" withString:@""]

stringByReplacingOccurrencesOfString:@">" withString:@""]

stringByReplacingOccurrencesOfString:@" " withString:@""];

NSLog(@"deviceTokenStr:%@",deviceTokenStr);

}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{

NSLog(@"didFailToRegisterForRemoteNotificationsWithError:%@",error);

}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{

NSLog(@"willPresentNotification:%@",notification.request.content.title);

// 这里真实需要处理交互的地方

// 获取通知所带的数据

NSString *apsContent = [notification.request.content.userInfo objectForKey:@"aps"];

NSLog(@"%@",apsContent);

}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{

//在没有启动本App时,收到服务器推送消息,下拉消息会有快捷回复的按钮,点击按钮后调用的方法,根据identifier来判断点击的哪个按钮

NSString *apsContent = [response.notification.request.content.userInfo objectForKey:@"aps"];

NSLog(@"didReceiveNotificationResponse:%@",response.notification.request.content.title);

NSLog(@"%@",apsContent);

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];

}

//远程推送APP在前台

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{

NSLog(@"didReceiveRemoteNotification:%@",userInfo);

}

- (void)setUpCategory

{

UNNotificationAction *action1 = [UNNotificationAction actionWithIdentifier:@"enterApp" title:@"进入应用" options:UNNotificationActionOptionForeground];

UNNotificationAction *action2 = [UNNotificationAction actionWithIdentifier:@"destructive" title:@"忽略" options:UNNotificationActionOptionDestructive];

//UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"helloIdentifier" actions:@[action1,action2] minimalActions:@[action1,action2] intentIdentifiers:@[] options:UNNotificationCategoryOptionNone];

UNNotificationCategory *caregory = [UNNotificationCategory categoryWithIdentifier:@"helloIdentifier" actions:@[action1,action2] intentIdentifiers:@[action1,action2] options:UNNotificationCategoryOptionNone];

[[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObjects:caregory, nil]];

}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// Override point for customization after application launch.

// iOS10 下需要使用新的 API

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10.0) {

#ifdef NSFoundationVersionNumber_iOS_9_x_Max

UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];

[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound + UNAuthorizationOptionBadge)

completionHandler:^(BOOL granted, NSError * _Nullable error) {

// Enable or disable features based on authorization.

if (granted) {

[application registerForRemoteNotifications];

}

}];

center.delegate = self;

#endif

}

else if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {

UIUserNotificationType myTypes = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;

UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:myTypes categories:nil];

[[UIApplication sharedApplication] registerUserNotificationSettings:settings];

}

NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

if (userInfo) {

NSLog(@"从消息启动:%@",userInfo);

//        [BPush handleNotification:userInfo];

}

//角标清0

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];

return YES;

}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

{

NSLog(@"didReceiveRemoteNotification : %@",userInfo);

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];

}

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{

NSLog(@"notification : %@",notification);

}

- (void)applicationDidBecomeActive:(UIApplication *)application //后台切换到前台的 或者应用激活的时候调用

{

// 清除图标数字

application.applicationIconBadgeNumber = 0;

}

下面开始 Xcode 里面的设置

后台设置  关于消息推送和服务器的交互 还有角标的清除 我会在下篇文章里面介绍 谢谢
上一篇 下一篇

猜你喜欢

热点阅读