友盟推送

一、感受

下面讲下我对推送这个功能在IOS下的感受,这个算是我做了服务端的功能和手机端的功能后的一个体会吧, 这功能在IOS上是多少给我带来了点鸡肋的感觉,首先很多时候收到推送有延时现象,还有如果连续推送两条消息给一个Iphone手机,如果遇到延时这个问题,那么第一条推送就会被覆盖。(这里我用的是友盟的推送,这个推送暂时感觉还不错,大多数推送能及时收到,至少我暂时测试是如此,所以我这里也选择了它,以前遇到过两小时后才到的第三方推送.....甚至根本就收不到。),再一个,即使延迟这个问题解决了,关于推送消息的管理也是一个麻烦事,如果想好好管理,我觉得数据库里面首先得创建一张对应的通知管理表,今天在IOS上面我是认真的测试了这功能,当应用从后台移除后,接受到的推送消息,是不会进入通知代理的,这一点是个麻烦事,(也就是这一点,让我想到了数据库中创建对应的表。),应为用户不可能一直把手机拿手上,即使拿手上,就看那通知在手机上的短暂停留时间,也可能导致用户没看明白后台推送的什么,而且这时候应用处于被移出后台的状态,由于这个原因,接受到的通知就不能进入通知代理,所以导致这条通知无法保存到本地。所以说创建一张表我觉得是必要的。 但我发现很多应用都没这样做。 如果这样做了,对推送来说是一个很大的优化,至少在IOS上是,因为IOS暂时无法完全避免接受不到通知这个揪心的问题,而且我觉得发送的消息内容都不重要了。感觉可以把通知的语音修改一下,这一点才重要.....改成这样:你有新消息请在通知表中去查看....  

二、下载友盟推送SDK集成到应用

这里注册我就不说了,下面我主要以截图的方式讲解。

1、 下面的截图点击"SDK下载及文档"

2、上一步执行后,进入新页面,找到消息推送。

3、上一步执行后,进入新页面,点击SDK下载。

4、将下载的SDK推送包COPY到项目中。

三、导出推送证书上传友盟,创建应用

    1、友盟的推送只需要导出P12证书即可,不需要生成pem证书,这里需要导出两个证书,一个是开发者证书,一个是产品证书,下面截图。

       你如果还不知道怎么制作Ios推送证书,可以去  http://www.cnblogs.com/xiaoliao/p/4928873.html 查看,讲得非常详细。 

2、下面是友盟创建IOS应用的步骤截图

首先进入帐号管理后台,可以从首页右上角点击 我的产品进入。

进入后点击页面左下角的 +增加新应用

     进入创建应用界面,创建应用,这里由于我是演示给各位看,所以随便填的。

上面步骤执行完毕后,拖动浏览器滚动条就会看到下面的截图

下面的截图我要说下,当你执行完上面截图的步骤之后,你就给选择的应用创建了推送的功能,然后你在看这个应用的就是下面截图的

的样式,下面这个截图是可以随意修改的,即使你上一步传错了证书,或则其他操作有误,创建完毕后也可以修改。下面是截图, 这里

要说一个重点:就是服务器IP的增加,如果这里不增加服务器IP,WEB服务端是无法将通知推送到你手机的。这里的IP就是Servlt架设到服务器的IP,我这里由于

是在本机上做的servlet接口推送,所以就是我本机的IP了,如果架设到服务器之后,这个IP就是要修改成服务器的IP。

四、IOS上实现推送功能的代码

  1、只贴通知部分代码,Appdelegate.m里面授权。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
   //asdf;lajksdfl;asdfl;
    
    
     /*友盟推送*/
    [UMessage startWithAppkey:@"568f2ccbe0f55a3e05?" launchOptions:launchOptions];
    
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= _IPHONE80_
    if(UMSYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"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
    //for log
    [UMessage setLogEnabled:YES];
    }

2、注册设备Token

 - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    //下面这个token是将获取的nsdata转换成String,应为指定推送时我们需要将这个传给服务端。
    NSString *token=[NSString stringWithFormat:@"%@",[[[[deviceToken description] stringByReplacingOccurrencesOfString: @"<" withString: @""]                  stringByReplacingOccurrencesOfString: @">" withString: @""]                 stringByReplacingOccurrencesOfString: @" " withString: @""]];
    //注册token
    [UMessage registerDeviceToken:deviceToken];
}

3、接收推送

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    
    //接受服务端推送通知传来的值,全部在userinfo里面。
    [UMessage didReceiveRemoteNotification:userInfo];
    [UIApplication sharedApplication].applicationIconBadgeNumber=5;
    
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
    {
        UIUserNotificationType myType = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
        UIUserNotificationSettings *mySetting = [UIUserNotificationSettings settingsForTypes:myType categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:mySetting];
        
    }else{
        
        UIRemoteNotificationType myType = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myType];
    }
    
  
}

四、管理后台测试,手动发送推送。(截图)

    下面的截图要说一下,4的一步,增加测试设备,由于我这里使用的是开发者模式,所以这里我们需要将

测试是被的TOKEN增加到进去, 获取TOKEN的方法就是上一个步骤的( 2、注册设备Token,的第一句代码)

   

  

  

明天会增加服务端servlet的实现。

原文地址:https://www.cnblogs.com/striveLD/p/5924551.html