博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
华为推送 简介 集成
阅读量:7063 次
发布时间:2019-06-28

本文共 16776 字,大约阅读时间需要 55 分钟。

账号【谢总@asp.citic.com】【icitic52818888】
应用包名:com.bqt.push
APP ID:100257971
文档:

集成步骤

1、注册成为开发者
2、 ,创建应用
点击“会员中心”,“创建产品”,在“创建产品”页面选择“移动应用”,创建新的应用。
填写移动应用的基本信息:应用名称、应用类型和默认语言,点击“上传APK”或者“产品详情”,完成产品创建。
3、生成证书指纹
  • 打开命令行工具,执行cd命令进入keytool.exe所在的目录:
cd C:\Program Files\Java\jdk1.8.0_92\bin
1
1
 
1
cd C:\Program Files\Java\jdk1.8.0_92\bin
  • 获取对应的SHA256指纹
keytool -list -v -keystore C:\_zxwk\zxwlke.keystore
1
1
 
1
keytool -list -v -keystore C:\_zxwk\zxwlke.keystore
  • 获取的信息
密钥库类型: JKS密钥库提供方: SUN您的密钥库包含 1 个条目别名: zxwk创建日期: 2014-11-21条目类型: PrivateKeyEntry证书链长度: 1证书[1]:所有者: CN=zxwk, OU=zxwk, O=zxwk, L=wuhan, C=CN发布者: CN=zxwk, OU=zxwk, O=zxwk, L=wuhan, C=CN序列号: 546ede5e有效期开始日期: Fri Nov 21 14:40:30 CST 2014, 截止日期: Sun Oct 28 14:40:30 CST 2114证书指纹:         MD5: DD:C9:E3:2A:77:D3:21:05:A4:C6:20:2F:EB:75:65:5C         SHA1: DA:FF:CF:09:E9:59:32:39:E7:0D:22:C5:3A:1C:CD:C8:9B:19:FC:7A         SHA256: 6D:3C:C4:74:46:E4:A4:27:4D:5F:97:53:E6:8D:EE:41:7F:FE:88:92:80:DE:02:F9:2B:76:76:92:E2:80:34:F8         签名算法名称: SHA1withRSA         版本: 3
20
20
 
1
密钥库类型: JKS
2
密钥库提供方: SUN
3
 
4
您的密钥库包含 1 个条目
5
 
6
别名: zxwk
7
创建日期: 2014-11-21
8
条目类型: PrivateKeyEntry
9
证书链长度: 1
10
证书[1]:
11
所有者: CN=zxwk, OU=zxwk, O=zxwk, L=wuhan, C=CN
12
发布者: CN=zxwk, OU=zxwk, O=zxwk, L=wuhan, C=CN
13
序列号: 546ede5e
14
有效期开始日期: Fri Nov 21 14:40:30 CST 2014, 截止日期: Sun Oct 28 14:40:30 CST 2114
15
证书指纹:
16
        MD5: DD:C9:E3:2A:77:D3:21:05:A4:C6:20:2F:EB:75:65:5C
17
        SHA1: DA:FF:CF:09:E9:59:32:39:E7:0D:22:C5:3A:1C:CD:C8:9B:19:FC:7A
18
        SHA256: 6D:3C:C4:74:46:E4:A4:27:4D:5F:97:53:E6:8D:EE:41:7F:FE:88:92:80:DE:02:F9:2B:76:76:92:E2:80:34:F8
19
        签名算法名称: SHA1withRSA
20
        版本: 3
