0
点赞
收藏
分享

微信扫一扫

APP 怎么免费接入 MobPush

1、获取 AppKey
 
申请 Appkey 的流程,请点击 
​​​http://bbs.mob.com/thread-8212-1-1.html?fromuid=70819​​​2、下载 SDK
下载解压后,如下图:

 APP 怎么免费接入 MobPush_自定义消息

目录结构

(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


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


5.1 配置 AppKey 和 AppSecret

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

 APP 怎么免费接入 MobPush_依赖库_02

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

 APP 怎么免费接入 MobPush_自定义消息_03

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

 APP 怎么免费接入 MobPush_推送_04


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、本地通知。根据不同的类型做相应显示即可,具体例子如下:

// 收到通知回调
- (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;
}
}

举报

相关推荐

0 条评论