Skip to content

Commit 79a6d12

Browse files
Implement cross-platform push completion handling for background tasks.
Added Display.notifyPushCompletion() to manually signal the completion of a background push task (e.g. playing audio). On iOS, this delays the call to the system completion handler if the `ios.delayPushCompletion` or `delayPushCompletion` property is set. On Android, this releases a partial WakeLock acquired when the push is received, preventing the device from sleeping during the task if the `android.delayPushCompletion` or `delayPushCompletion` property is set. Updated `IOSImplementation`, `AndroidImplementation`, and `PushNotificationService` to support this logic.
1 parent cebd1b5 commit 79a6d12

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

Ports/Android/src/com/codename1/impl/android/AndroidImplementation.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@
5252
import android.graphics.drawable.Drawable;
5353
import android.media.AudioManager;
5454
import android.net.Uri;
55-
import android.os.Vibrator;
55+
import android.os.Vibrator;
56+
import android.os.PowerManager;
5657
import android.telephony.TelephonyManager;
5758
import android.util.DisplayMetrics;
5859
import android.util.Log;
@@ -280,6 +281,19 @@ public static void setActivity(CodenameOneActivity aActivity) {
280281
private int displayHeight;
281282
static CodenameOneActivity activity;
282283
static ComponentName activityComponentName;
284+
private static PowerManager.WakeLock pushWakeLock;
285+
public static void acquirePushWakeLock(long timeout) {
286+
if (getContext() == null) return;
287+
try {
288+
if (pushWakeLock == null) {
289+
PowerManager pm = (PowerManager) getContext().getSystemService(Context.POWER_SERVICE);
290+
pushWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "CN1:PushWakeLock");
291+
}
292+
pushWakeLock.acquire(timeout);
293+
} catch (Exception ex) {
294+
com.codename1.io.Log.e(ex);
295+
}
296+
}
283297

284298
private static Context context;
285299
RelativeLayout relativeLayout;
@@ -2784,6 +2798,17 @@ public void exitApplication() {
27842798
}
27852799

27862800
@Override
2801+
@Override
2802+
public void notifyPushCompletion() {
2803+
if (pushWakeLock != null && pushWakeLock.isHeld()) {
2804+
try {
2805+
pushWakeLock.release();
2806+
} catch (Exception ex) {
2807+
com.codename1.io.Log.e(ex);
2808+
}
2809+
}
2810+
}
2811+
27872812
public void notifyCommandBehavior(int commandBehavior) {
27882813
if (commandBehavior == Display.COMMAND_BEHAVIOR_NATIVE) {
27892814
if (getActivity() instanceof CodenameOneActivity) {

Ports/Android/src/com/codename1/impl/android/PushNotificationService.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,17 @@ public void onDestroy() {
109109
public void push(final String value) {
110110
final PushCallback callback = getPushCallbackInstance();
111111
if(callback != null) {
112+
AndroidImplementation.acquirePushWakeLock(30000);
112113
Display.getInstance().callSerially(new Runnable() {
113114
public void run() {
114-
callback.push(value);
115+
try {
116+
callback.push(value);
117+
} finally {
118+
if (!"true".equals(Display.getInstance().getProperty("delayPushCompletion", "false")) &&
119+
!"true".equals(Display.getInstance().getProperty("android.delayPushCompletion", "false"))) {
120+
Display.getInstance().notifyPushCompletion();
121+
}
122+
}
115123
}
116124
});
117125
} else {

Ports/iOSPort/src/com/codename1/impl/ios/IOSImplementation.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8115,7 +8115,10 @@ public void run() {
81158115

81168116
pushCallback.push(message);
81178117
} finally {
8118-
nativeInstance.firePushCompletionHandler();
8118+
if (!"true".equals(Display.getInstance().getProperty("delayPushCompletion", "false")) &&
8119+
!"true".equals(Display.getInstance().getProperty("ios.delayPushCompletion", "false"))) {
8120+
nativeInstance.firePushCompletionHandler();
8121+
}
81198122
}
81208123
}
81218124
});

0 commit comments

Comments
 (0)