最近公司不忙,整理一下資料,以備以后直接用,本來計劃全部整理的,但是發現這篇文章程胖出品——極光推送(手把手教你啊)對極光推送的配置工作,說的很詳細了,現在我說一下接下來具體的全部推送、設備標簽推送、設備別名推送
1——對于新手,在看程胖出品的極光推送,推送消息之前,建議還是要把監聽加進去,防止出問題也不知道出在哪
//加在這個方法里面- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver:self
selector:@selector(networkDidSetup:)
name:kJPFNetworkDidSetupNotification
object:nil];
[defaultCenter addObserver:self
selector:@selector(networkDidClose:)
name:kJPFNetworkDidCloseNotification
object:nil];
[defaultCenter addObserver:self
selector:@selector(networkDidRegister:)
name:kJPFNetworkDidRegisterNotification
object:nil];
[defaultCenter addObserver:self
selector:@selector(networkDidLogin:)
name:kJPFNetworkDidLoginNotification
object:nil];
[defaultCenter addObserver:self
selector:@selector(networkDidReceiveMessage:)
name:kJPFNetworkDidReceiveMessageNotification
object:nil];
[defaultCenter addObserver:self
selector:@selector(serviceError:)
name:kJPFServiceErrorNotification
object:nil];
對應的方法
- (void)networkDidSetup:(NSNotification *)notification {
NSLog(@"已連接");
}
- (void)networkDidClose:(NSNotification *)notification {
NSLog(@"未連接");
}
- (void)networkDidRegister:(NSNotification *)notification {
NSLog(@"%@", [notification userInfo]);
NSLog(@"已注冊");
}
- (void)networkDidLogin:(NSNotification *)notification {
NSLog(@"已登錄");
if ([JPUSHService registrationID]) {
NSLog(@"get RegistrationID");
}
}
- (void)serviceError:(NSNotification *)notification {
NSDictionary *userInfo = [notification userInfo];
NSString *error = [userInfo valueForKey:@"error"];
NSLog(@"-----%@", error);
}
- (void)networkDidReceiveMessage:(NSNotification *)notification {
NSDictionary *userInfo = [notification userInfo];
NSLog(@"d消息=====%@",userInfo);
}
2——對于每次推送的角標,可以用下面一句代碼,在每次程序打開的時候去除,和上面的監聽,寫在同一個方法里
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
3——因為推送是默認程序在后臺才進行推送,但是我們公司要求,在打開程序的時候,也可以接收到推送,在這種情況下,我們可以加上下面的方法
- (void)application:(UIApplication *)application didReceiveRemoteNotification
:(NSDictionary *)userInfo fetchCompletionHandler
:(void (^)(UIBackgroundFetchResult))completionHandler {
// [HYBJPushHelper handleRemoteNotification:userInfo completion:completionHandler];
// IOS 7 Support Required
[JPUSHService handleRemoteNotification:userInfo];
completionHandler(UIBackgroundFetchResultNewData);
//下面是應用正在前臺下以提示框的方式顯示推送的代碼
if (application.applicationState == UIApplicationStateActive) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"收到推送消息"
message:userInfo[@"aps"][@"alert"]
delegate:nil
cancelButtonTitle:@"取消"
otherButtonTitles:@"確定",nil];
[alert show];
}
return;
}
4——前面這些已經滿足一般公司的需求了,但是有些公司需要根據不同的設備標簽和設備別名進行推送,具體文檔可以點擊進入官網查看,在設置標簽和別名進行推送的地方,導入 #import "JPUSHService.h" 頭文件,實現下面的代碼就可以了
!!設備標簽注意事項:
傳 nil 表示此次調用不設置此值。
空集合([NSSet set])表示取消之前的設置。
集合成員類型要求為NSString類型
每次調用至少設置一個 tag,覆蓋之前的設置,不是新增。
有效的標簽組成:字母(區分大小寫)、數字、下劃線、漢字。
限制:每個 tag 命名長度限制為 40 字節,最多支持設置 1000 個 tag,但總長度不得超過7K字節。(判斷長度需采用UTF-8編碼)
單個設備最多支持設置 1000 個 tag。App 全局 tag 數量無限制。
!!設備別名注意事項:
傳 nil 表示此次調用不設置此值。
空字符串 (@"")表示取消之前的設置。
每次調用設置有效的別名,覆蓋之前的設置。
有效的別名組成:字母(區分大小寫)、數字、下劃線、漢字。
限制:alias 命名長度限制為 40 字節。(判斷長度需采用UTF-8編碼)
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
17-8-17更新
__autoreleasing NSMutableSet *tags = [NSMutableSet set];
// 設備標簽 為安裝了應用程序的用戶,打上標簽。其目的主要是方便開發者根據標簽,來批量下發 Push 消息。可為每個用戶打多個標簽。 字母(區分大小寫)、數字、下劃線、漢字 每個 tag 命名長度限制為 40 字節
[self setTags:&tags addTag:@"233"];
[JPUSHService setTags:tags completion:^(NSInteger iResCode, NSSet *iTags, NSInteger seq) {
NSLog(@"1111--iResCode---%ld----iTags--%@----seq--%ld",(long)iResCode,iTags,(long)seq);
} seq:23];
// 設備別名
__autoreleasing NSString *alias = @"343434";
[JPUSHService setAlias:alias completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
NSLog(@"1111--iResCode---%ld----iAlias--%@----seq--%ld",(long)iResCode,iAlias,(long)seq);
} seq:45];
}
至此,設備標簽和設備別名推送就已經完成了,你就可以選擇推送方式進行推送了
選擇推送方式
選擇推送方式
極光推送接收自定義消息:在方法- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *) launchOptions 加入下面的代碼:(具體的位置在極光推送 入門指南的 iOS SDK API中)
監聽自定義消息
注冊監聽的代理