各位老鐵們,很抱歉,之前的文章內容我已經徹底刪除了,畢竟現在iOS 11都有了,現在做推送,想語音報內容,又不想程序被殺死,這是不可能滴哈哈!不過我確實有做讓程序一直掛在后臺運行的,堅持最高的好像有48個小時。 我靠!聽完都震精!這不是對手機有危害嗎!這種程序上架能過的都是毒瘤哈哈哈!
好了,不扯別的了,其實我們沒必要讓程序一直保持在運行當中,我們可以給他一個推送,讓程序喚醒,執行里面的推送擴展方法就可以。有人說支付寶可以播報,哪怕程序被殺死以后也可以,老實說,你可以把支付寶的推送給關閉了,你再試試收付款,你看看能不能播報了? 測試完你很快就能得知支付寶利用的也是推送擴展。 推送擴展哪方面的呢?
iOS 10 UNNotificationServiceExtension ! 這個具體各位大佬們可以直接百度搜索一下就好。我呢就直接將重要的代碼給各位展示出來。請看下面:
1.首先后端要給你個推送測試內容,內容模板如下:
( 要記住在aps里一定要有"mutable -content"這個字段,alert 這個用字符串就可以,不用字典。當然字典也行,后面可以獲取里面字符串也行。)
2.選擇創建“NotificationServiceExtension”
(看仔細了,不要問我為
什么不選左邊的,你可以先百度了解一下推送擴展,就了解了。)
3.NotificationService.m文件內的代碼,這就是iOS10的推送擴展
//
// NotificationService.m
// GYNotificationService
//
// Created by Sylar on 2018/4/12.
// Copyright ? 2018年 Sylar. All rights reserved.
//
#import "NotificationService.h"
#import <AVFoundation/AVFAudio.h>
@interface NotificationService ()<AVSpeechSynthesizerDelegate>
@property (nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver);
@property (nonatomic, strong) UNMutableNotificationContent *attemptContent;
@property (nonatomic, strong) AVSpeechSynthesizer *aVSpeechSynthesizer;
@end
@implementation NotificationService
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
self.contentHandler = contentHandler;
self.attemptContent = [request.content mutableCopy];
self.aVSpeechSynthesizer = [[AVSpeechSynthesizer alloc] init];
// Modify the notification content here...
NSString *str = self.attemptContent.userInfo[@"aps"][@"alert"];
AVSpeechUtterance * aVSpeechUtterance = [[AVSpeechUtterance alloc] initWithString:str];
aVSpeechUtterance.rate = AVSpeechUtteranceDefaultSpeechRate;
aVSpeechUtterance.voice =[AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];
[self.aVSpeechSynthesizer speakUtterance:aVSpeechUtterance];
}
- (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.attemptContent);
}
@end
-
配置擴展需要的開關
image.png
加上:“App Transport Security Settings” “Allow Arbitrary Loads”
5.把擴展里的Deployment Target 改成 10.0,畢竟擴展10.0以后才能用哈。
6.到這一步基本已經可以了。然后先運行一下擴展,然后再選擇運行一下項目
之后你就可以感受到推送的快感,你可以將程序殺死。如果你發現測試了但沒聲音,你可能需要換個手機試一下,推送是必須要打開的,不行的話,卸載APP重新安裝就可以了。其實推送擴展沒啥代碼的,很容易的,如果你覺得系統機器人聲音難聽,支付寶的好聽,那是因為他們在擴展里加了判斷,聲音是由多個音頻文件合成的。但是都是收款,需要的話可以加群直接要,群內也有示范demo,另外需要注意的是iOS 10以后才支持,10以前是不支持的哦!如果有幫助到您,請點贊+關注!愛你么么噠。想進群先打賞哈哈~
需要解決iOS12.1不能的問題,或者已解決過的來加QQ群:622177838
歡迎各位朋友來分享。