1.注册账号 登录用户名是注册的邮箱.友盟
2.进入后台:在首页选择产品,选择UM推送. 然后点击开始使用.进入网站后台.
3.创建应用
这里需要两个推送的证书,一个调试用的,一个发布用的.
注意这里的两个证书都是用于这台电脑调试某个app推送功能和发布功能的.
制作证书,就不多说了.按照文档流程.
推送生产用的证书下载位置如图:
调试用的选择Development 选择对应的证书.
下载后,打开钥匙串
如图:
注意 这里一定要是拖进来 否则可能不会出现密匙
4.拖进去以后导出证书
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)