1.注冊(cè)賬號(hào) 登錄用戶名是注冊(cè)的郵箱.友盟
2.進(jìn)入后臺(tái):在首頁(yè)選擇產(chǎn)品,選擇UM推送. 然后點(diǎn)擊開始使用.進(jìn)入網(wǎng)站后臺(tái).
3.創(chuàng)建應(yīng)用
這里需要兩個(gè)推送的證書,一個(gè)調(diào)試用的,一個(gè)發(fā)布用的.
C06C93CB-D727-4A2C-AED3-D527571CA755.png
注意這里的兩個(gè)證書都是用于這臺(tái)電腦調(diào)試某個(gè)app推送功能和發(fā)布功能的.
制作證書,就不多說(shuō)了.按照文檔流程.
推送生產(chǎn)用的證書下載位置如圖:
F3EF92BA-F09C-427C-BD73-DBAB12E3F996.png
調(diào)試用的選擇Development 選擇對(duì)應(yīng)的證書.
下載后,打開鑰匙串
如圖:
9988732F-8309-4A49-8A24-1DB6944B6538.png
注意 這里一定要是拖進(jìn)來(lái) 否則可能不會(huì)出現(xiàn)密匙
4.拖進(jìn)去以后導(dǎo)出證書
C899A586-4F52-4141-BA1F-59F5C8CD2704.png
5.在xcode中的代碼 下載sdk 導(dǎo)入頭文件
//注冊(cè)遠(yuǎn)程推送
- (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;//當(dāng)點(diǎn)擊的時(shí)候啟動(dòng)程序
UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init]; //第二按鈕
action2.identifier = @"action2_identifier";
action2.title=@"Reject";
action2.activationMode = UIUserNotificationActivationModeBackground;//當(dāng)點(diǎn)擊的時(shí)候不啟動(dòng)程序,在后臺(tái)處理
action2.authenticationRequired = YES;//需要解鎖才能處理邓萨,如果action.activationMode = UIUserNotificationActivationModeForeground;則這個(gè)屬性被忽略射众;
action2.destructive = YES;
UIMutableUserNotificationCategory *actionCategory = [[UIMutableUserNotificationCategory alloc] init];
actionCategory.identifier = @"category1";//這組動(dòng)作的唯一標(biāo)示
[actionCategory setActions:@[action1,action2] forContext:(UIUserNotificationActionContextDefault)];
NSSet *categories = [NSSet setWithObject:actionCategory];
//如果默認(rèn)使用角標(biāo)造烁,文字和聲音全部打開千元,請(qǐng)用下面的方法
[UMessage registerForRemoteNotifications:categories];
} else{
//register remoteNotification types
[UMessage registerForRemoteNotifications];
}
#else
[UMessage registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge
|UIRemoteNotificationTypeSound
|UIRemoteNotificationTypeAlert];
#endif
}
在回調(diào)的接受通知的方法中調(diào)用下面方法 處理回調(diào)信息
- (void)didReceiveRemoteNotification:(NSDictionary *)userInfo {
//關(guān)閉友盟自帶的彈出框
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.服務(wù)端的Python代碼:使用的是3.0以下的版本,沒有安裝requests 的需要安裝一下依賴庫(kù).
#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' //調(diào)試模式 在發(fā)布的時(shí)候,修改過(guò)來(lái).就可以了
}
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)