IOS10 Notification远程推送通知(三)
来源:互联网 发布:新网域名转出流程 编辑:程序博客网 时间:2024/05/29 08:31
前面几篇文章说了本地推送的相关内容,下面是关于远程推送的。由于推送要服务端进行推,客户端接收,但是有时候不好协调,希望自己本地可以推,方便我们客户端测试。现在推荐一个好用的工具Knuff和NWPusher,本篇文章是用Knuff为例,更多关于Knuff的简介看这篇文章
1、在mac上下载一个Knuff,界面很简单如下
2、注意设备的token在开发环境下是会变的哦,在删除app之后,会不一样,所以需要注意下,否则收不到推送哦!
点击push之后,该设备就可以收到推送啦,如图
下面看下远程带图片的推送
远程带附件的推送需要用到Notification Service Extension
新建一个target,选择Notification Service Extension
看到项目中会多出如下文件
我们先配制推送的格式,在knuff中
{ "aps":{ "alert":{ "title":"hello",//标题 "body":"this is body",//内容 "type":".jpg"//多媒体的类型,方便后面存储 }, "sound":"default",//推送声音 "badge":1,//app显示的数字 "mutable-content": "1",//附件推送要有这个关键字,否则不行 "my-attachment": "http://upload.univs.cn/2012/0104/1325645511371.jpg",//附件的链接 "userInfo":{ "locatin":"地址:上海浦东", "user":"yuna", }//额外信息 }}
在NotificationService.m项目中加上如下代码
#import "NotificationService.h"@interface NotificationService ()@property (nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver);@property (nonatomic, strong) UNMutableNotificationContent *bestAttemptContent;@end@implementation NotificationService- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler { self.contentHandler = contentHandler; self.bestAttemptContent = [request.content mutableCopy]; // Modify the notification content here... NSLog(@"request.content--->%@",request.content); NSDictionary *dict = self.bestAttemptContent.userInfo; NSDictionary *notiDict = dict[@"aps"][@"alert"]; // 可重新修改显示的内容,不重新赋值的话,会显示alert内设置的信息// self.bestAttemptContent.title = [notiDict objectForKey:@"title"];// self.bestAttemptContent.body =[notiDict objectForKey:@"body"]; NSString *mediaUrl = [NSString stringWithFormat:@"%@",dict[@"aps"][@"my-attachment"]]; NSString *mediaType = [NSString stringWithFormat:@"%@",notiDict[@"type"]]; if (!mediaUrl.length) { self.contentHandler(self.bestAttemptContent); } [self loadAttachmentForUrlString:mediaUrl withType:mediaType completionHandle:^(UNNotificationAttachment *attach) { if (attach) { self.bestAttemptContent.attachments = [NSArray arrayWithObject:attach]; } self.contentHandler(self.bestAttemptContent); }];}//下载多媒体附件- (void)loadAttachmentForUrlString:(NSString *)urlStr withType:(NSString *)type completionHandle:(void(^)(UNNotificationAttachment *attach))completionHandler{ __block UNNotificationAttachment *attachment = nil; NSURL *attachmentURL = [NSURL URLWithString:urlStr]; NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; [[session downloadTaskWithURL:attachmentURL completionHandler:^(NSURL *temporaryFileLocation, NSURLResponse *response, NSError *error) { if (error != nil) { NSLog(@"%@", error.localizedDescription); } else { NSFileManager *fileManager = [NSFileManager defaultManager]; NSURL *localURL = [NSURL fileURLWithPath:[temporaryFileLocation.path stringByAppendingString:type]]; [fileManager moveItemAtURL:temporaryFileLocation toURL:localURL error:&error]; NSError *attachmentError = nil; attachment = [UNNotificationAttachment attachmentWithIdentifier:type URL:localURL options:nil error:&attachmentError]; if (attachmentError) { NSLog(@"%@", attachmentError.localizedDescription); } } completionHandler(attachment); }] resume];}- (void)serviceExtensionTimeWillExpire { // Called just before the extension will be terminated by the system. // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used. self.contentHandler(self.bestAttemptContent);}@end
多媒体附件下载参考这篇文章
这里注意一下哈,前面在模拟器上下拉可以看到推送自定义的UI,我在iPhone5上怎么拉都不管用,后来才知道那是3D Touch,iPhone5还木有,所以一直看不到。
0 0
- IOS10 Notification远程推送通知(三)
- iOS10推送通知进阶(Notification Extension)
- IOS10 Notification推送通知(一)
- IOS10 Notification推送通知(二)
- iOS10 添加本地推送(Local Notification)
- iOS10中的通知Notification
- 推送通知(远程推送)
- iOS10推送 本地通知
- iOS10 推送通知整理
- 推送(远程Remote Notification)
- 推送通知 Push Notification
- iOS - iOS10 添加本地推送(Local Notification)
- iOS10推送通知适配
- iOS10推送通知适配
- 推送通知-远程推送
- iOS 远程推送(Push Notification)实现
- iOS10 极光推送(远程) 小结
- iOS10 远程推送适配
- apache服务出现Forbidden 403问题的解决方法总结
- 1071. Speech Patterns (25)
- 机器学习系列:(三)特征提取与处理
- Android源码开发之添加/删除系统应用
- ubuntu 下安装hadoop 2.7.3安装
- IOS10 Notification远程推送通知(三)
- js判断特殊字符
- [session] -- 将session写到数据库或缓存上
- studio的快捷键(收集中)
- Oracle约束(上)
- Charset.defaultCharset()由什么决定
- ASP.NET Web Pages – 页面布局
- SAP HANA跟我学HANA系列之创建计算视图一
- btye(字节型)与int(整数型)