友盟推送集成日記

1.注冊賬號 登錄用戶名是注冊的郵箱.友盟
2.進入后臺:在首頁選擇產品,選擇UM推送. 然后點擊開始使用.進入網站后臺.
3.創建應用
這里需要兩個推送的證書,一個調試用的,一個發布用的.

C06C93CB-D727-4A2C-AED3-D527571CA755.png

注意這里的兩個證書都是用于這臺電腦調試某個app推送功能和發布功能的.
制作證書,就不多說了.按照文檔流程.
推送生產用的證書下載位置如圖:

F3EF92BA-F09C-427C-BD73-DBAB12E3F996.png

調試用的選擇Development 選擇對應的證書.

下載后,打開鑰匙串
如圖:

9988732F-8309-4A49-8A24-1DB6944B6538.png

注意 這里一定要是拖進來 否則可能不會出現密匙

4.拖進去以后導出證書


C899A586-4F52-4141-BA1F-59F5C8CD2704.png

5.在xcode中的代碼 下載sdk 導入頭文件

//注冊遠程推送
- (void)registerRemoteMessage {
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= _IPHONE80_
    if(kiOS8Later)
    {
        //register remoteNotification types
        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 *actionCategory = [[UIMutableUserNotificationCategory alloc] init];
        actionCategory.identifier = @"category1";//這組動作的唯一標示
        [actionCategory setActions:@[action1,action2] forContext:(UIUserNotificationActionContextDefault)];
        
        NSSet *categories = [NSSet setWithObject:actionCategory];
        
        //如果默認使用角標,文字和聲音全部打開,請用下面的方法
        [UMessage registerForRemoteNotifications:categories];
        
    } else{
        //register remoteNotification types
         [UMessage registerForRemoteNotifications];
    }
#else
    [UMessage registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge
     |UIRemoteNotificationTypeSound
     |UIRemoteNotificationTypeAlert];
    
#endif
    
}

在回調的接受通知的方法中調用下面方法 處理回調信息

- (void)didReceiveRemoteNotification:(NSDictionary *)userInfo {
    //關閉友盟自帶的彈出框
    
    LOG(@"didReceiveRemoteNotification");
    [UMessage setAutoAlert:NO];
    [UMessage didReceiveRemoteNotification:userInfo];
    
    //    NSDictionary *dic = [MyHelp jsonDataFormatterWithStringSourceData:[DMDes decryptUseDES:userInfo[@"msg"] key:DMDESKEYS]] ;
    //
    //    if ([[dic objectForKey:@"type"] isEqualToString:@"articles"] ||
    //        [[dic objectForKey:@"type"] isEqualToString:@"projects"]) {
    //        [self didReceiveRemoteJingWhenAppBackgroundWithDic:dic];
    //    } else {
    //        [self didReceiveRemoteMessageWhenAppBackgroundWithDic:userInfo];
    //    }
}

6.服務端的Python代碼:使用的是3.0以下的版本,沒有安裝requests 的需要安裝一下依賴庫.

#coding=utf-8

import time

import hashlib
import requests
import json
import urllib2

def md5(s):
    m = hashlib.md5(s)
    return m.hexdigest()

def push_unicast(appkey, app_master_secret, device_token):
    timestamp = int(time.time() * 1000 )
    method = 'POST'
    url = 'http://msg.umeng.com/api/send'
    params = {'appkey': appkey,
              'timestamp': timestamp,
              'device_tokens': device_token,
              'type': 'unicast',
              'payload': {'body': {'ticker': 'Hello World',
                                   'title':'Hello',
                                   'text':'UM',
                                   'after_open': 'go_app'},
                            'display_type': 'notification',
                          'aps':{'alert':'xxx'}
                          },
              'production_mode':'false' //調試模式 在發布的時候,修改過來.就可以了
    }
    post_body = json.dumps(params)
    print post_body
    sign = md5('%s%s%s%s' % (method,url,post_body,app_master_secret))
    try:
        r = urllib2.urlopen(url + '?sign='+sign, data=post_body)
        print r.read()
    except urllib2.HTTPError,e:
        print e.reason,e.read()
    except urllib2.URLError,e:
        print e.reason

if __name__ == '__main__':
    appkey = 'your key'
    app_master_secret = 'you secret'
    device_token = 'you device_token'

    push_unicast(appkey, app_master_secret, device_token)
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容