【iOS】集成友盟推送

首先得注册友盟账号,添加应用,请自行解决。友盟+

1. 集成SDK

1.下载SDK -- UMessage_Sdk_All_x.x.x.zip并解压缩
2.导入所需文件夹 -- UMessage_Sdk_x.x.x

配置 : 如果您使用了-all_load,可能需要添加libz的库:
TARGETS-->Build Phases-->Link Binary With Libraries--> +
-->libz

2. 代码实现

#import "UMessage.h"

AppDelegate.m中

注册友盟推送

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[UMessage startWithAppkey:@"you app key" launchOptions:launchOptions];

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= _IPHONE80_
    if([[[UIDevice currentDevice] systemVersion] floatValue] >=  8.0 ){
        //register remoteNotification types (iOS 8.0及其以上版本)
        UIMutableUserNotificationAction *action1 = [[UIMutableUserNotificationAction alloc] init];
        action1.identifier = @"action1_identifier";
        action1.title=@"增加";
        action1.activationMode = UIUserNotificationActivationModeForeground;//当点击的时候启动程序
        
        UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init];  //第二按钮
        action2.identifier = @"action2_identifier";
        action2.title=@"修改";
        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)];
        
        //调用U-Push的自定义注册方法
        NSSet *categories = [NSSet setWithObject:categorys];
        //如果默认使用角标,文字和声音全部打开,请用下面的方法
        [UMessage registerForRemoteNotifications:categories];
        
        //如果对角标,文字和声音的取舍,请用下面的方法
//        UIRemoteNotificationType types7 = UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeSound;
//        UIUserNotificationType types8 = UIUserNotificationTypeAlert|UIUserNotificationTypeSound|UIUserNotificationTypeBadge;
//        [UMessage registerForRemoteNotifications:categories withTypesForIos7:types7 withTypesForIos8:types8];
        
    } 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
    
    //for log
    [UMessage setLogEnabled:YES];
}

获取deviceToken

- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    
    // 1.2.7版本开始不需要用户再手动注册devicetoken,SDK会自动注册
    //[UMessage registerDeviceToken:deviceToken];
    NSString * token = [[[[deviceToken description]
                          stringByReplacingOccurrencesOfString: @"<" withString: @""]
                         stringByReplacingOccurrencesOfString: @">" withString: @""]
                        stringByReplacingOccurrencesOfString: @" " withString: @""];
    NSLog(@"deviceToken: %@",token);
}

处理推送过来的消息

#pragma mark - 推送过来的消息进行处理的方法

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

    self.userInfo = userInfo;
    // 关闭友盟对话框
    [UMessage setAutoAlert:NO];
    
    // 也可以不交给友盟处理 自己处理消息【不要删除】
    [UMessage didReceiveRemoteNotification:userInfo];
    
    // app was already in the foreground
    if([UIApplication sharedApplication].applicationState == UIApplicationStateActive)
    {
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"中奖通知"
                                                         message:[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]
                                                        delegate:self
                                               cancelButtonTitle:@"确定"
                                               otherButtonTitles:nil];
        [alert show];
    }    
    // userInfo是服务器发过来的消息字典集合
}

通知消息点击事件

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
     // 统计消息点击事件
     [UMessage sendClickReportForRemoteNotification:self.userInfo];
}

JPush(极光推送)注册及正确的配置证书,参考这里iOS证书配置指南

你可能感兴趣的:(【iOS】集成友盟推送)