Skip to content

Commit 26f2bc7

Browse files
authored
fix(localPush): Fix local push notification does not show. (#153)
1 parent fab09da commit 26f2bc7

File tree

4 files changed

+166
-120
lines changed

4 files changed

+166
-120
lines changed

AndroidSDKCore/src/main/java/com/leanplum/Leanplum.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public class Leanplum {
7777
* Default event name to use for Purchase events.
7878
*/
7979
public static final String PURCHASE_EVENT_NAME = "Purchase";
80-
public static final String LEANPLUM_PUSH_SERVICE = "com.leanplum.LeanplumPushService";
80+
private static final String LEANPLUM_PUSH_SERVICE = "com.leanplum.LeanplumPushService";
8181

8282
private static final ArrayList<StartCallback> startHandlers = new ArrayList<>();
8383
private static final ArrayList<VariablesChangedCallback> variablesChangedHandlers =

AndroidSDKCore/src/main/java/com/leanplum/internal/ActionManager.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ public class ActionManager {
5151

5252
public static final String PUSH_NOTIFICATION_ACTION_NAME = "__Push Notification";
5353
public static final String HELD_BACK_ACTION_NAME = "__held_back";
54-
54+
private static final String LEANPLUM_LOCAL_PUSH_HELPER =
55+
"com.leanplum.internal.LeanplumLocalPushHelper";
5556
private static final String PREFERENCES_NAME = "__leanplum_messaging__";
5657

5758
public static class MessageMatchResult {
@@ -131,10 +132,11 @@ public boolean onResponse(ActionContext actionContext) {
131132
long eta = System.currentTimeMillis() + ((Number) countdownObj).longValue() * 1000L;
132133
// Schedule notification.
133134
try {
134-
return (boolean) Class.forName(Leanplum.LEANPLUM_PUSH_SERVICE)
135-
.getDeclaredMethod("scheduleLocalPush", Context.class, String.class,
135+
return (boolean) Class.forName(LEANPLUM_LOCAL_PUSH_HELPER)
136+
.getDeclaredMethod("scheduleLocalPush", ActionContext.class, String.class,
136137
long.class).invoke(new Object(), actionContext, messageId, eta);
137138
} catch (Throwable throwable) {
139+
Log.e("scheduleLocalPush problem",throwable);
138140
return false;
139141
}
140142
} catch (Throwable t) {
@@ -164,7 +166,7 @@ public boolean onResponse(ActionContext actionContext) {
164166

165167
// Cancel notification.
166168
try {
167-
Class.forName(Leanplum.LEANPLUM_PUSH_SERVICE)
169+
Class.forName(LEANPLUM_LOCAL_PUSH_HELPER)
168170
.getDeclaredMethod("cancelLocalPush", Context.class, String.class)
169171
.invoke(new Object(), context, messageId);
170172
boolean didCancel = existingEta > System.currentTimeMillis();

AndroidSDKPush/src/main/java/com/leanplum/LeanplumPushService.java

Lines changed: 0 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ public class LeanplumPushService {
9393
private static final String LEANPLUM_PUSH_SERVICE_GCM = "com.leanplum.LeanplumPushServiceGcm";
9494
private static final String LEANPLUM_PUSH_SERVICE_FCM = "com.leanplum.LeanplumPushServiceFcm";
9595

96-
private static final String PREFERENCES_NAME = "__leanplum_messaging__";
9796
private static final int NOTIFICATION_ID = 1;
9897
private static final String OPEN_URL = "Open URL";
9998
private static final String URL = "URL";
@@ -852,118 +851,4 @@ static void showDeviceRegistedPush(Context context, Context currentContext) {
852851
Log.i("Device is registered.");
853852
}
854853
}
855-
856-
/**
857-
* Schedule local push notification. This method will call by reflection from AndroidSDKCore.
858-
*
859-
* @param actionContext Action Context.
860-
* @param messageId String message id for local push notification.
861-
* @param eta Eta for local push notification.
862-
* @return True if notification was scheduled.
863-
*/
864-
static boolean scheduleLocalPush(ActionContext actionContext, String messageId, long eta) {
865-
try {
866-
Context context = Leanplum.getContext();
867-
Intent intentAlarm = new Intent(context, LeanplumLocalPushListenerService.class);
868-
AlarmManager alarmManager = (AlarmManager) context.getSystemService(
869-
Context.ALARM_SERVICE);
870-
871-
// If there's already one scheduled before the eta, discard this.
872-
// Otherwise, discard the scheduled one.
873-
SharedPreferences preferences = context.getSharedPreferences(
874-
PREFERENCES_NAME, Context.MODE_PRIVATE);
875-
long existingEta = preferences.getLong(String.format(
876-
Constants.Defaults.LOCAL_NOTIFICATION_KEY, messageId), 0L);
877-
if (existingEta > 0L && existingEta > System.currentTimeMillis()) {
878-
if (existingEta < eta) {
879-
return false;
880-
} else if (existingEta >= eta) {
881-
PendingIntent existingIntent = PendingIntent.getService(
882-
context, messageId.hashCode(), intentAlarm,
883-
PendingIntent.FLAG_UPDATE_CURRENT);
884-
alarmManager.cancel(existingIntent);
885-
}
886-
}
887-
888-
// Specify custom data for the notification
889-
Map<String, Serializable> data = actionContext.objectNamed("Advanced options.Data");
890-
if (data != null) {
891-
for (String key : data.keySet()) {
892-
intentAlarm.putExtra(key, data.get(key));
893-
}
894-
}
895-
896-
// Specify open action
897-
String openAction = actionContext.stringNamed(Constants.Values.DEFAULT_PUSH_ACTION);
898-
boolean muteInsideApp = Boolean.TRUE.equals(actionContext.objectNamed(
899-
"Advanced options.Mute inside app"));
900-
if (openAction != null) {
901-
if (muteInsideApp) {
902-
intentAlarm.putExtra(Constants.Keys.PUSH_MESSAGE_ID_MUTE_WITH_ACTION, messageId);
903-
} else {
904-
intentAlarm.putExtra(Constants.Keys.PUSH_MESSAGE_ID_NO_MUTE_WITH_ACTION, messageId);
905-
}
906-
} else {
907-
if (muteInsideApp) {
908-
intentAlarm.putExtra(Constants.Keys.PUSH_MESSAGE_ID_MUTE, messageId);
909-
} else {
910-
intentAlarm.putExtra(Constants.Keys.PUSH_MESSAGE_ID_NO_MUTE, messageId);
911-
}
912-
}
913-
914-
// Message.
915-
String message = actionContext.stringNamed("Message");
916-
intentAlarm.putExtra(Constants.Keys.PUSH_MESSAGE_TEXT,
917-
message != null ? message : Constants.Values.DEFAULT_PUSH_MESSAGE);
918-
919-
// Collapse key.
920-
String collapseKey = actionContext.stringNamed("Android options.Collapse key");
921-
if (collapseKey != null) {
922-
intentAlarm.putExtra("collapseKey", collapseKey);
923-
}
924-
925-
// Delay while idle.
926-
boolean delayWhileIdle = Boolean.TRUE.equals(actionContext.objectNamed(
927-
"Android options.Delay while idle"));
928-
if (delayWhileIdle) {
929-
intentAlarm.putExtra("delayWhileIdle", true);
930-
}
931-
932-
// Schedule notification.
933-
PendingIntent operation = PendingIntent.getService(
934-
context, messageId.hashCode(), intentAlarm,
935-
PendingIntent.FLAG_UPDATE_CURRENT);
936-
alarmManager.set(AlarmManager.RTC_WAKEUP, eta, operation);
937-
938-
// Save notification so we can cancel it later.
939-
SharedPreferences.Editor editor = preferences.edit();
940-
editor.putLong(String.format(Constants.Defaults.LOCAL_NOTIFICATION_KEY, messageId), eta);
941-
SharedPreferencesUtil.commitChanges(editor);
942-
943-
Log.i("Scheduled notification.");
944-
return true;
945-
} catch (Throwable t) {
946-
Util.handleException(t);
947-
return false;
948-
}
949-
}
950-
951-
/**
952-
* Cancel local push notification. This method will call by reflection from AndroidSDKCore.
953-
*
954-
* @param context The application context.
955-
* @param messageId Message id of notification that should be canceled.
956-
*/
957-
static void cancelLocalPush(Context context, String messageId) {
958-
try {
959-
Intent intentAlarm = new Intent(context, LeanplumLocalPushListenerService.class);
960-
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
961-
PendingIntent existingIntent = PendingIntent.getService(
962-
context, messageId.hashCode(), intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
963-
if (alarmManager != null && existingIntent != null) {
964-
alarmManager.cancel(existingIntent);
965-
}
966-
} catch (Throwable ignored) {
967-
}
968-
}
969854
}
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
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

Comments
 (0)