|
| 1 | +/* |
| 2 | + * Copyright 2021, Leanplum, Inc. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 5 | + * or more contributor license agreements. See the NOTICE file |
| 6 | + * distributed with this work for additional information |
| 7 | + * regarding copyright ownership. The ASF licenses this file |
| 8 | + * to you under the Apache License, Version 2.0 (the |
| 9 | + * "License"); you may not use this file except in compliance |
| 10 | + * with the License. You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, |
| 15 | + * software distributed under the License is distributed on an |
| 16 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 17 | + * KIND, either express or implied. See the License for the |
| 18 | + * specific language governing permissions and limitations |
| 19 | + * under the License. |
| 20 | + */ |
| 21 | + |
| 22 | +package com.leanplum; |
| 23 | + |
| 24 | +import android.content.Context; |
| 25 | +import android.os.Bundle; |
| 26 | +import com.huawei.hms.push.HmsMessageService; |
| 27 | +import com.huawei.hms.push.RemoteMessage; |
| 28 | +import com.leanplum.internal.Constants; |
| 29 | +import com.leanplum.internal.Constants.Keys; |
| 30 | +import com.leanplum.internal.Log; |
| 31 | +import com.leanplum.internal.OperationQueue; |
| 32 | +import com.leanplum.internal.Util; |
| 33 | +import java.util.Map; |
| 34 | + |
| 35 | +/** |
| 36 | + * This class encapsulates functionality for handling notification messages and registration ID from |
| 37 | + * Huawei Push Kit. Needs to be called from your instance of {@link HmsMessageService}. |
| 38 | + */ |
| 39 | +public class LeanplumHmsHandler { |
| 40 | + |
| 41 | + public void onCreate(Context context) { |
| 42 | + Leanplum.setApplicationContext(context); |
| 43 | + } |
| 44 | + |
| 45 | + public void onNewToken(String token, Context context) { |
| 46 | + if (Util.isMainThread()) { |
| 47 | + OperationQueue.sharedInstance().addParallelOperation(() -> onNewTokenImpl(token, context)); |
| 48 | + } else { |
| 49 | + onNewTokenImpl(token, context); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + private void onNewTokenImpl(String token, Context context) { |
| 54 | + // Send token to backend |
| 55 | + LeanplumPushService.getPushProviders().setRegistrationId(PushProviderType.HMS, token); |
| 56 | + } |
| 57 | + |
| 58 | + public void onMessageReceived(RemoteMessage remoteMessage, Context context) { |
| 59 | + if (Util.isMainThread()) { |
| 60 | + OperationQueue.sharedInstance().addParallelOperation( |
| 61 | + () -> onMessageReceivedImpl(remoteMessage, context)); |
| 62 | + } else { |
| 63 | + onMessageReceivedImpl(remoteMessage, context); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private void onMessageReceivedImpl(RemoteMessage remoteMessage, Context context) { |
| 68 | + try { |
| 69 | + Map<String, String> messageMap = remoteMessage.getDataOfMap(); |
| 70 | + String channel = PushTracking.CHANNEL_HMS; |
| 71 | + if (messageMap.containsKey(Constants.Keys.PUSH_MESSAGE_TEXT)) { |
| 72 | + Bundle notification = getBundle(messageMap); |
| 73 | + notification.putString(Keys.CHANNEL_INTERNAL_KEY, channel); |
| 74 | + LeanplumPushService.handleNotification(context, notification); |
| 75 | + } |
| 76 | + Log.i("Received HMS notification message: %s", messageMap.toString()); |
| 77 | + } catch (Throwable t) { |
| 78 | + Log.exception(t); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + private Bundle getBundle(Map<String, String> messageMap) { |
| 83 | + Bundle bundle = new Bundle(); |
| 84 | + if (messageMap != null) { |
| 85 | + for (Map.Entry<String, String> entry : messageMap.entrySet()) { |
| 86 | + bundle.putString(entry.getKey(), entry.getValue()); |
| 87 | + } |
| 88 | + } |
| 89 | + return bundle; |
| 90 | + } |
| 91 | +} |
0 commit comments