Skip to content

Commit f603618

Browse files
milos1290hrishileanplum
authored andcommitted
E2-1983: Fixing local notifications not fired (#371)
* Fixing local notifications not fired * Adding javadocs
1 parent 04ecb11 commit f603618

File tree

4 files changed

+116
-28
lines changed

4 files changed

+116
-28
lines changed
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
<manifest package="com.leanplum.push"
2-
xmlns:android="http://schemas.android.com/apk/res/android">
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.leanplum.push">
34

45
<application>
6+
<receiver android:name="com.leanplum.LeanplumJobStartReceiver" />
57
<!-- Leanplum FCM Registration Service. -->
6-
<service android:name="com.leanplum.LeanplumPushRegistrationService"/>
8+
<service android:name="com.leanplum.LeanplumPushRegistrationService" />
79
<!-- Leanplum Local Push Notification Service. -->
8-
<service android:name="com.leanplum.LeanplumLocalPushListenerService"/>
10+
<service
11+
android:name="com.leanplum.LeanplumLocalPushListenerService"
12+
android:permission="android.permission.BIND_JOB_SERVICE" />
913
</application>
10-
</manifest>
14+
15+
</manifest>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2019, 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.BroadcastReceiver;
25+
import android.content.Context;
26+
import android.content.Intent;
27+
28+
import androidx.core.app.JobIntentService;
29+
30+
import com.leanplum.internal.Log;
31+
import com.leanplum.internal.Util;
32+
33+
/**
34+
* Broadcast receiver used for starting any JobIntentService.
35+
* Received intent needs to have {@link LeanplumJobStartReceiver#LP_EXTRA_SERVICE_CLASS} and
36+
* {@link LeanplumJobStartReceiver#LP_EXTRA_JOB_ID} populated to successfully enqueue work.
37+
*/
38+
public class LeanplumJobStartReceiver extends BroadcastReceiver {
39+
40+
public static final String LP_EXTRA_SERVICE_CLASS = "com.leanplum.service_class";
41+
public static final String LP_EXTRA_JOB_ID = "com.leanplum.service_job_id";
42+
43+
@Override
44+
public void onReceive(Context context, Intent intent) {
45+
try {
46+
if (intent.getExtras() == null) {
47+
Log.w("Cannot enqueue work on JobIntentService, no extras in intent.");
48+
return;
49+
}
50+
51+
String serviceName = intent.getStringExtra(LP_EXTRA_SERVICE_CLASS);
52+
int jobId = intent.getIntExtra(LP_EXTRA_JOB_ID, 0);
53+
54+
Class service = Class.forName(serviceName);
55+
if (!JobIntentService.class.isAssignableFrom(service)) {
56+
Log.w("The service provided is not a type of JobIntentService.");
57+
return;
58+
}
59+
60+
intent.setClass(context, service);
61+
62+
JobIntentService.enqueueWork(context, service, jobId, intent);
63+
} catch (Exception e) {
64+
Util.handleException(e);
65+
}
66+
}
67+
}

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

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@
2121

2222
package com.leanplum;
2323

24-
import android.app.IntentService;
24+
import android.content.Context;
2525
import android.content.Intent;
2626
import android.os.Bundle;
2727

28+
import androidx.annotation.NonNull;
29+
import androidx.core.app.JobIntentService;
30+
2831
import com.leanplum.internal.Constants;
2932
import com.leanplum.internal.Log;
3033
import com.leanplum.internal.Util;
@@ -34,24 +37,37 @@
3437
*
3538
* @author Aleksandar Gyorev
3639
*/
37-
public class LeanplumLocalPushListenerService extends IntentService {
38-
public LeanplumLocalPushListenerService() {
39-
super("LeanplumLocalPushListenerService");
40-
}
41-
42-
@Override
43-
protected void onHandleIntent(Intent intent) {
44-
try {
45-
if (intent == null) {
46-
Log.e("The intent cannot be null");
47-
return;
48-
}
49-
Bundle extras = intent.getExtras();
50-
if (extras != null && extras.containsKey(Constants.Keys.PUSH_MESSAGE_TEXT)) {
51-
LeanplumPushService.handleNotification(this, extras);
52-
}
53-
} catch (Throwable t) {
54-
Util.handleException(t);
40+
public class LeanplumLocalPushListenerService extends JobIntentService {
41+
42+
private static final String LP_CLASS_NAME = LeanplumLocalPushListenerService.class.getName();
43+
private static final int LP_JOB_ID = 1;
44+
45+
/**
46+
* Convenience method that returns Intent which can be used to start the job.
47+
* @param context Surrounding context.
48+
* @return Intent with class name and job id.
49+
*/
50+
public static Intent getIntent(Context context) {
51+
Intent intent = new Intent();
52+
intent.putExtra(LeanplumJobStartReceiver.LP_EXTRA_SERVICE_CLASS, LP_CLASS_NAME);
53+
intent.putExtra(LeanplumJobStartReceiver.LP_EXTRA_JOB_ID, LP_JOB_ID);
54+
intent.setClass(context, LeanplumJobStartReceiver.class);
55+
return intent;
56+
}
57+
58+
@Override
59+
protected void onHandleWork(@NonNull Intent intent) {
60+
try {
61+
if (intent == null) {
62+
Log.e("The intent cannot be null");
63+
return;
64+
}
65+
Bundle extras = intent.getExtras();
66+
if (extras != null && extras.containsKey(Constants.Keys.PUSH_MESSAGE_TEXT)) {
67+
LeanplumPushService.handleNotification(this, extras);
68+
}
69+
} catch (Throwable t) {
70+
Util.handleException(t);
71+
}
5572
}
56-
}
5773
}

AndroidSDKPush/src/main/java/com/leanplum/internal/LeanplumLocalPushHelper.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class LeanplumLocalPushHelper {
5454
static boolean scheduleLocalPush(ActionContext actionContext, String messageId, long eta) {
5555
try {
5656
Context context = Leanplum.getContext();
57-
Intent intentAlarm = new Intent(context, LeanplumLocalPushListenerService.class);
57+
Intent intentAlarm = LeanplumLocalPushListenerService.getIntent(context);
5858
AlarmManager alarmManager = (AlarmManager) context.getSystemService(
5959
Context.ALARM_SERVICE);
6060

@@ -68,7 +68,7 @@ static boolean scheduleLocalPush(ActionContext actionContext, String messageId,
6868
if (existingEta < eta) {
6969
return false;
7070
} else if (existingEta >= eta) {
71-
PendingIntent existingIntent = PendingIntent.getService(
71+
PendingIntent existingIntent = PendingIntent.getBroadcast(
7272
context, messageId.hashCode(), intentAlarm,
7373
PendingIntent.FLAG_UPDATE_CURRENT);
7474
alarmManager.cancel(existingIntent);
@@ -120,7 +120,7 @@ static boolean scheduleLocalPush(ActionContext actionContext, String messageId,
120120
}
121121

122122
// Schedule notification.
123-
PendingIntent operation = PendingIntent.getService(
123+
PendingIntent operation = PendingIntent.getBroadcast(
124124
context, messageId.hashCode(), intentAlarm,
125125
PendingIntent.FLAG_UPDATE_CURRENT);
126126
alarmManager.set(AlarmManager.RTC_WAKEUP, eta, operation);

0 commit comments

Comments
 (0)