您的位置:首页 > 其它

XGPush集成(信鸽集成)demo

2015-01-07 21:47 120 查看
#import "AppDelegate.h"
#import "XGPush.h"
#import "XGSetting.h"

#define _IPHONE80_ 80000

@implementation AppDelegate
- (void)registerPushForIOS8{
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= _IPHONE80_

//Types
UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;

//Actions
UIMutableUserNotificationAction *acceptAction = [[UIMutableUserNotificationAction alloc] init];

acceptAction.identifier = @"ACCEPT_IDENTIFIER";
acceptAction.title = @"Accept";

acceptAction.activationMode = UIUserNotificationActivationModeForeground;
acceptAction.destructive = NO;
acceptAction.authenticationRequired = NO;

//Categories
UIMutableUserNotificationCategory *inviteCategory = [[UIMutableUserNotificationCategory alloc] init];

inviteCategory.identifier = @"INVITE_CATEGORY";

[inviteCategory setActions:@[acceptAction] forContext:UIUserNotificationActionContextDefault];

[inviteCategory setActions:@[acceptAction] forContext:UIUserNotificationActionContextMinimal];

NSSet *categories = [NSSet setWithObjects:inviteCategory, nil];

UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:categories];

[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];

[[UIApplication sharedApplication] registerForRemoteNotifications];
#endif
}

- (void)registerPush{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

//注册推送
[XGPush startApp:2200065428 appKey:@"IXJ8177VC3BG"];

//注销之后需要再次注册前的准备
void (^successCallback)(void) = ^(void){
//如果变成需要注册状态
if(![XGPush isUnRegisterStatus])
{
//iOS8注册push方法
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= _IPHONE80_

float sysVer = [[[UIDevice currentDevice] systemVersion] floatValue];
if(sysVer < 8){
[self registerPush];
}
else{
[self registerPushForIOS8];
}
NSLog(@"sysVer ----------- %g", sysVer);
#else
//iOS8之前注册push方法
//注册Push服务,注册后才能收到推送
[self registerPush];
#endif
}
};
[XGPush initForReregister:successCallback];

//推送反馈回调版本示例
void (^successBlock)(void) = ^(void){
//成功之后的处理
NSLog(@"[XGPush]handleLaunching's successBlock");
};

void (^errorBlock)(void) = ^(void){
//失败之后的处理
NSLog(@"[XGPush]handleLaunching's errorBlock");
};
[XGPush handleLaunching:launchOptions successCallback:successBlock errorCallback:errorBlock];
//角标清0
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];

//清除所有通知(包含本地通知)
[XGPush clearLocalNotifications];
//本地推送示例
//    [XGPush handleLaunching:launchOptions successCallback:successBlock errorCallback:errorBlock];
//
//    NSDate *fireDate = [[NSDate new] dateByAddingTimeInterval:5];
//
//    NSMutableDictionary *dicUserInfo = [[NSMutableDictionary alloc] init];
//    [dicUserInfo setValue:@"myid" forKey:@"clockID"];
//    NSDictionary *userInfo = dicUserInfo;
//
//    [XGPush localNotification:fireDate alertBody:@"测试本地推送" badge:2 alertAction:@"确定" userInfo:userInfo];
[self.window makeKeyAndVisible];
return YES;
}
#pragma mark - Notification

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= _IPHONE80_

//注册UserNotification成功的回调
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
//用户已经允许接收以下类型的推送
//UIUserNotificationType allowedTypes = [notificationSettings types];
NSLog(@"didRegisterUserNotificationSettings");
}

//按钮点击事件回调
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler{
if([identifier isEqualToString:@"ACCEPT_IDENTIFIER"]){
NSLog(@"ACCEPT_IDENTIFIER is clicked");
}

completionHandler();
}

#endif

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

NSLog(@"Token----------------: %@", [[NSString alloc] initWithData:deviceToken encoding:NSUTF8StringEncoding]);

NSString * deviceTokenStr = [XGPush registerDevice:deviceToken];

//注册设备
[XGPush setAccount:@"123456"];
//    [XGPush registerDevice:deviceToken];

void (^successBlock)(void) = ^(void){
//成功之后的处理
NSLog(@"[XGPush]register successBlock ,deviceToken: %@",deviceTokenStr);
};

void (^errorBlock)(void) = ^(void){
//失败之后的处理
NSLog(@"[XGPush]register errorBlock");
};

[XGPush registerDevice:deviceToken successCallback:successBlock errorCallback:errorBlock];

//打印获取的deviceToken的字符串
NSLog(@"deviceTokenStr is %@",deviceTokenStr);
}

//如果deviceToken获取不到会进入此事件
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {

NSString *str = [NSString stringWithFormat: @"Error: %@",err];

NSLog(@"%@",str);

}

- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
{// 取得 APNs 标准信息内容
NSDictionary *aps = [userInfo valueForKey:@"aps"];
NSString *content = [aps valueForKey:@"alert"]; //推送显示的内容
NSInteger badge = [[aps valueForKey:@"badge"] integerValue]; //badge数量
//    badge += [UIApplication sharedApplication].applicationIconBadgeNumber;

NSLog(@"didReceiveRemoteNotification badge = %ld, systemBadgeNumber = %ld", (long)badge, (long)[UIApplication sharedApplication].applicationIconBadgeNumber);
//    [APService setBadge:badge];
NSLog(@"data:%@ --- dic:%@", aps, userInfo);

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
//    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:badge];
NSString *sound = [aps valueForKey:@"sound"]; //播放的声音

// 取得自定义字段内容
NSString *customizeField1 = [userInfo valueForKey:@"customizeField1"]; //自定义参数,key是自己定义的
NSLog(@"content =[%@], badge=[%ld], sound=[%@], customize field =[%@]",content,(long)badge,sound,customizeField1);
//推送反馈(app运行时)
[XGPush handleReceiveNotification:userInfo];

}

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
//notification是发送推送时传入的字典信息
[XGPush localNotificationAtFrontEnd:notification userInfoKey:@"clockID" userInfoValue:@"myid"];

//删除推送列表中的这一条
//    [XGPush delLocalNotification:notification];
//[XGPush delLocalNotification:@"clockID" userInfoValue:@"myid"];

//清空推送列表
//[XGPush clearLocalNotifications];
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: