iOS遠程推送之友盟Push,iospush
入職後的一個任務,就是做遠程推送,聽老大說用的是友盟Push.所以就看了一下友盟push,具體的整合以及認證的產生請參照這裡。具體的就不再多說了,主要是自己重新封裝了一下UMessage,具體的內容如下:
//// ZGUmessagePush.h// NotePad//// Created by zhanggui on 15/10/19.// Copyright © 2015年 xiaoguizi. All rights reserved.//#import <Foundation/Foundation.h>#import <CoreLocation/CoreLocation.h>#import "UMessage.h"typedef void(^ResponsData)(id responseObject,NSError *error);@interface ZGUmessagePush : NSObject@property (nonatomic,strong)NSDictionary *receiveDic;+ (instancetype)shared;/** *添加友盟推送 */+ (void) startAddUmessageWithOptions:(NSDictionary *)launchOptions;/** *註冊裝置 */+ (void)registerDeviceWithToken:(NSData *)data;/** *接收資訊 */+(void)didReceiveRemoteNotification:(NSDictionary *)receiveInfoMation;/** *關閉接收訊息的通知 */+ (void)closeUmessageNotification;/** *使用友盟提供的預設提示顯示 */+ (void)setCustomShow:(BOOL)isShow;/** *自訂處理顯示訊息,receiveDic是推送過來的內容 * @param receiveDic 指代推送過來的資訊 */+(void)handleReceiveData:(NSDictionary *)receiveDic;/** *是否允許SDK自動清空角標(預設為開啟) * @param boo yes清空,no不自動清空 */+ (void)setApplicationBadegeClear:(BOOL)boo;/** *設定是否允許SDK當應用在前台運行時收到Push時彈出Alert框(預設開啟) * @param boo yes為允許前台運行時彈出alert,no為不允許 */+ (void)setShowAutoAlert:(BOOL)boo;/** *設定地理位置資訊 * @param location:地理資訊 */+ (void)setLocation:(CLLocation *)location;/** *添加標籤 * @param tagArray 標籤數組,用於盛放要添加的標籤 */+ (void)addTags:(NSArray *)tagArray;/** 綁定一個別名至裝置(含賬戶,和平台類型) @warning 添加Alias的先決條件是已經成功擷取到device_token,否則失敗(kUMessageErrorDependsErr) @param alias 賬戶,例如email @param type 平台類型,參見UMessage檔案頭部的`kUMessageAliasType...`,例如:kUMessageAliasTypeSina @param data 反饋資料,error為擷取失敗時的資訊,responseObject為成功返回的資料 */+ (void)addAlias:(NSString *)alias type:(NSString *)type response:(ResponsData)data;/** 刪除一個裝置的別名綁定 @warning 刪除Alias的先決條件是已經成功擷取到device_token,否則失敗(kUMessageErrorDependsErr) @param alias 賬戶,例如email @param type 平台類型,參見本檔案頭部的`kUMessageAliasType...`,例如:kUMessageAliasTypeSina @param response block返回資料,error為擷取失敗時的資訊,responseObject為成功返回的資料 */+ (void)deleteAlias:(NSString *)alias type:(NSString *)type response:(ResponsData)data;@end
以上是.h檔案。
//// ZGUmessagePush.m// NotePad//// Created by zhanggui on 15/10/19.// Copyright © 2015年 xiaoguizi. All rights reserved.//#import "ZGUmessagePush.h"#import <UIKit/UIKit.h>#import "LoginViewController.h"#import "LeftTableViewController.h"#define _IPHONE80_ 80000#define APPKEY @"5620da47e0f55a062b003b57"#define UMSYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)@implementation ZGUmessagePush+(instancetype)shared { static ZGUmessagePush *sharedPush = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ sharedPush = [[ZGUmessagePush alloc] init]; }); return sharedPush;}+ (void)startAddUmessageWithOptions:(NSDictionary *)launchOptions { [UMessage startWithAppkey:APPKEY launchOptions:launchOptions];#if __IPHONE_OS_VERSION_MAX_ALLOWED >= _IPHONE80_ if(UMSYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) { //register remoteNotification types (iOS 8.0及其以上版本) UIMutableUserNotificationAction *action1 = [[UIMutableUserNotificationAction alloc] init]; action1.identifier = @"action1_identifier"; action1.title=@"Accept"; action1.activationMode = UIUserNotificationActivationModeForeground;//當點擊的時候啟動程式 UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init]; //第二按鈕 action2.identifier = @"action2_identifier"; action2.title=@"Reject"; action2.activationMode = UIUserNotificationActivationModeBackground;//當點擊的時候不啟動程式,在幕後處理 action2.authenticationRequired = YES;//需要解鎖才能處理,如果action.activationMode = UIUserNotificationActivationModeForeground;則這個屬性被忽略; action2.destructive = YES; UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init]; categorys.identifier = @"category1";//這組動作的唯一標示 [categorys setActions:@[action1,action2] forContext:(UIUserNotificationActionContextDefault)]; UIUserNotificationSettings *userSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:[NSSet setWithObject:categorys]]; [UMessage registerRemoteNotificationAndUserNotificationSettings:userSettings]; } else{ //register remoteNotification types (iOS 8.0以下) [UMessage registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound |UIRemoteNotificationTypeAlert]; }#else //register remoteNotification types (iOS 8.0以下) [UMessage registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound |UIRemoteNotificationTypeAlert]; #endif#if DEBUG [UMessage setLogEnabled:YES];#endif [UMessage setLogEnabled:NO];}+ (void)didReceiveRemoteNotification:(NSDictionary *)receiveInfoMation { [UMessage didReceiveRemoteNotification:receiveInfoMation];}+ (void)registerDeviceWithToken:(NSData *)data { NSString *deveiceToken = [NSString stringWithFormat:@"%@",data]; deveiceToken = [deveiceToken stringByReplacingOccurrencesOfString:@" " withString:@""]; NSLog(@"deveice-token:%@",deveiceToken); [UMessage registerDeviceToken:data];}+ (void)closeUmessageNotification { [UMessage unregisterForRemoteNotifications];}+ (void)setCustomShow:(BOOL)isShow { [UMessage setAutoAlert:isShow];}//返回的推送資訊:aps和d,p是UMessage帶過來的,gender是自己添加的//{// aps = {// alert = "\U8868\U793a\U5185\U5bb9\U90a3\U4e2a";// badge = 1;// sound = default;// };// d = us88294144523914949311;// gender = girl;// p = 0;//}+ (void)handleReceiveData:(NSDictionary *)receiveDic { [UMessage setAutoAlert:NO]; //不自動彈出UMessage預設的提示框 if ([UIApplication sharedApplication].applicationIconBadgeNumber!=0) { [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; //設定徽章顯示為0,即不顯示 } //不在前台才去處理 if ([UIApplication sharedApplication].applicationState!=UIApplicationStateActive) { NSLog(@"UMessage:開始處理常式邏輯處理"); UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"標題" message:[receiveDic objectForKey:@"name"] delegate:nil cancelButtonTitle:@"確定" otherButtonTitles: nil]; [alert show]; }else { NSLog(@"UMessage:程式正在前台運行,預設不做任何處理"); }}+ (void)addTags:(NSArray *)tagArray { if ([tagArray isKindOfClass:[NSArray class]]) { if ([tagArray count]==0) { NSLog(@"沒有添加任何tag"); return; }else { [UMessage addTag:tagArray response:^(id responseObject, NSInteger remain, NSError *error) { if (error) { NSLog(@"添加tag失敗"); } }]; } } }+ (void)addAlias:(NSString *)alias type:(NSString *)type response :(ResponsData)data { [UMessage addAlias:alias type:type response:data];}+ (void)deleteAlias:(NSString *)alias type:(NSString *)type response:(ResponsData)data { [UMessage removeAlias:alias type:type response:data];}+ (void)setApplicationBadegeClear:(BOOL)boo { [UMessage setBadgeClear:boo];}+ (void)setShowAutoAlert:(BOOL)boo { [UMessage setAutoAlert:boo];}+ (void)setLocation:(CLLocation *)location { [UMessage setLocation:location];}@end
注意事項:
1、如果是開發環境的話,需要添加deviceToken到友盟推送後台。
2、根據傳過來的欄位進行不同頁面之間的跳轉都是在handleReceiveData:這個方法中進行操作的。
附:
類:http://pan.baidu.com/s/1o6kZbrc