|
|
@@ -1,27 +1,10 @@
|
|
|
-/*
|
|
|
- * Copyright 2020. Huawei TechnoLogUtilies Co., Ltd. All rights reserved.
|
|
|
- *
|
|
|
- * Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
- * you may not use this file except in compliance with the License.
|
|
|
- * You may obtain a copy of the License at
|
|
|
-
|
|
|
- * http://www.apache.org/licenses/LICENSE-2.0
|
|
|
- *
|
|
|
- * Unless required by applicable law or agreed to in writing, software
|
|
|
- * distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
- * See the License for the specific language governing permissions and
|
|
|
- * limitations under the License.
|
|
|
- */
|
|
|
-
|
|
|
package cn.seecoder.courselearningdemo.service;
|
|
|
|
|
|
-import android.content.Intent;
|
|
|
import android.text.TextUtils;
|
|
|
+import android.util.Log;
|
|
|
|
|
|
import com.huawei.hms.push.HmsMessageService;
|
|
|
import com.huawei.hms.push.RemoteMessage;
|
|
|
-import com.huawei.hms.push.SendException;
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
|
@@ -38,10 +21,9 @@ public class MyPushService extends HmsMessageService {
|
|
|
private static final String TAG = "MyPushService";
|
|
|
|
|
|
/**
|
|
|
- * When an app calls the getToken method to apply for a token from the server,
|
|
|
- * if the server does not return the token during current method calling, the server can return the token through this method later.
|
|
|
- * This method callback must be completed in 10 seconds. Otherwise, you need to start a new Job for callback processing.
|
|
|
- * @param token token
|
|
|
+ * Android 10.0及以上版本的设备上,调用getToken方法直接返回Token。如果当次调用失败会缓存申请,之后会自动重试申请,成功后则以onNewToken方法返回。
|
|
|
+ * 低于Android 10.0的设备上,调用getToken方法返回为空,确保推送服务开通的情况下,Token值以onNewToken方法返回。
|
|
|
+ * 此处覆写了onNewToken方法,将接收到的非空的Token值发送给服务器。
|
|
|
*/
|
|
|
@Override
|
|
|
public void onNewToken(String token) {
|
|
|
@@ -52,6 +34,7 @@ public class MyPushService extends HmsMessageService {
|
|
|
refreshedTokenToServer(token);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
private void refreshedTokenToServer(String token) {
|
|
|
if(UserDao.getUser() == null || !StringUtil.hasText(UserDao.getUser().getUid())) return;
|
|
|
Call<ResultVO<String>> refreshUserTokenCall = RetrofitClient.userService.refreshUserToken(UserDao.getUser().getUid(), token);
|
|
|
@@ -69,19 +52,18 @@ public class MyPushService extends HmsMessageService {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * This method is used to receive downstream data messages.
|
|
|
- * This method callback must be completed in 10 seconds. Otherwise, you need to start a new Job for callback processing.
|
|
|
- *
|
|
|
+ * 该方法用于接收透传消息方法
|
|
|
* @param message RemoteMessage
|
|
|
*/
|
|
|
@Override
|
|
|
public void onMessageReceived(RemoteMessage message) {
|
|
|
- System.out.println("onMessageReceived is called");
|
|
|
LogUtil.i(TAG, "onMessageReceived is called");
|
|
|
if (message == null) {
|
|
|
LogUtil.e(TAG, "Received message entity is null!");
|
|
|
return;
|
|
|
}
|
|
|
+ // 这里仅将消息解析出来打印到日志
|
|
|
+
|
|
|
// getCollapseKey() Obtains the classification identifier (collapse key) of a message.
|
|
|
// getData() Obtains valid content data of a message.
|
|
|
// getMessageId() Obtains the ID of a message.
|
|
|
@@ -136,45 +118,14 @@ public class MyPushService extends HmsMessageService {
|
|
|
+ "\n getLink: " + notification.getLink()
|
|
|
+ "\n getNotifyId: " + notification.getNotifyId());
|
|
|
}
|
|
|
-
|
|
|
- Intent intent = new Intent();
|
|
|
- intent.putExtra("method", "onMessageReceived");
|
|
|
- intent.putExtra("msg", "onMessageReceived called, message id:" + message.getMessageId() + ", payload data:"
|
|
|
- + message.getData());
|
|
|
-
|
|
|
- sendBroadcast(intent);
|
|
|
-
|
|
|
- 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) {
|
|
|
- LogUtil.d(TAG, "Start new Job processing.");
|
|
|
- }
|
|
|
-
|
|
|
- private void processWithin10s(RemoteMessage message) {
|
|
|
- LogUtil.d(TAG, "Processing now.");
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void onMessageSent(String msgId) {
|
|
|
- LogUtil.i(TAG, "onMessageSent called, Message id:" + msgId);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void onSendError(String msgId, Exception exception) {
|
|
|
- LogUtil.i(TAG, "onSendError called, message id:" + msgId + ", ErrCode:"
|
|
|
- + ((SendException) exception).getErrorCode() + ", description:" + exception.getMessage());
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 申请Token失败时会回调该方法。
|
|
|
+ */
|
|
|
@Override
|
|
|
public void onTokenError(Exception e) {
|
|
|
super.onTokenError(e);
|
|
|
+ Log.e(TAG, "onTokenError! error info: " +e.getMessage() + " caused by: "+ e.getCause());
|
|
|
}
|
|
|
}
|