APP怎么免费接入MobPush

1、获取AppKey

 

申请Appkey的流程,请点击 

http://bbs.mob.com/thread-8212-1-1.html?fromuid=70819

2、下载SDK

下载解压后,如下图:


目录结构


(1)Sample:演示Demo。
 
(2)SDK:集成项目时,只需导入此文件夹即可。具体说明在里面的2个文件夹:
 
Required:必要的依赖库(必要)。
 
MobPush:MobPush的SDK。

3、导入SDK

1)手动下载SDK导入

解压下载的ZIP包,将解压后的SDK添加到项目中。
注意:该步骤中添加时,请选择“Create groups for any added folders”单选按钮组。如果你选择“Create folder references for any added folders”,一个蓝色的文件夹引用将被添加到项目并且将无法找到它的资源。

2)pod导入

1、首先 cd 至项目的根目录,执行 pod setup;

2、按需在 Podfile 文件中添加命令:

pod 'mob_pushsdk'

3、如果之前没有安装过,第一次使用请先执行

安装库:pod install

,如果之前已经安装过,那只需要在执行

更新库:pod update

4、添加项目依赖库

必须添加的依赖库如下(Xcode 7 之后 *.dylib库后缀名更改为*.tbd):

libstdc++.dylib
libz.1.2.5.dylib
CoreLocation.framework

5MobPush的初始化配置和功能接口。

5.1 配置AppKeyAppSecret

在项目的Info.plist中添加2个字段:MOBAppKey和MOBAppSecret,对应的值是在mob.com官方申请的应用的AppKey和AppSecret。


Info.plist配置 Privacy – Location When In Use Usage Description 权限以及App Transport Security Settings


证书里需要开通apns功能,然后在项目里设置,如下:


5.2 推送配置(以下代码具有通用性,可直接粘贴使用)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions中进行推送配置即可。

引入头文件:


#import <MobPush/MobPush.h>

调用方法:

// 设置推送环境
#ifdef DEBUG
    [MobPush setAPNsForProduction:NO];
#else
    [MobPush setAPNsForProduction:YES];
#endif
 
//MobPush推送设置(获得角标、声音、弹框提醒权限)
MPushNotificationConfiguration *configuration = [[MPushNotificationConfiguration alloc] init];
configuration.types = MPushAuthorizationOptionsBadge | MPushAuthorizationOptionsSound | MPushAuthorizationOptionsAlert;
[MobPush setupNotification:configuration];

5.3 功能接口调用

所有的功能接口都在MobPush.h中。

目前的MobPush的推送机制是,如果应用不处于active状态,会以苹果的推送系统(APNs)形式发送到手机上。(目前推送都是走APNs,监听不到回调,自定义消息除外)

如果应用是处于active状态,推送会以应用内推送下发到应用中,这时只需要使用一个通知监听@“MobPushDidReceiveMessageNotification”通知即可。例子如下:

先引入头文件:

#import <MobPush/MobPush.h>

再调用方法:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveMessage:) name:MobPushDidReceiveMessageNotification object:nil];

收到的消息数据可能是:1、UDP推送,2、UDP自定义消息,3、APNs,4、本地通知。根据不同的类型做相应显示即可,具体例子如下:

Objective-C

// 收到通知回调
- (void)didReceiveMessage:(NSNotification *)notification
{
    MPushMessage *message = notification.object;
    
    switch (message.messageType)
    {
        case MPushMessageTypeNotification:
        {// UDP 通知
            
        }
            break;
        case MPushMessageTypeCustom:
        {// 自定义消息
            
       }
            break;
        case MPushMessageTypeAPNs:
        {// APNs 回调
            NSLog(@"%@", message.apnsDict);
            
            if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive)
            { // 前台
               
           }
            else
            { // 后台
                
           }
        }
            break;
        case MPushMessageTypeLocal:
        { // 本地通知回调
            NSString *body = message.notification.body;
            NSString *title = message.notification.title;
            NSString *subtitle = message.notification.subTitle;
            NSInteger badge = message.notification.badge;
            NSString *sound = message.notification.sound;
 
            NSLog(@"收到本地通知:{\nbody:%@,\ntitle:%@,\nsubtitle:%@,\nbadge:%ld,\nsound:%@,\n}",body, title, subtitle, badge, sound);
        }
            break;
        default:
            break;
    }
}











猜你喜欢

转载自blog.csdn.net/pipimob/article/details/80595565