|
| 1 | +/* |
| 2 | + * Copyright 2017, 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 | +package com.leanplum; |
| 22 | + |
| 23 | +import android.content.Context; |
| 24 | +import android.os.Bundle; |
| 25 | +import android.support.v4.app.NotificationCompat; |
| 26 | +import android.text.TextUtils; |
| 27 | + |
| 28 | +import com.leanplum.internal.JsonConverter; |
| 29 | +import com.leanplum.internal.Log; |
| 30 | + |
| 31 | +import java.util.Map; |
| 32 | + |
| 33 | +/** |
| 34 | + * LeanplumNotificationHelper helper class for push notifications. |
| 35 | + * |
| 36 | + * @author Anna Orlova |
| 37 | + */ |
| 38 | +class LeanplumNotificationHelper { |
| 39 | + /** |
| 40 | + * If notification channels are supported this method will try to create |
| 41 | + * NotificationCompat.Builder with default notification channel if default channel id is provided. |
| 42 | + * If notification channels not supported this method will return NotificationCompat.Builder for |
| 43 | + * context. |
| 44 | + * |
| 45 | + * @param context The application context. |
| 46 | + * @param isNotificationChannelSupported True if notification channels are supported. |
| 47 | + * @return NotificationCompat.Builder for provided context or null. |
| 48 | + */ |
| 49 | + // NotificationCompat.Builder(Context context) constructor was deprecated in API level 26. |
| 50 | + @SuppressWarnings("deprecation") |
| 51 | + static NotificationCompat.Builder getDefaultNotificationBuilder(Context context, |
| 52 | + boolean isNotificationChannelSupported) { |
| 53 | + if (!isNotificationChannelSupported) { |
| 54 | + return new NotificationCompat.Builder(context); |
| 55 | + } |
| 56 | + String channelId = LeanplumNotificationChannel.getDefaultNotificationChannelId(context); |
| 57 | + if (!TextUtils.isEmpty(channelId)) { |
| 58 | + return new NotificationCompat.Builder(context, channelId); |
| 59 | + } else { |
| 60 | + Log.w("Failed to post notification, there are no notification channels configured."); |
| 61 | + return null; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * If notification channels are supported this method will try to create a channel with |
| 67 | + * information from the message if it doesn't exist and return NotificationCompat.Builder for this |
| 68 | + * channel. In the case where no channel information inside the message, we will try to get a |
| 69 | + * channel with default channel id. If notification channels not supported this method will return |
| 70 | + * NotificationCompat.Builder for context. |
| 71 | + * |
| 72 | + * @param context The application context. |
| 73 | + * @param message Push notification Bundle. |
| 74 | + * @return NotificationCompat.Builder or null. |
| 75 | + */ |
| 76 | + // NotificationCompat.Builder(Context context) constructor was deprecated in API level 26. |
| 77 | + @SuppressWarnings("deprecation") |
| 78 | + static NotificationCompat.Builder getNotificationBuilder(Context context, Bundle message) { |
| 79 | + NotificationCompat.Builder builder = null; |
| 80 | + // If we are targeting API 26, try to find supplied channel to post notification. |
| 81 | + if (LeanplumNotificationChannel.isNotificationChannelSupported(context)) { |
| 82 | + try { |
| 83 | + String channel = message.getString("lp_channel"); |
| 84 | + if (!TextUtils.isEmpty(channel)) { |
| 85 | + // Create channel if it doesn't exist and post notification to that channel. |
| 86 | + Map<String, Object> channelDetails = JsonConverter.fromJson(channel); |
| 87 | + String channelId = LeanplumNotificationChannel.createNotificationChannel(context, |
| 88 | + channelDetails); |
| 89 | + if (!TextUtils.isEmpty(channelId)) { |
| 90 | + builder = new NotificationCompat.Builder(context, channelId); |
| 91 | + } else { |
| 92 | + Log.w("Failed to post notification to specified channel."); |
| 93 | + } |
| 94 | + } else { |
| 95 | + // If channel isn't supplied, try to look up for default channel. |
| 96 | + builder = LeanplumNotificationHelper.getDefaultNotificationBuilder(context, true); |
| 97 | + } |
| 98 | + } catch (Exception e) { |
| 99 | + Log.e("Failed to post notification to specified channel."); |
| 100 | + } |
| 101 | + } else { |
| 102 | + builder = new NotificationCompat.Builder(context); |
| 103 | + } |
| 104 | + return builder; |
| 105 | + } |
| 106 | +} |
0 commit comments