4、申请PUSH服务
登录“华为开发者联盟”,点击“会员中心”,
在“我的产品”页面,选择创建的应用,进入产品详情页面,点击产品服务列表的“+”号添加PUSH服务。
在配置信息界面填写应用包名、回调地址、SHA256指纹,
应用包名必须和APK包名保持一致,且不可更改。
回调地址配置后可修改。
5、获取PUSH服务参数
开发者为APP开通PUSH服务后,华为方会分配服务参数,用于开发者集成HMS SDK时使用。获取服务参数操作如下。
登录“华为开发者联盟”,
选择“会员中心 > 我的产品”,点击产品名称,进入指定的产品页面。
点击“移动应用详情”链接,在弹出的“移动服务详情”对话框获取服务参数。
产品名称:测试应用包名:com.bqt.pushAPP ID:100257971        applicationID,华为开发者联盟为APP分配的唯一标识APP SECRET:785bf3e10d53b0420b79a6c2ef97981a     应用秘钥,在开发PUSH 服务端时需要用到此参数
4
4
 
1
产品名称:测试
2
应用包名:com.bqt.push
3
APP ID:100257971        applicationID,华为开发者联盟为APP分配的唯一标识
4
APP SECRET:785bf3e10d53b0420b79a6c2ef97981a     应用秘钥,在开发PUSH 服务端时需要用到此参数

开发前自检

参数 类型 来源 意义
Keystore-file 文件 自行创建 Android应用签名文件,和华为开发者联盟申请应用服务时填写的签名文件指纹匹配
Appid 字符串 开发者联盟网站应用服务详情 开发者联盟给应用分配的唯一标识
应用包名 字符串 开发者联盟网站创建应用时填写的包名 Android应用的包名要与这个包名一致
APP SECRET 字符串 开发者联盟,“移动应用详情”界面 PUSH 服务端开发时发送消息需要
注意:请务必确保开发环境和开发者联盟上使用的签名文件一致,否则连接华为移动服务将失败,错误码为 6003 或 907135702。

简洁版自定义消息推送Demo

获取需要的SDK

1、去 下载 HMS Agent套件(必选)
795730-20180419174258269-212610259.png
这货是非常非常非常坑爹的,我日你个巴拉!下载之后会发现里面杂七杂八什么都系都有,有华为支付、华为账号、华为...等所有华为开放的服务,而且还是一个module!麻辣隔壁的,我就用一个推送(其他推送SDK只需写一个广播接受者就可以了),你这货让我导入这么多东西怎么能忍!更坑爹的是,里面common包下有十几个文件,这是所有服务通用的包,就算把其他服务去掉,只有这是几个类这也是不能忍的!
2、运行
Agent下的【GetHMSAgent.bat】文件
草你麻痹的,我移了半天,越搞错误越多,完全不能忍!这时就仔细看了下解压包根目录那一坨坨都是些什么垃圾东西,结果发现了新大陆!
点击运行
GetHMSAgent.bat文件可以从全量的 HMSAgent 代码,根据您的选择删除不需要的代码!
795730-20180419174258503-999062855.png
这些爽翻了!
不过用的时候要注意,运行过程会让你输入cpid,我们没有这玩意,随意输几个就可以了。
3、运行后我们只需把 hms 包移到我们的项目中就可以了。
795730-20180419174258712-650245025.png

初始化

public class App extends Application {	@Override	public void onCreate() {		super.onCreate();		boolean success = HMSAgent.init(this);		Log.i("bqt", ""+success);			}		@Override	public void onTerminate() {		super.onTerminate();		HMSAgent.destroy();	}}
15
15
 
1
public class App extends Application {
2
@Override
3
public void onCreate() {
4
super.onCreate();
5
boolean success = HMSAgent.init(this);
6
Log.i("bqt", ""+success);
7
 
8
}
9
 
10
@Override
11
public void onTerminate() {
12
super.onTerminate();
13
HMSAgent.destroy();
14
}
15
}

Activity

