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
原创粉丝点击