各位老鐵們,很抱歉,之前的文章內(nèi)容我已經(jīng)徹底刪除了,畢竟現(xiàn)在iOS 11都有了,現(xiàn)在做推送,想語(yǔ)音報(bào)內(nèi)容,又不想程序被殺死,這是不可能滴哈哈!不過(guò)我確實(shí)有做讓程序一直掛在后臺(tái)運(yùn)行的,堅(jiān)持最高的好像有48個(gè)小時(shí)。 我靠!聽(tīng)完都震精!這不是對(duì)手機(jī)有危害嗎!這種程序上架能過(guò)的都是毒瘤哈哈哈!
好了,不扯別的了,其實(shí)我們沒(méi)必要讓程序一直保持在運(yùn)行當(dāng)中,我們可以給他一個(gè)推送,讓程序喚醒,執(zhí)行里面的推送擴(kuò)展方法就可以。有人說(shuō)支付寶可以播報(bào),哪怕程序被殺死以后也可以,老實(shí)說(shuō),你可以把支付寶的推送給關(guān)閉了,你再試試收付款,你看看能不能播報(bào)了? 測(cè)試完你很快就能得知支付寶利用的也是推送擴(kuò)展。 推送擴(kuò)展哪方面的呢?
iOS 10 UNNotificationServiceExtension ! 這個(gè)具體各位大佬們可以直接百度搜索一下就好。我呢就直接將重要的代碼給各位展示出來(lái)。請(qǐng)看下面:
1.首先后端要給你個(gè)推送測(cè)試內(nèi)容,內(nèi)容模板如下:
( 要記住在aps里一定要有"mutable -content"這個(gè)字段,alert 這個(gè)用字符串就可以,不用字典。當(dāng)然字典也行,后面可以獲取里面字符串也行。)

2.選擇創(chuàng)建“NotificationServiceExtension”
(看仔細(xì)了,不要問(wèn)我為
什么不選左邊的,你可以先百度了解一下推送擴(kuò)展,就了解了。)



3.NotificationService.m文件內(nèi)的代碼,這就是iOS10的推送擴(kuò)展
//
// 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
-
配置擴(kuò)展需要的開(kāi)關(guān)
image.png

加上:“App Transport Security Settings” “Allow Arbitrary Loads”
5.把擴(kuò)展里的Deployment Target 改成 10.0,畢竟擴(kuò)展10.0以后才能用哈。

6.到這一步基本已經(jīng)可以了。然后先運(yùn)行一下擴(kuò)展,然后再選擇運(yùn)行一下項(xiàng)目

之后你就可以感受到推送的快感,你可以將程序殺死。如果你發(fā)現(xiàn)測(cè)試了但沒(méi)聲音,你可能需要換個(gè)手機(jī)試一下,推送是必須要打開(kāi)的,不行的話,卸載APP重新安裝就可以了。其實(shí)推送擴(kuò)展沒(méi)啥代碼的,很容易的,如果你覺(jué)得系統(tǒng)機(jī)器人聲音難聽(tīng),支付寶的好聽(tīng),那是因?yàn)樗麄冊(cè)跀U(kuò)展里加了判斷,聲音是由多個(gè)音頻文件合成的。但是都是收款,需要的話可以加群直接要,群內(nèi)也有示范demo,另外需要注意的是iOS 10以后才支持,10以前是不支持的哦!如果有幫助到您,請(qǐng)點(diǎn)贊+關(guān)注!愛(ài)你么么噠。想進(jìn)群先打賞哈哈~
需要解決iOS12.1不能的問(wèn)題,或者已解決過(guò)的來(lái)加QQ群:622177838
歡迎各位朋友來(lái)分享。
