一、設置badge(角標)
[[UIApplicationsharedApplication]setApplicationIconBadgeNumber:0];
[JPUSHServicesetBadge:0];//清空JPush服務器中存儲的badge值
JPush爲每個客戶端保存其特定的badge值。客戶端有變更時,把badge值更新到JPush服務器。有新的推送時,把這個值+1推送下來。
(1)+(BOOL)setBadge:(int)value ; //設置JPush服務器中存儲的值
注:設置badge值,本地仍須調用UIApplication:setApplicationIconBadgeNumber函數
二、推送目標的設備選擇方式
(1)廣播
(2)tag(標籤):用標籤來進行大規模的設備屬性、用戶屬性的分羣。
爲安裝應用程序的用戶打上標籤,以便於開發者根據標籤來批量下發push消息。
(3)alias(別名):用別名來標識一個用戶,一個設備只能綁定一個別名,但多個設備可以綁定同一個別名。
同一個應用程序內,對不同的用戶建議取不同的別名,儘可能根據別名來唯一確定用戶。
(4)RegistrationID(設備標識)
集成了Jpush SDK的應用程序在第一次成功註冊到JPush服務器時,JPush服務器會給客戶端返 回一個唯一的該設備標識。ios9系統,應用卸載重裝,RegistationID會發生改變。
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(registerDeviceId)name:kJPFNetworkDidLoginNotificationobject:nil];//登錄成功後
-(void)registerDeviceId
{
[JPUSHServiceregistrationID];
NSLog(@"registrationID:%@",[JPUSHServiceregistrationID]);
//在登錄成功對應的方法中設置標籤及別名
注意:在退出登陸時,要去除對標籤及別名的綁定
/**tags alias
*空字符串(@“”)表示取消之前的設置
*nil,此次調用不設置此值
*每次調用設置有效的別名,覆蓋之前的設置
*/
NSString *alias = @"hello";
[JPUSHServicesetTags:nilalias:alias fetchCompletionHandle:^(int iResCode,NSSet*iTags, NSString *iAlias) {
NSLog(@"rescode: %d, \ntags: %@, \nalias: %@\n", iResCode, iTags , iAlias);//對應的狀態碼返回爲0,代表成功
}];
三、本地通知 (獲取apns推送內容)
//1.對於極光推送來說,如果App狀態爲未運行,函數application:didFinishLaunchingWithOptions:將被調用
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//本地通知內容獲取
NSDictionary *remoteNotification = [launchOptionsobjectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
NSLog(@"若launchOptions包含UIApplicationLaunchOptionsRemoteNotificationKey表示用戶點擊本地通知導致app被啓動運行;若不包含則可能爲直接點擊icon被啓動或其他");
}
//2. 如果App狀態爲正在前臺或者點擊通知欄的通知消息,蘋果的回調函數將被調用
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
// 取得 APNs 標準信息內容
NSDictionary *aps = [userInfo valueForKey:@"aps"];
NSString *content = [aps valueForKey:@"alert"]; //推送顯示的內容
NSInteger badge = [[aps valueForKey:@"badge"] integerValue]; //badge數量
NSString *sound = [aps valueForKey:@"sound"]; //播放的聲音
// 取得Extras字段內容
NSString *customizeField1 = [userInfo valueForKey:@"customizeExtras"]; //服務端中Extras字段,key是自己定義的
NSLog(@"content =[%@], badge=[%ld], sound=[%@], customize field =[%@]",content,(long)badge,sound,customizeField1);
//判斷程序是否在前臺運行
if (application.applicationState ==UIApplicationStateActive) {
//如果應用在前臺,在這裏執行
UIAlertView *alertView = [[UIAlertViewalloc] initWithTitle:@"極光推送"message:content delegate:nilcancelButtonTitle:@"ok"otherButtonTitles:nil,nil];
[alertViewshow];
}
// iOS 7 Support Required,處理收到的APNS信息
//如果應用在後臺,在這裏執行
[JPUSHServicehandleRemoteNotification:userInfo];
completionHandler(UIBackgroundFetchResultNewData);
[JPUSHServicesetBadge:0];//清空JPush服務器中存儲的badge值
[application setApplicationIconBadgeNumber:0];//小紅點清0操作
}
/**前臺運行時,可接收由JPush下發的自定義消息
*獲取自定義消息(只有在前端運行的時候才能收到自定義消息的推送)
* kJPFNetworkDidReceiveMessageNotification// 收到消息(非APNS)
*/
NSNotificationCenter *defaultCenter = [NSNotificationCenterdefaultCenter];
[defaultCenteraddObserver:selfselector:@selector(networkDidReceiveMessage:)name:kJPFNetworkDidReceiveMessageNotificationobject:nil];
- (void)networkDidReceiveMessage:(NSNotification *)notification {
/**
*參數描述:
content:獲取推送的內容
extras:獲取用戶自定義參數
customizeField1:根據自定義key獲取自定義的value
*/
NSDictionary * userInfo = [notification userInfo];
NSString *content = [userInfo valueForKey:@"content"];
NSDictionary *extras = [userInfo valueForKey:@"extras"];
NSString *customizeField1 = [extras valueForKey:@"customizeField1"]; ///服務端傳遞的Extras附加字段,key是自己定義的
NSInteger badge = [[[userInfovalueForKey:@"aps"]valueForKey:@"badge"]integerValue];
NSLog(@"%jiaobao--ld",(long)badge);
NSLog(@"custuserInfo:%@",userInfo);
NSLog(@"custcontent:%@",content);
NSLog(@"custextras:%@",extras);
NSLog(@"customizeField1:%@",customizeField1);
NSLog(@"cust獲取註冊ID:%@", [JPUSHServiceregistrationID]);
}