华为推送遇到的两个值得注意的点个
1、开发者通过在APP服务器端调用HUAWEI Push接口,向HUAWEI Push平台发送推送消息;
2、HUAWEI Push平台将消息发给安装了相应APP的终端设备。
在开发者联盟注册开发者帐号,并在开发者管理台上创建应用以及配置应用信息和开通HUAWEI Push服务;
使用Android Studio IDE创建Project,并配置接入HUAWEI Push依赖的SDK;
根据HUAWEI Push开发指南,实现华为Push开放服务的API调用以及调试。
您将建立什么
在这个Codelab中,你将使用已经创建好的Demo Project实现对华为Push开放服务的API调用,通过Demo Project你可以体验到:
向HUAWEI Push服务端申请Token;
接收HUAWEI Push服务端推送的测试消息。
1、集成华为依赖库的时候注意点
先 app gradle 添加
implementation 'com.huawei.hms:push:4.0.0.300'
再 项目gradle 底部添加
第一点
repositories {
google()
jcenter()
mavenCentral()
maven { url 'http://developer.huawei.com/repo/' }
}
第二点
dependencies {
classpath 'com.huawei.agconnect:agcp:1.2.0.300'
}
第三点
allprojects {
repositories {
google()
jcenter()
mavenLocal()
mavenCentral()
maven {
url 'https://maven.google.com'
}
maven { url 'http://developer.huawei.com/repo/' }
}
}
这三点一个不能少要不然会提示 依赖失败!!!!
2 配置获取token
官网第六部 AndroidManifest.xml 配置 只提示说要自定义写一个集成它HmsMessageService的服务类,却没说还要在application节点下配置一个
<meta-data
android:name="com.huawei.hms.client.appid"
android:value="my_appid" />
如果不配置获取不到token,官网也没提示,连demo里面也没看到,所以这个地方很容就被忽视。
获取token注意点:
1、建议在APP启动后首个activity的onCreate方法调用getToken方法。
调用getInstance方法获取HmsInstanceId类实例:
HmsInstanceId inst = HmsInstanceId.getInstance(context);
2、调用getToken方法获取Token:
public void getToken() {
new Thread() {
@Override
public void run() {
try {
String getToken = HmsInstanceId.getInstance(context).getToken(appId, "HCM");
if (!TextUtils.isEmpty(getToken)) {
//TODO: Send token to your app server.
}
} catch (Exception e) {
Log.e(TAG, "getToken failed.", e);
}
}
}.start();
}
3、Token发生变化时或者EMUI版本低于10.0以onNewToken方法返回。
示例代码:
public class DemoHmsMessageService extends HmsMessageService {
@Override
public void onNewToken(String token) {
Log.d(TAG, "Refreshed token: " + token);
// send the token to your app server.
if (!TextUtils.isEmpty(token)) {
sendRegTokenToServer(token);
}
}
private void sendRegTokenToServer(String token) {
// TODO: Implement this method to send token to your app server.
}
}
吐槽点
1、只有透传的消息前台接收消息的时候 才进入HmsMessageService这个服务类里面的onMessageReceived这个回调方法,发送通知只有EMUI 9.1.0以上和HMS SDK 4.0以上支持。
App如果订阅了主题消息或者服务器主动推送的透传消息(包括通知消息前台展示功能中设置通知消息由应用自己处理),都需要App通过实现 onMessageReceived回调方法来接收消息,收到消息的后续行为由应用自己处理。
@Override
public void onMessageReceived(RemoteMessage message) {
Log.i(TAG, "onMessageReceived is called");
if (message == null) {
Log.e(TAG, "Received message entity is null!");
return;
}
Log.i(TAG, "getCollapseKey: " + message.getCollapseKey()
+ "\n getData: " + message.getData()
+ "\n getFrom: " + message.getFrom()
+ "\n getTo: " + message.getTo()
+ "\n getMessageId: " + message.getMessageId()
+ "\n getOriginalUrgency: " + message.getOriginalUrgency()
+ "\n getUrgency: " + message.getUrgency()
+ "\n getSendTime: " + message.getSentTime()
+ "\n getMessageType: " + message.getMessageType()
+ "\n getTtl: " + message.getTtl());
RemoteMessage.Notification notification = message.getNotification();
if (notification != null) {
Log.i(TAG, "\n getImageUrl: " + notification.getImageUrl()
+ "\n getTitle: " + notification.getTitle()
+ "\n getTitleLocalizationKey: " + notification.getTitleLocalizationKey()
+ "\n getTitleLocalizationArgs: " + Arrays.toString(notification.getTitleLocalizationArgs())
+ "\n getBody: " + notification.getBody()
+ "\n getBodyLocalizationKey: " + notification.getBodyLocalizationKey()
+ "\n getBodyLocalizationArgs: " + Arrays.toString(notification.getBodyLocalizationArgs())
+ "\n getIcon: " + notification.getIcon()
+ "\n getSound: " + notification.getSound()
+ "\n getTag: " + notification.getTag()
+ "\n getColor: " + notification.getColor()
+ "\n getClickAction: " + notification.getClickAction()
+ "\n getChannelId: " + notification.getChannelId()
+ "\n getLink: " + notification.getLink()
+ "\n getNotifyId: " + notification.getNotifyId());
}
Boolean judgeWhetherIn10s = false;
// If the messages are not processed in 10 seconds, the app needs to use WorkManager for processing.
if (judgeWhetherIn10s) {
startWorkManagerJob(message);
} else {
// Process message within 10s
processWithin10s(message);
}
}
private void startWorkManagerJob(RemoteMessage message) {
Log.d(TAG, "Start new job processing.");
}
private void processWithin10s(RemoteMessage message) {
Log.d(TAG, "Processing now.");
}