package com.tejia.lijin.app.service;
|
|
import android.content.Intent;
|
import android.util.Log;
|
|
import com.huawei.hms.push.HmsMessageService;
|
import com.huawei.hms.push.RemoteMessage;
|
import com.huawei.hms.push.SendException;
|
import com.tejia.lijin.app.ShoppingApi;
|
import com.tejia.lijin.app.util.user.UserUtil;
|
import com.yeshi.push.entity.PushRomType;
|
|
import java.util.Arrays;
|
|
public class HWPushService extends HmsMessageService {
|
|
|
private static final String TAG = "HWPushService";
|
private final static String CODELABS_ACTION = "com.huawei.codelabpush.action";
|
|
@Override
|
public void onCreate() {
|
Log.i(TAG, "onCreate");
|
super.onCreate();
|
}
|
|
/**
|
* 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
|
*/
|
@Override
|
public void onNewToken(String token) {
|
Log.i(TAG, "onNewToken:" + token);
|
ShoppingApi.bindPushToken(getApplicationContext(), UserUtil.getUid(getApplicationContext()), PushRomType.huawei, token, null);
|
}
|
|
private void refreshedTokenToServer(String token) {
|
Log.i(TAG, "sending token to server. token:" + token);
|
}
|
|
/**
|
* 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) {
|
Log.i(TAG, "onMessageReceived is called");
|
if (message == null) {
|
Log.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.
|
// getMessageType() Obtains the type of a message.
|
// getNotification() Obtains the notification data instance from a message.
|
// getOriginalUrgency() Obtains the original priority of a message.
|
// getSentTime() Obtains the time when a message is sent from the server.
|
// getTo() Obtains the recipient of a message.
|
|
Log.i(TAG, "getCollapseKey: " + message.getCollapseKey()
|
+ "\n getData: " + message.getData()
|
+ "\n getFrom: " + message.getFrom()
|
+ "\n getTo: " + message.getTo()
|
+ "\n getMessageId: " + message.getMessageId()
|
+ "\n getMessageType: " + message.getMessageType()
|
+ "\n getSendTime: " + message.getSentTime()
|
+ "\n getTtl: " + message.getTtl()
|
+ "\n getSendMode: " + message.getSendMode()
|
+ "\n getReceiptMode: " + message.getReceiptMode()
|
+ "\n getOriginalUrgency: " + message.getOriginalUrgency()
|
+ "\n getUrgency: " + message.getUrgency()
|
+ "\n getToken: " + message.getToken());
|
|
// getBody() Obtains the displayed content of a message
|
// getTitle() Obtains the title of a message
|
// getTitleLocalizationKey() Obtains the key of the displayed title of a notification message
|
// getTitleLocalizationArgs() Obtains variable parameters of the displayed title of a message
|
// getBodyLocalizationKey() Obtains the key of the displayed content of a message
|
// getBodyLocalizationArgs() Obtains variable parameters of the displayed content of a message
|
// getIcon() Obtains icons from a message
|
// getSound() Obtains the sound from a message
|
// getTag() Obtains the tag from a message for message overwriting
|
// getColor() Obtains the colors of icons in a message
|
// getClickAction() Obtains actions triggered by message tapping
|
// getChannelId() Obtains IDs of channels that support the display of messages
|
// getImageUrl() Obtains the image URL from a message
|
// getLink() Obtains the URL to be accessed from a message
|
// getNotifyId() Obtains the unique ID of a message
|
|
RemoteMessage.Notification notification = message.getNotification();
|
if (notification != null) {
|
Log.i(TAG, "\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 getImageUrl: " + notification.getImageUrl()
|
+ "\n getSound: " + notification.getSound()
|
+ "\n getTag: " + notification.getTag()
|
+ "\n getColor: " + notification.getColor()
|
+ "\n getClickAction: " + notification.getClickAction()
|
+ "\n getIntentUri: " + notification.getIntentUri()
|
+ "\n getChannelId: " + notification.getChannelId()
|
+ "\n getLink: " + notification.getLink()
|
+ "\n getNotifyId: " + notification.getNotifyId()
|
+ "\n isDefaultLight: " + notification.isDefaultLight()
|
+ "\n isDefaultSound: " + notification.isDefaultSound()
|
+ "\n isDefaultVibrate: " + notification.isDefaultVibrate()
|
+ "\n getWhen: " + notification.getWhen()
|
+ "\n getLightSettings: " + Arrays.toString(notification.getLightSettings())
|
+ "\n isLocalOnly: " + notification.isLocalOnly()
|
+ "\n getBadgeNumber: " + notification.getBadgeNumber()
|
+ "\n isAutoCancel: " + notification.isAutoCancel()
|
+ "\n getImportance: " + notification.getImportance()
|
+ "\n getTicker: " + notification.getTicker()
|
+ "\n getVibrateConfig: " + Arrays.toString(notification.getVibrateConfig())
|
+ "\n getVisibility: " + notification.getVisibility());
|
}
|
|
Intent intent = new Intent();
|
intent.setAction(CODELABS_ACTION);
|
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) {
|
Log.d(TAG, "Start new Job processing.");
|
}
|
|
private void processWithin10s(RemoteMessage message) {
|
Log.d(TAG, "Processing now.");
|
}
|
|
@Override
|
public void onMessageSent(String msgId) {
|
Log.i(TAG, "onMessageSent called, Message id:" + msgId);
|
Intent intent = new Intent();
|
intent.setAction(CODELABS_ACTION);
|
intent.putExtra("method", "onMessageSent");
|
intent.putExtra("msg", "onMessageSent called, Message id:" + msgId);
|
|
sendBroadcast(intent);
|
}
|
|
@Override
|
public void onSendError(String msgId, Exception exception) {
|
Log.i(TAG, "onSendError called, message id:" + msgId + ", ErrCode:"
|
+ ((SendException) exception).getErrorCode() + ", description:" + exception.getMessage());
|
|
Intent intent = new Intent();
|
intent.setAction(CODELABS_ACTION);
|
intent.putExtra("method", "onSendError");
|
intent.putExtra("msg", "onSendError called, message id:" + msgId + ", ErrCode:"
|
+ ((SendException) exception).getErrorCode() + ", description:" + exception.getMessage());
|
|
sendBroadcast(intent);
|
}
|
|
@Override
|
|
public void onTokenError(Exception e) {
|
super.onTokenError(e);
|
e.printStackTrace();
|
Log.i(TAG, "onTokenError:" + e.getMessage());
|
}
|
}
|