Skip to content

Commit 204a9d8

Browse files
committed
refactor(push): Add LeanplumNotificationHelper class.
1 parent 1cb0879 commit 204a9d8

File tree

4 files changed

+92
-47
lines changed

4 files changed

+92
-47
lines changed

AndroidSDK/src/com/leanplum/Leanplum.java

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -843,28 +843,12 @@ public void onResponse(boolean success) {
843843
@Override
844844
public void run() {
845845
try {
846-
NotificationCompat.Builder builder = null;
847-
// If we are targeting API 26, try to post notification to default channel.
848-
if (LeanplumNotificationChannel.isNotificationChannelSupported(context)) {
849-
try {
850-
String channelId = LeanplumNotificationChannel.getDefaultNotificationChannelId(context);
851-
if (!TextUtils.isEmpty(channelId)) {
852-
builder = new NotificationCompat.Builder(context, channelId);
853-
} else {
854-
Log.w("Failed to post notification, there are no notification channels configured.");
855-
return;
856-
}
857-
} catch (Exception e) {
858-
Log.e("Failed to post notification, there are no notification channels configured.");
859-
}
860-
} else {
861-
builder = new NotificationCompat.Builder(context);
862-
}
863-
846+
NotificationCompat.Builder builder =
847+
LeanplumNotificationHelper.getDefaultNotificationBuilder(context,
848+
LeanplumNotificationChannel.isNotificationChannelSupported(context));
864849
if (builder == null) {
865850
return;
866851
}
867-
868852
builder.setSmallIcon(android.R.drawable.star_on)
869853
.setContentTitle("Leanplum")
870854
.setContentText("Your device is registered.");

AndroidSDK/src/com/leanplum/LeanplumFcmServiceHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
*
3131
* @author Anna Orlova
3232
*/
33-
public class LeanplumFcmServiceHelper extends IntentService {
33+
class LeanplumFcmServiceHelper extends IntentService {
3434
public LeanplumFcmServiceHelper() {
3535
super("LeanplumFcmServiceHelper");
3636
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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 class.
35+
*
36+
* @author Anna Orlova
37+
*/
38+
class LeanplumNotificationHelper {
39+
40+
// NotificationCompat.Builder(Context context) constructor was deprecated in API level 26.
41+
@SuppressWarnings("deprecation")
42+
static NotificationCompat.Builder getDefaultNotificationBuilder(Context context,
43+
boolean isNotificationChannelSupported) {
44+
if (!isNotificationChannelSupported) {
45+
return new NotificationCompat.Builder(context);
46+
}
47+
String channelId = LeanplumNotificationChannel.getDefaultNotificationChannelId(context);
48+
if (!TextUtils.isEmpty(channelId)) {
49+
return new NotificationCompat.Builder(context, channelId);
50+
} else {
51+
Log.w("Failed to post notification, there are no notification channels configured.");
52+
return null;
53+
}
54+
}
55+
56+
// NotificationCompat.Builder(Context context) constructor was deprecated in API level 26.
57+
@SuppressWarnings("deprecation")
58+
static NotificationCompat.Builder getNotificationBuilder(Context context, Bundle message) {
59+
NotificationCompat.Builder builder = null;
60+
// If we are targeting API 26, try to find supplied channel to post notification.
61+
if (LeanplumNotificationChannel.isNotificationChannelSupported(context)) {
62+
try {
63+
String channel = message.getString("lp_channel");
64+
if (!TextUtils.isEmpty(channel)) {
65+
// Create channel if it doesn't exist and post notification to that channel.
66+
Map<String, Object> channelDetails = JsonConverter.fromJson(channel);
67+
String channelId = LeanplumNotificationChannel.createNotificationChannel(context,
68+
channelDetails);
69+
if (!TextUtils.isEmpty(channelId)) {
70+
builder = new NotificationCompat.Builder(context, channelId);
71+
} else {
72+
Log.w("Failed to post notification to specified channel.");
73+
}
74+
} else {
75+
// If channel isn't supplied, try to look up for default channel.
76+
builder = LeanplumNotificationHelper.getDefaultNotificationBuilder(context, true);
77+
}
78+
} catch (Exception e) {
79+
Log.e("Failed to post notification to specified channel.");
80+
}
81+
} else {
82+
builder = new NotificationCompat.Builder(context);
83+
}
84+
return builder;
85+
}
86+
}

AndroidSDK/src/com/leanplum/LeanplumPushService.java

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -318,33 +318,8 @@ private static void showNotification(Context context, Bundle message) {
318318
if (message.getString("title") != null) {
319319
title = message.getString("title");
320320
}
321-
NotificationCompat.Builder builder = null;
322-
323-
// If we are targeting API 26, try to find supplied channel to post notification.
324-
if (LeanplumNotificationChannel.isNotificationChannelSupported(context)) {
325-
try {
326-
String channel = message.getString("lp_channel");
327-
if (!TextUtils.isEmpty(channel)) {
328-
// Create channel if it doesn't exist and post notification to that channel.
329-
Map<String, Object> channelDetails = JsonConverter.fromJson(channel);
330-
String channelId = LeanplumNotificationChannel.createNotificationChannel(context, channelDetails);
331-
builder = new NotificationCompat.Builder(context, channelId);
332-
} else {
333-
// If channel isn't supplied, try to look up for default channel.
334-
String channelId = LeanplumNotificationChannel.getDefaultNotificationChannelId(context);
335-
if (!TextUtils.isEmpty(channelId)) {
336-
builder = new NotificationCompat.Builder(context, channelId);
337-
} else {
338-
Log.w("Failed to post notification, there are no notification channels configured.");
339-
return;
340-
}
341-
}
342-
} catch (Exception e) {
343-
Log.e("Failed to post notification to specified channel.");
344-
}
345-
} else {
346-
builder = new NotificationCompat.Builder(context);
347-
}
321+
NotificationCompat.Builder builder =
322+
LeanplumNotificationHelper.getNotificationBuilder(context, message);
348323

349324
if (builder == null) {
350325
return;

0 commit comments

Comments
 (0)