前面幾篇文章說了本地推送的相關內容,下面是關于遠程推送的。由于推送要服務端進行推,客戶端接收,但是有時候不好協調,希望自己本地可以推,方便我們客戶端測試。現在推薦一個好用的工具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"http://多媒體的類型,方便后面存儲 }, "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還木有,所以一直看不到。
新聞熱點
疑難解答