为了账号安全,请及时绑定邮箱和手机立即绑定

APP怎么免费接入MobPush

标签:
Android

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

5、MobPush的初始化配置和功能接口。
5.1 配置AppKey和AppSecret
在项目的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、本地通知。根据不同的类型做相应显示即可,具体例子如下:

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

原文链接:
点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

举报

0/150
提交
取消