|
| 1 | +/* |
| 2 | + * Copyright 2018, 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.internal; |
| 23 | + |
| 24 | +import android.app.AlarmManager; |
| 25 | +import android.app.PendingIntent; |
| 26 | +import android.content.Context; |
| 27 | +import android.content.Intent; |
| 28 | +import android.content.SharedPreferences; |
| 29 | + |
| 30 | +import com.leanplum.ActionContext; |
| 31 | +import com.leanplum.Leanplum; |
| 32 | +import com.leanplum.LeanplumLocalPushListenerService; |
| 33 | +import com.leanplum.utils.SharedPreferencesUtil; |
| 34 | + |
| 35 | +import java.io.Serializable; |
| 36 | +import java.util.Map; |
| 37 | + |
| 38 | +/** |
| 39 | + * Leanplum local push notification helper class. |
| 40 | + * |
| 41 | + * @author Anna Orlova |
| 42 | + */ |
| 43 | +class LeanplumLocalPushHelper { |
| 44 | + private static final String PREFERENCES_NAME = "__leanplum_messaging__"; |
| 45 | + |
| 46 | + /** |
| 47 | + * Schedule local push notification. This method will call by reflection from AndroidSDKCore. |
| 48 | + * |
| 49 | + * @param actionContext Action Context. |
| 50 | + * @param messageId String message id for local push notification. |
| 51 | + * @param eta Eta for local push notification. |
| 52 | + * @return True if notification was scheduled. |
| 53 | + */ |
| 54 | + static boolean scheduleLocalPush(ActionContext actionContext, String messageId, long eta) { |
| 55 | + try { |
| 56 | + Context context = Leanplum.getContext(); |
| 57 | + Intent intentAlarm = new Intent(context, LeanplumLocalPushListenerService.class); |
| 58 | + AlarmManager alarmManager = (AlarmManager) context.getSystemService( |
| 59 | + Context.ALARM_SERVICE); |
| 60 | + |
| 61 | + // If there's already one scheduled before the eta, discard this. |
| 62 | + // Otherwise, discard the scheduled one. |
| 63 | + SharedPreferences preferences = context.getSharedPreferences( |
| 64 | + PREFERENCES_NAME, Context.MODE_PRIVATE); |
| 65 | + long existingEta = preferences.getLong(String.format( |
| 66 | + Constants.Defaults.LOCAL_NOTIFICATION_KEY, messageId), 0L); |
| 67 | + if (existingEta > 0L && existingEta > System.currentTimeMillis()) { |
| 68 | + if (existingEta < eta) { |
| 69 | + return false; |
| 70 | + } else if (existingEta >= eta) { |
| 71 | + PendingIntent existingIntent = PendingIntent.getService( |
| 72 | + context, messageId.hashCode(), intentAlarm, |
| 73 | + PendingIntent.FLAG_UPDATE_CURRENT); |
| 74 | + alarmManager.cancel(existingIntent); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + // Specify custom data for the notification |
| 79 | + Map<String, Serializable> data = actionContext.objectNamed("Advanced options.Data"); |
| 80 | + if (data != null) { |
| 81 | + for (String key : data.keySet()) { |
| 82 | + intentAlarm.putExtra(key, data.get(key)); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + // Specify open action |
| 87 | + String openAction = actionContext.stringNamed(Constants.Values.DEFAULT_PUSH_ACTION); |
| 88 | + boolean muteInsideApp = Boolean.TRUE.equals(actionContext.objectNamed( |
| 89 | + "Advanced options.Mute inside app")); |
| 90 | + if (openAction != null) { |
| 91 | + if (muteInsideApp) { |
| 92 | + intentAlarm.putExtra(Constants.Keys.PUSH_MESSAGE_ID_MUTE_WITH_ACTION, messageId); |
| 93 | + } else { |
| 94 | + intentAlarm.putExtra(Constants.Keys.PUSH_MESSAGE_ID_NO_MUTE_WITH_ACTION, messageId); |
| 95 | + } |
| 96 | + } else { |
| 97 | + if (muteInsideApp) { |
| 98 | + intentAlarm.putExtra(Constants.Keys.PUSH_MESSAGE_ID_MUTE, messageId); |
| 99 | + } else { |
| 100 | + intentAlarm.putExtra(Constants.Keys.PUSH_MESSAGE_ID_NO_MUTE, messageId); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + // Message. |
| 105 | + String message = actionContext.stringNamed("Message"); |
| 106 | + intentAlarm.putExtra(Constants.Keys.PUSH_MESSAGE_TEXT, |
| 107 | + message != null ? message : Constants.Values.DEFAULT_PUSH_MESSAGE); |
| 108 | + |
| 109 | + // Collapse key. |
| 110 | + String collapseKey = actionContext.stringNamed("Android options.Collapse key"); |
| 111 | + if (collapseKey != null) { |
| 112 | + intentAlarm.putExtra("collapseKey", collapseKey); |
| 113 | + } |
| 114 | + |
| 115 | + // Delay while idle. |
| 116 | + boolean delayWhileIdle = Boolean.TRUE.equals(actionContext.objectNamed( |
| 117 | + "Android options.Delay while idle")); |
| 118 | + if (delayWhileIdle) { |
| 119 | + intentAlarm.putExtra("delayWhileIdle", true); |
| 120 | + } |
| 121 | + |
| 122 | + // Schedule notification. |
| 123 | + PendingIntent operation = PendingIntent.getService( |
| 124 | + context, messageId.hashCode(), intentAlarm, |
| 125 | + PendingIntent.FLAG_UPDATE_CURRENT); |
| 126 | + alarmManager.set(AlarmManager.RTC_WAKEUP, eta, operation); |
| 127 | + |
| 128 | + // Save notification so we can cancel it later. |
| 129 | + SharedPreferences.Editor editor = preferences.edit(); |
| 130 | + editor.putLong(String.format(Constants.Defaults.LOCAL_NOTIFICATION_KEY, messageId), eta); |
| 131 | + SharedPreferencesUtil.commitChanges(editor); |
| 132 | + |
| 133 | + Log.i("Scheduled notification."); |
| 134 | + return true; |
| 135 | + } catch (Throwable t) { |
| 136 | + Util.handleException(t); |
| 137 | + return false; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Cancel local push notification. This method will call by reflection from AndroidSDKCore. |
| 143 | + * |
| 144 | + * @param context The application context. |
| 145 | + * @param messageId Message id of notification that should be canceled. |
| 146 | + */ |
| 147 | + static void cancelLocalPush(Context context, String messageId) { |
| 148 | + try { |
| 149 | + Intent intentAlarm = new Intent(context, LeanplumLocalPushListenerService.class); |
| 150 | + AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); |
| 151 | + PendingIntent existingIntent = PendingIntent.getService( |
| 152 | + context, messageId.hashCode(), intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT); |
| 153 | + if (alarmManager != null && existingIntent != null) { |
| 154 | + alarmManager.cancel(existingIntent); |
| 155 | + } |
| 156 | + } catch (Throwable ignored) { |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments