您好, , 海量一手媒体资源,专业、正规、高效,为企业提供一站式营销推广服务!
温馨提示
运营小帮手
一站式互联网营销服务平台
  全国免费咨询热线
0755-23071973
运营小帮手
软文发稿
PC端 选择媒体
更方便、更快捷!
查看媒体价格
软文发布、软文代写、百科制作、问答营销、微信营销、微信营销
查看媒体价格
当前位置: 运营小帮手首页 > 新闻中心 > 文章正文

iOS友盟推送封装

这里是很久以前封装的一个小工具类,现在搬到本博客里。这里没有详细教你如何插入友盟推送的教程,只是直接将原来在某小公司里所做的项目需要到集成时,写了一个demo,这里公开给大家而已。

友盟推送官网:http://www.umeng.com/push,需要到官网先注册一个账号哦!

注意事项

友盟推送中,有一个问题,那就是应用处于前台时接收到的推送消息如何显示的问题。 友盟提供了默认的显示框,但是样式不是我们想要的,因此友盟也提供了用户自定义显示框的功能, 但是在用户点击后,友盟要求调用指定的API向友盟反馈。

//如果不调用此方法,统计数据会拿不到,但是如果调用此方法,会再弹一次友盟定制的alertview显示推送消息
//所以这里根据需要来处理是否屏掉此功能
[UMessagesendClickReportForRemoteNotification:[HYBUMessageHelpershared].userInfo];

如何封装

下面是我所封装的友盟推送工具类:

//
//HYBUMessageHelper.h
//UMessageDemo
//
//Createdby黄仪标on14/11/20.
//Copyright(c)2014年黄仪标.Allrightsreserved.
//

#import<Foundation/Foundation.h>
#import<UIKit/UIKit.h>

/*!
*@brief友盟消息推送API相关封装类
*@authorhuangyibiao
*/
@interfaceHYBUMessageHelper:NSObject<UIAlertViewDelegate>

///在应用启动时调用此方法注册
+(void)startWithLaunchOptions:(NSDictionary*)launchOptions;

+(void)registerDeviceToken:(NSData*)deviceToken;
+(void)didReceiveRemoteNotification:(NSDictionary*)userInfo;
//关闭接收消息通知
+(void)unregisterRemoteNotifications;

//defaultisYES
//使用友盟提供的默认提示框显示推送信息
+(void)setAutoAlertView:(BOOL)shouldShow;

//应用在前台时,使用自定义的alertview弹出框显示信息
+(void)showCustomAlertViewWithUserInfo:(NSDictionary*)userInfo;

@end

//
//HYBUMessageHelper.m
//UMessageDemo
//
//Createdby黄仪标on14/11/20.
//Copyright(c)2014年黄仪标.Allrightsreserved.
//

#import"HYBUMessageHelper.h"
#import"UMessage.h"
#include<objc/runtime.h>

#definekUMessageAppKey@"546d9a53fd98c533600016bb"

//ios8.0以后可用,这个参数要求指定为固定值
#definekCategoryIdentifier@"xiaoyaor"

@interfaceHYBUMessageHelper()

@property(nonatomic,strong)NSDictionary*userInfo;

@end

@implementationHYBUMessageHelper

+(HYBUMessageHelper*)shared{
staticHYBUMessageHelper*sharedObject=nil;

staticdispatch_once_tonceToken;
dispatch_once(&onceToken,^{
if(!sharedObject){
sharedObject=[[[selfclass]alloc]init];
}
});

returnsharedObject;
}

+(void)startWithLaunchOptions:(NSDictionary*)launchOptions{
//setAppKeyandLaunchOptions
[UMessagestartWithAppkey:kUMessageAppKeylaunchOptions:launchOptions];

#if__IPHONE_OS_VERSION_MAX_ALLOWED>=__IPHONE_8_0
if([[[UIDevicecurrentDevice]systemVersion]floatValue]>=8.0){
//registerremoteNotificationtypes
UIMutableUserNotificationAction*action1=[[UIMutableUserNotificationActionalloc]init];
action1.identifier=@"action1_identifier";
action1.title=@"Accept";
action1.activationMode=UIUserNotificationActivationModeForeground;//当点击的时候启动程序

UIMutableUserNotificationAction*action2=[[UIMutableUserNotificationActionalloc]init];//第二按钮
action2.identifier=@"action2_identifier";
action2.title=@"Reject";
action2.activationMode=UIUserNotificationActivationModeBackground;//当点击的时候不启动程序,在后台处理
//需要解锁才能处理,如果action.activationMode=UIUserNotificationActivationModeForeground;则这个属性被忽略;
action2.authenticationRequired=YES;
action2.destructive=YES;

UIMutableUserNotificationCategory*categorys=[[UIMutableUserNotificationCategoryalloc]init];
categorys.identifier=kCategoryIdentifier;//这组动作的唯一标示
[categoryssetActions:@[action1,action2]forContext:(UIUserNotificationActionContextDefault)];

UIUserNotificationTypetypes=UIUserNotificationTypeBadge
|UIUserNotificationTypeSound
|UIUserNotificationTypeAlert;
UIUserNotificationSettings*userSettings=[UIUserNotificationSettingssettingsForTypes:types
categories:[NSSetsetWithObject:categorys]];

[UMessageregisterRemoteNotificationAndUserNotificationSettings:userSettings];
}else{
//registerremoteNotificationtypes
UIRemoteNotificationTypetypes=UIRemoteNotificationTypeBadge
|UIRemoteNotificationTypeSound
|UIRemoteNotificationTypeAlert;

[UMessageregisterForRemoteNotificationTypes:types];
}
#else
//iOS8.0之前使用此注册
//registerremoteNotificationtypes
UIRemoteNotificationTypetypes=UIRemoteNotificationTypeBadge
|UIRemoteNotificationTypeSound
|UIRemoteNotificationTypeAlert;

[UMessageregisterForRemoteNotificationTypes:types];
#endif

#ifDEBUG
[UMessagesetLogEnabled:YES];
#else
[UMessagesetLogEnabled:NO];
#endif
}

