iOS客戶端本地推送實(shí)現(xiàn)代碼
本文實(shí)例為大家分享了iOS本地推送的具體代碼,供大家參考,具體內(nèi)容如下
首先創(chuàng)建全局的本地通知對象及彈出框
// 彈出本地消息
@property(nonatomic,strong)UILocalNotification *localNotification;
@property(nonatomic,strong)UIAlertController *alertcontrol;
其次在代碼中實(shí)現(xiàn)如下:
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:
(void (^)(UIBackgroundFetchResult))completionHandler {
// 判斷程序是否在打開狀態(tài),即是否在前臺(tái)運(yùn)行的狀態(tài)
if (application.applicationState == UIApplicationStateActive) {
// 如果之前已經(jīng)接受到消息,并且用戶未對彈出框進(jìn)行處理,關(guān)閉原來彈出框
if (self.localNotification) {
[self.alertcontrol dismissViewControllerAnimated:YES completion:nil];
}
[self bulidLocationNotification:application userinfo:userInfo];
}else{
// 判斷程序是否未打開狀態(tài),即是否在后臺(tái)運(yùn)行或關(guān)閉狀態(tài),極光推送設(shè)置角標(biāo)
if ([application applicationIconBadgeNumber]>0) {
[JPUSHService setBadge:[application applicationIconBadgeNumber]];
}
}
[JPUSHService handleRemoteNotification:userInfo];
NSLog(@"收到通知:%@", [self logDic:userInfo]);
completionHandler(UIBackgroundFetchResultNewData);
}
- (void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification {
// 彈出消息
if (self.localNotification) {
self.alertcontrol = [UIAlertController alertControllerWithTitle:@"" message:self.localNotification.alertBody preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
self.localNotification = nil;
self.alertcontrol = nil;
}];
[self.alertcontrol addAction:action];
[self.window.rootViewController presentViewController:self.alertcontrol animated:YES completion:nil];
}
}
// 創(chuàng)建本地推送消息
-(void)bulidLocationNotification:(UIApplication *)application userinfo:(NSDictionary *)userInfo{
self.localNotification = [[UILocalNotification alloc]init];
self.localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:1];
self.localNotification.alertBody = [[userInfo objectForKey:@"aps"] objectForKey:@"alert"];
self.localNotification.alertAction = @"確定";
self.localNotification.soundName = @"sound.caf";
self.localNotification.userInfo = userInfo;
[application presentLocalNotificationNow:self.localNotification];
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS實(shí)現(xiàn)時(shí)間顯示幾分鐘前,幾小時(shí)前以及剛剛的方法示例
這篇文章主要介紹了如何利用iOS實(shí)現(xiàn)時(shí)間顯示是在幾小時(shí)前,幾分鐘前以及剛剛的格式,類似大家在qq空間和朋友圈微博看到的效果,文中給出了詳細(xì)的示例代碼,有需要的朋友們可以參考借鑒,下面來一起學(xué)習(xí)學(xué)習(xí)吧。2017-01-01
iOS 簡約日歷控件EBCalendarView的實(shí)現(xiàn)代碼
本篇文章主要介紹了iOS 簡約日歷控件EBCalendarView的實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05
iOS App設(shè)計(jì)模式開發(fā)中對建造者模式的運(yùn)用實(shí)例
這篇文章主要介紹了iOS App設(shè)計(jì)模式開發(fā)中對建造者模式的運(yùn)用實(shí)例,示例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-04-04
Framework中實(shí)現(xiàn)OC和Swift的混編方案
這篇文章主要為大家介紹了Framework中實(shí)現(xiàn)OC和Swift的混編方案詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
IOS Ble藍(lán)牙開發(fā)實(shí)現(xiàn)方法
這篇文章主要為大家詳細(xì)介紹了IOS Ble藍(lán)牙開發(fā)的實(shí)現(xiàn)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12
iOS11 SectionHeader 胡亂移動(dòng)且滑動(dòng)時(shí)出現(xiàn)重復(fù)內(nèi)容的解決方法
這篇文章主要介紹了iOS11 SectionHeader 胡亂移動(dòng)且滑動(dòng)時(shí)出現(xiàn)重復(fù)內(nèi)容的解決方法,需要的朋友可以參考下2017-11-11
iOS如何自定義控制器轉(zhuǎn)場動(dòng)畫push詳解
在平時(shí)開發(fā)中,有時(shí)候需要一些轉(zhuǎn)場動(dòng)畫給界面調(diào)整增添一些活力,而實(shí)現(xiàn)這些動(dòng)畫相對比較繁瑣。下面這篇文章主要給大家介紹了關(guān)于iOS如何自定義控制器轉(zhuǎn)場動(dòng)畫push的相關(guān)資料,需要的朋友可以參考下。2017-12-12
iOS開發(fā)之獲取系統(tǒng)相冊中的圖片與視頻教程(內(nèi)帶url轉(zhuǎn)換)
本篇文章主要介紹了iOS開發(fā)之獲取系統(tǒng)相冊中的圖片與視頻教程(內(nèi)帶url轉(zhuǎn)換),主要介紹AssetsLibrary 框架,具有一定的參考價(jià)值,有需要的可以了解一下。2016-11-11