public class HuaweiPushActivity extends ListActivity {		public static boolean isForeground = false;		private TextView tv;	private String[] array;		protected void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		tv = new TextView(this);		tv.setTextColor(Color.BLUE);		getListView().addFooterView(tv);		array = new String[]{"获取token",				"删除token",				" 获取push状态",				"设置是否接收普通透传消息",				"设置接收通知消息",				"显示push协议",};		setListAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, new ArrayList<>(Arrays.asList(array))));		EventBus.getDefault().register(this);	}		private boolean b;		@Override	protected void onListItemClick(ListView l, View v, int position, long id) {		tv.append("\n" + array[position] + "  ");		b = !b;		switch (position) {			case 0:				HMSAgent.Push.getToken((rst) -> tv.append("+rst"));				break;			case 1://token=0990005841682350300001697200CN01  belongId=1				String token = getSharedPreferences("huawei_token", Context.MODE_PRIVATE).getString("token", null);				HMSAgent.Push.deleteToken(token, rst -> tv.append("" + rst));				break;			case 2:				HMSAgent.Push.getPushState(rst -> tv.append("" + rst));				break;			case 3:				HMSAgent.Push.enableReceiveNormalMsg(b, rst -> tv.append(b + "   " + rst));				break;			case 4:				HMSAgent.Push.enableReceiveNotifyMsg(b, rst -> tv.append(b + "   " + rst));				break;			case 5:				HMSAgent.Push.queryAgreement(rst -> tv.append("" + rst));				break;		}	}		@Override	protected void onResume() {		isForeground = true;		super.onResume();	}		@Override	protected void onPause() {		isForeground = false;		super.onPause();	}		@Override	protected void onDestroy() {		super.onDestroy();		EventBus.getDefault().unregister(this);	}		@Subscribe(threadMode = ThreadMode.MAIN)	public void onPushEvent(BasePushBean bean) {		TextView tv = new TextView(this);		tv.setTextColor(Color.BLUE);		tv.setText(bean.msg);		getListView().addFooterView(tv);	}}
77
 
1
public class HuaweiPushActivity extends ListActivity {
2
 
3
public static boolean isForeground = false;
4
 
5
private TextView tv;
6
private String[] array;
7
 
8
protected void onCreate(Bundle savedInstanceState) {
9
super.onCreate(savedInstanceState);
10
tv = new TextView(this);
11
tv.setTextColor(Color.BLUE);
12
getListView().addFooterView(tv);
13
array = new String[]{
"获取token",
14
"删除token",
15
" 获取push状态",
16
"设置是否接收普通透传消息",
17
"设置接收通知消息",
18
"显示push协议",};
19
setListAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, new ArrayList<>(Arrays.asList(array))));
20
EventBus.getDefault().register(this);
21
}
22
 
23
private boolean b;
24
 
25
@Override
26
protected void onListItemClick(ListView l, View v, int position, long id) {
27
tv.append("\n" + array[position] + "  ");
28
b = !b;
29
switch (position) {
30
case 0:
31
HMSAgent.Push.getToken((rst) -> tv.append("+rst"));
32
break;
33
case 1://token=0990005841682350300001697200CN01  belongId=1
34
String token = getSharedPreferences("huawei_token", Context.MODE_PRIVATE).getString("token", null);
35
HMSAgent.Push.deleteToken(token, rst -> tv.append("" + rst));
36
break;
37
case 2:
38
HMSAgent.Push.getPushState(rst -> tv.append("" + rst));
39
break;
40
case 3:
41
HMSAgent.Push.enableReceiveNormalMsg(b, rst -> tv.append(b + "   " + rst));
42
break;
43
case 4:
44
HMSAgent.Push.enableReceiveNotifyMsg(b, rst -> tv.append(b + "   " + rst));
45
break;
46
case 5:
47
HMSAgent.Push.queryAgreement(rst -> tv.append("" + rst));
48
break;
49
}
50
}
51
 
52
@Override
53
protected void onResume() {
54
isForeground = true;
55
super.onResume();
56
}
57
 
58
@Override
59
protected void onPause() {
60
isForeground = false;
61
super.onPause();
62
}
63
 
64
@Override
65
protected void onDestroy() {
66
super.onDestroy();
67
EventBus.getDefault().unregister(this);
68
}
69
 
70
@Subscribe(threadMode = ThreadMode.MAIN)
71
public void onPushEvent(BasePushBean bean) {
72
TextView tv = new TextView(this);
73
tv.setTextColor(Color.BLUE);
74
tv.setText(bean.msg);
75
getListView().addFooterView(tv);
76
}
77
}

Receiver

public class HuaweiPushRevicer extends PushReceiver {		/**	 * 这里接收到穿透消息,自定义通知栏,和操作	 *	 * @param context 上下文	 * @param msg     消息	 * @param bundle  bundle	 * @return 不清楚返回真假的作用	 */	@Override	public boolean onPushMsg(Context context, byte[] msg, Bundle bundle) {		try {			String content = new String(msg, "UTF-8");			Log.i("bqt", "华为【onPushMsg】" + content);			PushMsgReceiverHelper.getInstance().onHuaweiPushMsgReceiver(content);		} catch (Exception e) {			e.printStackTrace();		}		return false;	}		@Override	public void onToken(Context context, String token, Bundle extras) {		Log.i("bqt", "华为【onToken】token=" + token + "  belongId=" + extras.getString("belongId"));		//保存在本地备用		context.getSharedPreferences("huawei_token", Context.MODE_PRIVATE).edit().putString("token", token).apply();		Toast.makeText(context, token, Toast.LENGTH_SHORT).show();	}		/**	 * 这里接收到的是通知栏消息,无法自定义操作,默认打开应用	 *	 * @param context 上下文	 * @param event   事件	 * @param extras  额外	 */	public void onEvent(Context context, Event event, Bundle extras) {		int notifyId = extras.getInt(BOUND_KEY.pushNotifyId, 0);		String message = extras.getString(BOUND_KEY.pushMsgKey);		Log.i("bqt", "华为【onEvent】event=" + event + "  notifyId=" + notifyId + "  message=" + message);				if (Event.NOTIFICATION_OPENED.equals(event) || Event.NOTIFICATION_CLICK_BTN.equals(event)) {			NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);			if (0 != notifyId && manager != null) {				manager.cancel(notifyId);			}		}		super.onEvent(context, event, extras);	}		/**	 * 得到华为推送的连接状态	 *	 * @param context   上下文	 * @param pushState 连接状态	 */	@Override	public void onPushState(Context context, boolean pushState) {		Log.i("bqt", "华为【onPushState】连接状态:" + pushState);	}}
62
 
1
public class HuaweiPushRevicer extends PushReceiver {
2
 
3
/**
4
* 这里接收到穿透消息,自定义通知栏,和操作
5
*
6
* @param context 上下文
7
* @param msg     消息
8
* @param bundle  bundle
9
* @return 不清楚返回真假的作用
10
*/
11
@Override
12
public boolean onPushMsg(Context context, byte[] msg, Bundle bundle) {
13
try {
14
String content = new String(msg, "UTF-8");
15
Log.i("bqt", "华为【onPushMsg】" + content);
16
PushMsgReceiverHelper.getInstance().onHuaweiPushMsgReceiver(content);
17
} catch (Exception e) {
18
e.printStackTrace();
19
}
20
return false;
21
}
22
 
23
@Override
24
public void onToken(Context context, String token, Bundle extras) {
25
Log.i("bqt", "华为【onToken】token=" + token + "  belongId=" + extras.getString("belongId"));
26
//保存在本地备用
27
context.getSharedPreferences("huawei_token", Context.MODE_PRIVATE).edit().putString("token", token).apply();
28
Toast.makeText(context, token, Toast.LENGTH_SHORT).show();
29
}
30
 
31
/**
32
* 这里接收到的是通知栏消息,无法自定义操作,默认打开应用
33
*
34
* @param context 上下文
35
* @param event   事件
36
* @param extras  额外
37
*/
38
public void onEvent(Context context, Event event, Bundle extras) {
39
int notifyId = extras.getInt(BOUND_KEY.pushNotifyId, 0);
40
String message = extras.getString(BOUND_KEY.pushMsgKey);
41
Log.i("bqt", "华为【onEvent】event=" + event + "  notifyId=" + notifyId + "  message=" + message);
42
 
43
if (Event.NOTIFICATION_OPENED.equals(event) || Event.NOTIFICATION_CLICK_BTN.equals(event)) {
44
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
45
if (0 != notifyId && manager != null) {
46
manager.cancel(notifyId);
47
}
48
}
49
super.onEvent(context, event, extras);
50
}
51
 
52
/**
53
* 得到华为推送的连接状态
54
*
55
* @param context   上下文
56
* @param pushState 连接状态
57
*/
58
@Override
59
public void onPushState(Context context, boolean pushState) {
60
Log.i("bqt", "华为【onPushState】连接状态:" + pushState);
61
}
62
}

消息处理类

/** * 处理华为光推送推过来的自定义消息 */public void onHuaweiPushMsgReceiver(String message) {	Log.i("bqt", "【华为推送】" + message);	if (HuaweiPushActivity.isForeground) {		EventBus.getDefault().post(new BasePushBean(message, BasePushBean.TYPE_STRING));	}}
 
1
/**
2
* 处理华为光推送推过来的自定义消息
3
*/
4
public void onHuaweiPushMsgReceiver(String message) {
5
Log.i("bqt", "【华为推送】" + message);
6
if (HuaweiPushActivity.isForeground) {
7
EventBus.getDefault().post(new BasePushBean(message, BasePushBean.TYPE_STRING));
8
}
9
}

配置文件

project的
build.gradle
allprojects {    repositories {        google()        jcenter()        maven {url 'http://developer.huawei.com/repo'}//华为maven仓路径    }}
7
7
 
1
allprojects {
2
   repositories {
3
       google()
4
       jcenter()
5
       maven {
url 'http://developer.huawei.com/repo'}//华为maven仓路径
6
   }
7
}
module的build.gradle
implementation 'com.huawei.android.hms:push:2.6.0.301'
 
1
implementation 'com.huawei.android.hms:push:2.6.0.301'
// 签名配置signingConfigs {	release {		storeFile file("./push.keystore")		storePassword "***"		keyAlias "zxwk"		keyPassword "***"	}}
x
 
1
// 签名配置
2
signingConfigs {
3
release {
4
storeFile file("./push.keystore")
5
storePassword "***"
6
keyAlias "zxwk"
7
keyPassword "***"
8
}
9
}
混淆文件:proguard-android.txt
-ignorewarning-keepattributes *Annotation*-keepattributes Exceptions-keepattributes InnerClasses-keepattributes Signature-keepattributes SourceFile,LineNumberTable-keep class com.hianalytics.android.**{*;}-keep class com.huawei.updatesdk.**{*;}-keep class com.huawei.hms.**{*;}-keep class com.huawei.gamebox.plugin.gameservice.**{*;}-keep public class com.huawei.android.hms.agent.** extends android.app.Activity { public *; protected *; }-keep interface com.huawei.android.hms.agent.common.INoProguard {*;}-keep class * extends com.huawei.android.hms.agent.common.INoProguard {*;}
15
15
 
1
-ignorewarning
2
-keepattributes *Annotation*
3
-keepattributes Exceptions
4
-keepattributes InnerClasses
5
-keepattributes Signature
6
-keepattributes SourceFile,LineNumberTable
7
-keep class com.hianalytics.android.**{
*;}
8
-keep class com.huawei.updatesdk.**{
*;}
9
-keep class com.huawei.hms.**{
*;}
10
 
11
-keep class com.huawei.gamebox.plugin.gameservice.**{
*;}
12
 
13
-keep public class com.huawei.android.hms.agent.** extends android.app.Activity { public *; protected *; }
14
-keep interface com.huawei.android.hms.agent.common.INoProguard {
*;}
15
-keep class * extends com.huawei.android.hms.agent.common.INoProguard {
*;}    

AndroidManifest.xml

添加权限:
5
5
 
1
2
3
4
5
在application节点下增加APPID:
3
3
 
1
 
2
   android:name="com.huawei.hms.client.appid"
3
   android:value="appid=100257971"/>
注册四大组件:
91
91
 
1
2
3
 
4
android:name=".hms.agent.common.HMSAgentActivity"
5
android:configChanges="orientation|locale|screenSize|layoutDirection|fontScale"
6
android:excludeFromRecents="true"
7
android:exported="false"
8
android:hardwareAccelerated="true"
9
android:theme="@android:style/Theme.Translucent" >
10
 
11
android:name="hwc-theme"
12
android:value="androidhwext:style/Theme.Emui.Translucent" />
13
 
14
15
 
16
android:name="com.huawei.hms.activity.BridgeActivity"
17
android:configChanges="orientation|locale|screenSize|layoutDirection|fontScale"
18
android:excludeFromRecents="true"
19
android:exported="false"
20
android:hardwareAccelerated="true"
21
android:theme="@android:style/Theme.Translucent">
22
 
23
android:name="hwc-theme"
24
android:value="androidhwext:style/Theme.Emui.Translucent"/>
25
 
26
 
27
28
 
29
android:name="com.huawei.updatesdk.service.otaupdate.AppUpdateActivity"
30
android:configChanges="orientation|screenSize"
31
android:exported="false"
32
android:theme="@style/upsdkDlDialog">
33
 
34
android:name="hwc-theme"
35
android:value="androidhwext:style/Theme.Emui.Translucent.NoTitleBar"/>
36
 
37
 
38
39
 
40
android:name="com.huawei.updatesdk.support.pm.PackageInstallerActivity"
41
android:configChanges="orientation|keyboardHidden|screenSize"
42
android:exported="false"
43
android:theme="@style/upsdkDlDialog">
44
 
45
android:name="hwc-theme"
46
android:value="androidhwext:style/Theme.Emui.Translucent"/>
47
 
48
 
49
50
51
52
53
54
55
56
57
58
59
60
61
 
62
 
63
 
64
65
66
67
68
 
69
 
70
 
71
72
73
 
74
android:name="com.huawei.hms.update.provider.UpdateProvider"
75
android:authorities="com.bqt.push.hms.update.provider"
76
android:exported="false"
77
android:grantUriPermissions="true"/>
78
 
79
80
 
81
android:name="com.huawei.updatesdk.fileprovider.UpdateSdkFileProvider"
82
android:authorities="com.bqt.push.updateSdk.fileProvider"
83
android:exported="false"
84
android:grantUriPermissions="true">
85
 
86
 
87
88
89
 
90
android:name="com.huawei.updatesdk.service.deamon.download.DownloadService"
91
android:exported="false"/>
2018-4-19

转载地址:http://hvnll.baihongyu.com/

你可能感兴趣的文章
多进程写入文件
查看>>
【运维趟坑回忆录 开篇】初入初创, 一脸懵
查看>>
你学不懂C语言,是因为不懂编写C程序的7个步骤 ...
查看>>
Prometheus VS InfluxDB
查看>>
《码出高效》学习笔记与书中错误记录
查看>>
东超科技获得千万级Pre-A轮融资,投资方为中科创星 ...
查看>>
翻译 | The Principles of OOD 面向对象设计原则
查看>>
个推基于Consul的配置管理
查看>>
zabbix安装
查看>>
阿里云RDS PostgreSQL GPU加速规格(支持GIS时空加速)发布
查看>>
Netflix Media Database - 架构设计和实现
查看>>
云HBase Spark分析引擎对接云数据库POLARDB
查看>>
揭秘 DockerCon 重量级演讲嘉宾(六)
查看>>
性能优化技巧 - 集群维表
查看>>
python 静态文件以及一个项目框架
查看>>
组复制官方翻译七、Requirements and Limitations
查看>>
2019年4月上旬值得一读的10本技术书籍(Python、大数据、深度学习等)!
查看>>
微信钱包入口开发笔记
查看>>
[error] 17755#0: *58522 readv() failed (104: Connection reset by peer) while reading upstream
查看>>
如何从CRM中获益?看专家怎么说!
查看>>