天天看點

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           

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

5.1 配置AppKey和AppSecret

在項目的Info.plist中添加2個字段:MOBAppKey和MOBAppSecret,對應的值是在mob.com官方申請的應用的AppKey和AppSecret。

APP怎麼免費接入MobPush

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

APP怎麼免費接入MobPush

證書裡需要開通apns功能,然後在項目裡設定,如下:

APP怎麼免費接入MobPush

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;
    }
}