日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

极光推送浅解

發(fā)布時(shí)間:2023/12/10 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 极光推送浅解 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1.去極光推送官網(wǎng)注冊(cè)賬號(hào)

2.在蘋(píng)果開(kāi)發(fā)網(wǎng)注冊(cè)推送證書(shū),還有描述文件

3.回到鑰匙串將證書(shū)導(dǎo)出p12格式

4.回到極光推送創(chuàng)建應(yīng)用

5.下載SDK導(dǎo)入工程

6.在工程中加入以下類(lèi):

? ?(1)CFNetwork.framework

? ?(2)CoreFoundation.framework

? ?(3)CoreTelephony.framework

? ?(4)SystemConfiguration.framework

? ?(5)CoreGraphics.framework

? ?(6)Foundation.framework

? ?(7)UIKit.framework

? ?(8)Security.framework

? ?(9)libz.tbd

? ?(10)Adsupport.framework (獲取IDFA需要;如果不使用IDFA,請(qǐng)不要添加)

7.在plist文件中加入 <key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/> </dict> 8.如果需要聲音,將音頻格式轉(zhuǎn)換為caf格式 9.Bundle identifier 中設(shè)置bundle ID 10 11.

#import <AVFoundation/AVFoundation.h>

#import "AppDelegate.h"

#import "JPUSHService.h"

#import "ViewController.h"

@interface AppDelegate ()

@property(nonatomic, strong) AVAudioPlayer *audioPlayer;

@end

@implementation AppDelegate

#warning 應(yīng)用程序啟動(dòng)后,要執(zhí)行的委托調(diào)用

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

? ? // Override point for customization after application launch.

?? ?

?? ?

? ? self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

? ? self.window.backgroundColor = [UIColor whiteColor];

? ? [self.window makeKeyAndVisible];

?? ?

? ? //創(chuàng)建一個(gè)RootViewController對(duì)象

? ? ViewController *rootVC = [[ViewController alloc] init];

? ? UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:rootVC];

? ? UITabBarController *tabbarVC = [[UITabBarController alloc] init];

? ? tabbarVC.viewControllers = @[naVC];

? ? //window指定根視圖控制器

? ? self.window.rootViewController = tabbarVC;

?? ?

?? ?

? ? //? ? 1.注冊(cè)遠(yuǎn)程推送

? ? //? ? 1.1設(shè)置配置選項(xiàng)

? ? //? ? 1.2注冊(cè)

? ? //? ? 2.獲取deviceToken,并發(fā)送給服務(wù)器

? ? //? ? 3.接收遠(yuǎn)程推送通知

? ? //? ? 注意:2,3步通過(guò)協(xié)議方法完成

? ? //? ? 1.注冊(cè)

? ? //? ? 1.1設(shè)置推送選項(xiàng)

? ? [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge| UIUserNotificationTypeSound| UIUserNotificationTypeAlert) categories:nil];

?? ?

? ? /*!

?? ? * @abstract 啟動(dòng)SDK

?? ? *

?? ? * @param launchingOption 啟動(dòng)參數(shù).

?? ? * @param appKey 一個(gè)JPush 應(yīng)用必須的,唯一的標(biāo)識(shí). 請(qǐng)參考 JPush 相關(guān)說(shuō)明文檔來(lái)獲取這個(gè)標(biāo)識(shí).

?? ? * @param channel 發(fā)布渠道. 可選.

?? ? * @param isProduction 是否生產(chǎn)環(huán)境. 如果為開(kāi)發(fā)狀態(tài),設(shè)置為 NO; 如果為生產(chǎn)狀態(tài),應(yīng)改為 YES.

?? ? * @param advertisingIdentifier 廣告標(biāo)識(shí)符(IDFA 如果不需要使用IDFA,傳nil.

?? ? *

?? ? * @discussion 提供SDK啟動(dòng)必須的參數(shù), 來(lái)啟動(dòng) SDK.

?? ? * 此接口必須在 App 啟動(dòng)時(shí)調(diào)用, 否則 JPush SDK 將無(wú)法正常工作.

?? ? */

?? ?

//? 1.2注冊(cè)

? ? [JPUSHService setupWithOption:launchOptions appKey:@"db43c773ba7db0917d1940be"

? ? ? ? ? ? ? ? ? ? ? ? ? channel:@"Publish channel"

?? ? ? ? ? ? ? ? apsForProduction:NO

? ? ? ? ? ? advertisingIdentifier:@""];

?? ?

?? ?

//**************下面是當(dāng)前app不在活躍狀態(tài)(關(guān)閉應(yīng)用)時(shí)處理推送通知********

//聲明一個(gè)字典接收打開(kāi)應(yīng)用時(shí)收到的通知

? ? NSDictionary* remoteNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

? ? NSLog(@"收到通知 === :%@", remoteNotification);

?? ?

? ? if (application.applicationState ==? UIApplicationStateInactive) {

?? ? ? ?

? ? ? ? dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

? ? ? ? ? ? //執(zhí)行跳轉(zhuǎn)功能

? ? ? ? ? ? [self handleNotificationAction:remoteNotification];

? ? ? ? });

? ? }

? ? return YES;

}

//2.獲取deviceToken的協(xié)議方法

-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

?? ?

? ? NSLog(@"====%@",deviceToken);

//手機(jī)運(yùn)行獲取token傳給極光服務(wù)器

? ? [JPUSHService registerDeviceToken:deviceToken];

?? ?

}

//接收到極光推送過(guò)來(lái)的消息

//此方法如果App處于后臺(tái),當(dāng)用戶(hù)點(diǎn)擊<推送通知>時(shí)會(huì)執(zhí)行該方法

//另一種注釋:如果遠(yuǎn)程消息發(fā)送過(guò)來(lái)的時(shí)候,app正在運(yùn)行,這時(shí)候會(huì)發(fā)生什么呢?

//  app代理的application:didReceiveRemoteNotification:方法會(huì)被調(diào)用,同時(shí)遠(yuǎn)程消息中的payload數(shù)據(jù)會(huì)作為參數(shù)傳遞進(jìn)去。

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {

?? ?

? ? NSLog(@"userInfo = %@",userInfo);

? ? //處理收到的anps消息 不寫(xiě)會(huì)收不到消息

? ? [JPUSHService handleRemoteNotification:userInfo];

? ? //記錄新數(shù)據(jù)

? ? completionHandler(UIBackgroundFetchResultNewData);

//? ? NSLog(@"收到通知2: = %@",userInfo);

? ? //設(shè)置badge角標(biāo)數(shù)字為零

//? ? application.applicationIconBadgeNumber = 0;

?? ?

//加個(gè)判斷

? ? if (application.applicationState ==? UIApplicationStateActive) {

? ? ? ? //將取得的值發(fā)給下面的方法

? ? ? ? [self handleNotificationAction:userInfo];

? ? }else{

? ? ? ? return;

? ? }

?? ?

}

//自定義通知方法

-(void)handleNotificationAction:(NSDictionary *)dic

{

//? ? NSString *string = [dic objectForKey:@"ViewController"];

? ? //通過(guò)字符串得到一個(gè)類(lèi)

//? ? Class vcClass = NSClassFromString(string);

? ? //通過(guò)類(lèi)創(chuàng)建對(duì)象

//? ? id vcObject = [[[vcClass class] alloc] init];

?? ?

?? //取到相應(yīng)的值發(fā)送給首頁(yè)

? ? NSString *str = [dic objectForKey:@"tong"];

? ? [[NSNotificationCenter defaultCenter] postNotificationName:@"push" object:str userInfo:nil];

}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {

? ? //Optional

? ? NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);

}

#warning 應(yīng)用程序?qū)⒁苫顒?dòng)狀態(tài)切換到非活動(dòng)狀態(tài)時(shí)執(zhí)行的委托調(diào)用,如按下home 按鈕,返回主屏幕,或全屏之間切換應(yīng)用程序等。

- (void)applicationWillResignActive:(UIApplication *)application {

? ? // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

? ? // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

}

#warning 在應(yīng)用程序已進(jìn)入后臺(tái)程序時(shí),要執(zhí)行的委托調(diào)用。所以要設(shè)置后臺(tái)繼續(xù)運(yùn)行,則在這個(gè)函數(shù)里面設(shè)置即可。

- (void)applicationDidEnterBackground:(UIApplication *)application {

? ? // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

? ? // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

}

#warning 在應(yīng)用程序?qū)⒁M(jìn)入前臺(tái)時(shí)(被激活),要執(zhí)行的委托調(diào)用,與applicationWillResignActive 方法相對(duì)應(yīng)。

- (void)applicationWillEnterForeground:(UIApplication *)application {

? ? // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

}

#warning 在應(yīng)用程序已被激活后,要執(zhí)行的委托調(diào)用,剛好與? applicationDidEnterBackground 方法相對(duì)應(yīng)。

- (void)applicationDidBecomeActive:(UIApplication *)application {

?? ?

?? ?

?? ?

? ? // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

}

#warning 在應(yīng)用程序要完全退出的時(shí)候,要執(zhí)行的委托調(diào)用。

- (void)applicationWillTerminate:(UIApplication *)application {

? ? // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

}

需要給系統(tǒng)服務(wù)器傳別名的可以用方法:

[JPUSHService setAlias:[NSString stringWithFormat:@"%@",telTF.text] callbackSelector:nil object:self];

字符串是你設(shè)置的別名.

總結(jié)

以上是生活随笔為你收集整理的极光推送浅解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。