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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

极光推送浅解

發布時間:2023/12/10 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 极光推送浅解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.去極光推送官網注冊賬號

2.在蘋果開發網注冊推送證書,還有描述文件

3.回到鑰匙串將證書導出p12格式

4.回到極光推送創建應用

5.下載SDK導入工程

6.在工程中加入以下類:

? ?(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,請不要添加)

7.在plist文件中加入 <key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/> </dict> 8.如果需要聲音,將音頻格式轉換為caf格式 9.Bundle identifier 中設置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 應用程序啟動后,要執行的委托調用

- (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];

?? ?

? ? //創建一個RootViewController對象

? ? 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.注冊遠程推送

? ? //? ? 1.1設置配置選項

? ? //? ? 1.2注冊

? ? //? ? 2.獲取deviceToken,并發送給服務器

? ? //? ? 3.接收遠程推送通知

? ? //? ? 注意:2,3步通過協議方法完成

? ? //? ? 1.注冊

? ? //? ? 1.1設置推送選項

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

?? ?

? ? /*!

?? ? * @abstract 啟動SDK

?? ? *

?? ? * @param launchingOption 啟動參數.

?? ? * @param appKey 一個JPush 應用必須的,唯一的標識. 請參考 JPush 相關說明文檔來獲取這個標識.

?? ? * @param channel 發布渠道. 可選.

?? ? * @param isProduction 是否生產環境. 如果為開發狀態,設置為 NO; 如果為生產狀態,應改為 YES.

?? ? * @param advertisingIdentifier 廣告標識符(IDFA 如果不需要使用IDFA,傳nil.

?? ? *

?? ? * @discussion 提供SDK啟動必須的參數, 來啟動 SDK.

?? ? * 此接口必須在 App 啟動時調用, 否則 JPush SDK 將無法正常工作.

?? ? */

?? ?

//? 1.2注冊

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

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

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

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

?? ?

?? ?

//**************下面是當前app不在活躍狀態(關閉應用)時處理推送通知********

//聲明一個字典接收打開應用時收到的通知

? ? 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(), ^{

? ? ? ? ? ? //執行跳轉功能

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

? ? ? ? });

? ? }

? ? return YES;

}

//2.獲取deviceToken的協議方法

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

?? ?

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

//手機運行獲取token傳給極光服務器

? ? [JPUSHService registerDeviceToken:deviceToken];

?? ?

}

//接收到極光推送過來的消息

//此方法如果App處于后臺,當用戶點擊<推送通知>時會執行該方法

//另一種注釋:如果遠程消息發送過來的時候,app正在運行,這時候會發生什么呢?

//  app代理的application:didReceiveRemoteNotification:方法會被調用,同時遠程消息中的payload數據會作為參數傳遞進去。

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

?? ?

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

? ? //處理收到的anps消息 不寫會收不到消息

? ? [JPUSHService handleRemoteNotification:userInfo];

? ? //記錄新數據

? ? completionHandler(UIBackgroundFetchResultNewData);

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

? ? //設置badge角標數字為零

//? ? application.applicationIconBadgeNumber = 0;

?? ?

//加個判斷

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

? ? ? ? //將取得的值發給下面的方法

? ? ? ? [self handleNotificationAction:userInfo];

? ? }else{

? ? ? ? return;

? ? }

?? ?

}

//自定義通知方法

-(void)handleNotificationAction:(NSDictionary *)dic

{

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

? ? //通過字符串得到一個類

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

? ? //通過類創建對象

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

?? ?

?? //取到相應的值發送給首頁

? ? 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 應用程序將要由活動狀態切換到非活動狀態時執行的委托調用,如按下home 按鈕,返回主屏幕,或全屏之間切換應用程序等。

- (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 在應用程序已進入后臺程序時,要執行的委托調用。所以要設置后臺繼續運行,則在這個函數里面設置即可。

- (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 在應用程序將要進入前臺時(被激活),要執行的委托調用,與applicationWillResignActive 方法相對應。

- (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 在應用程序已被激活后,要執行的委托調用,剛好與? applicationDidEnterBackground 方法相對應。

- (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 在應用程序要完全退出的時候,要執行的委托調用。

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

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

}

需要給系統服務器傳別名的可以用方法:

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

字符串是你設置的別名.

總結

以上是生活随笔為你收集整理的极光推送浅解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。