怎么使用極光推送進(jìn)行遠(yuǎn)程推送
在極光官網(wǎng)注冊極光推送
創(chuàng)建一個應(yīng)用
在應(yīng)用配置中導(dǎo)入兩個證書(我這是已經(jīng)驗(yàn)證了的微谓,不然就是讓你上傳證書的按鈕)Bundle ID是導(dǎo)入證書后自動出現(xiàn)的憨栽,證書最好一個一個上傳不然可能會出現(xiàn)驗(yàn)證失敗的問題.
然后下載SDK
把SDK中的Lib文件夾導(dǎo)入你的項(xiàng)目中,記得勾選Copy記得勾選Copy
在Build Phases中導(dǎo)入以下庫
在Build Phases中導(dǎo)入以下庫
修改Capabilities
打開Background Modes
修改Bundle ID 以及選擇Team
修改Bundle ID 以及選擇Team
下面便是代碼部分
在Appdelegate.m
中導(dǎo)入頭文件#import "JPUSHService.h"
在didFinishLaunchingWithOptions
方法中寫入以下代碼床玻,有的地方要按你自己的信息填
// Required
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0)
{
//可以添加自定義categories
[JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |UIUserNotificationTypeSound
|UIUserNotificationTypeAlert)categories:nil];
}
else
{
//categories 必須為nil
[JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound
|UIRemoteNotificationTypeAlert)categories:nil];
}
[JPUSHService setupWithOption:launchOptions appKey:你的appKey(字符串)毁涉,
在極光的應(yīng)用詳情中可以看到" channel:@""(這個不填就行) apsForProduction:這個值生產(chǎn)環(huán)境為YES,開發(fā)環(huán)境為NO(BOOL值)];
實(shí)現(xiàn)-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
在該方法中注冊設(shè)備
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
//這個方法是設(shè)置別名和tag 可省
// [JPUSHService setTags:nil alias:@"WzxJiang"
//fetchCompletionHandle:^(int iResCode, NSSet *iTags, NSString *iAlias) {
// NSLog(@"rescode: %d, \ntags: %@, \nalias: %@\n", iResCode, iTags , iAlias);// }];
// Required
[JPUSHService registerDeviceToken:deviceToken];
}
實(shí)現(xiàn)- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
App在后臺時收到推送時的處理
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
//iOS 7及之后才能用锈死,現(xiàn)在沒人適配iOS6了吧...
// IOS 7 Support Required
[JPUSHService handleRemoteNotification:userInfo];
completionHandler(UIBackgroundFetchResultNewData);
}
在applicationWillEnterForeground
方法(App即將進(jìn)入前臺)中將小紅點(diǎn)清除
- (void)applicationWillEnterForeground:(UIApplication *)application
{
NSLog(@"進(jìn)入前臺");
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
}
創(chuàng)建didFailToRegisterForRemoteNotificationsWithError
方法贫堰,處理接收推送錯誤的情況(一般不會...)
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
//Optional
NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}
代碼都已經(jīng)弄好了穆壕,現(xiàn)在推送以下,(注意只能是真機(jī)調(diào)試)在極光上點(diǎn)擊發(fā)送通知,注意是開發(fā)環(huán)境點(diǎn)擊發(fā)送通知,注意是開發(fā)環(huán)境
然后你就能收到推送了然后你就能收到推送了
文/WzxJiang(簡書作者)原文鏈接:http://www.reibang.com/p/469bf2a7b16f/comments/1567154著作權(quán)歸作者所有其屏,轉(zhuǎn)載請聯(lián)系作者獲得授權(quán)喇勋,并標(biāo)注“簡書作者”。