+(void)registerDeviceToken:(NSData*)deviceToken{
[UMessageregisterDeviceToken:deviceToken];
return;
}

+(void)unregisterRemoteNotifications{
[UMessageunregisterForRemoteNotifications];
return;
}

+(void)didReceiveRemoteNotification:(NSDictionary*)userInfo{
[UMessagedidReceiveRemoteNotification:userInfo];
return;
}

+(void)setAutoAlertView:(BOOL)shouldShow{
[UMessagesetAutoAlert:shouldShow];
return;
}

+(void)showCustomAlertViewWithUserInfo:(NSDictionary*)userInfo{
[HYBUMessageHelpershared].userInfo=userInfo;

//应用当前处于前台时,需要手动处理
if([UIApplicationsharedApplication].applicationState==UIApplicationStateActive){
dispatch_async(dispatch_get_main_queue(),^{
[UMessagesetAutoAlert:NO];
UIAlertView*alertView=[[UIAlertViewalloc]initWithTitle:@"推送消息"
message:userInfo[@"aps"][@"alert"]
delegate:[HYBUMessageHelpershared]
cancelButtonTitle:@"取消"
otherButtonTitles:@"确定",nil];
[alertViewshow];
});
}
return;
}

#pragmamark-UIAlertViewDelegate
-(void)alertView:(UIAlertView*)alertViewclickedButtonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex==1){
//如果不调用此方法,统计数据会拿不到,但是如果调用此方法,会再弹一次友盟定制的alertview显示推送消息
//所以这里根据需要来处理是否屏掉此功能
[UMessagesendClickReportForRemoteNotification:[HYBUMessageHelpershared].userInfo];
}
return;
}

@end

测试使用

下面是测试代码:

//
//AppDelegate.m
//UMessageDemo
//
//Createdby黄仪标on14/11/20.
//Copyright(c)2014年黄仪标.Allrightsreserved.
//

#import"AppDelegate.h"
#import"HYBUMessageHelper.h"
#import"UMessage_Sdk_1.1.0/UMessage.h"

@interfaceAppDelegate()

@end

@implementationAppDelegate


-(BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions{
self.window=[[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]];
//Overridepointforcustomizationafterapplicationlaunch.

[HYBUMessageHelperstartWithLaunchOptions:launchOptions];

self.window.backgroundColor=[UIColorwhiteColor];
[self.windowmakeKeyAndVisible];
returnYES;
}

-(void)application:(UIApplication*)applicationdidRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken{
[HYBUMessageHelperregisterDeviceToken:deviceToken];

NSLog(@"%@",[[[[deviceTokendescription]stringByReplacingOccurrencesOfString:@"<"withString:@""]
stringByReplacingOccurrencesOfString:@">"withString:@""]
stringByReplacingOccurrencesOfString:@""withString:@""]);
return;
}

-(void)application:(UIApplication*)applicationdidReceiveRemoteNotification:(NSDictionary*)userInfo{
[HYBUMessageHelperdidReceiveRemoteNotification:userInfo];

[HYBUMessageHelpersetAutoAlertView:NO];
return;
}

-(void)application:(UIApplication*)applicationdidReceiveRemoteNotification
:(NSDictionary*)userInfofetchCompletionHandler
:(void(^)(UIBackgroundFetchResult))completionHandler{
[HYBUMessageHelperdidReceiveRemoteNotification:userInfo];

[HYBUMessageHelpersetAutoAlertView:NO];
completionHandler(UIBackgroundFetchResultNewData);
return;
}

@end

接下来就需要去官网发通知测试一下,在发通知之前,需要先注册设备,否则在开发环境下,是不会有设备收到信息的。

源代码

之前一直给了错误的链接,原来给的是友盟统一的demo的链接,这里已经更正过来了!

请到github下载:CoderJackyHuang:UMessageDemo_Push

关键词:

猜您可能需要的服务: