国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

IOS10 Notification遠程推送通知(三)

2019-11-09 17:12:34
字體:
來源:轉載
供稿:網友

前面幾篇文章說了本地推送的相關內容,下面是關于遠程推送的。由于推送要服務端進行推,客戶端接收,但是有時候不好協調,希望自己本地可以推,方便我們客戶端測試。現在推薦一個好用的工具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還木有,所以一直看不到。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 崇文区| 礼泉县| 大丰市| 冷水江市| 昌乐县| 安岳县| 镇宁| 吉隆县| 宁明县| 开封县| 密山市| 瑞金市| 乌拉特中旗| 辽阳市| 通海县| 新沂市| 古蔺县| 崇左市| 高台县| 永康市| 花垣县| 孝昌县| 道孚县| 雷州市| 萨迦县| 麦盖提县| 灌南县| 广宗县| 西藏| 盘山县| 托里县| 永清县| 塔城市| 仁布县| 吉林市| 晋宁县| 亳州市| 高密市| 响水县| 江城| 平南县|