Skip to content

Commit c918783

Browse files
authored
Merge pull request #81 from Leanplum/feature/feat-channels-registration
feat(channel): add channel to notification "Your device is registered.”.
2 parents 6ec1bf2 + 013a5d8 commit c918783

File tree

4 files changed

+124
-42
lines changed

4 files changed

+124
-42
lines changed

AndroidSDK/src/com/leanplum/Leanplum.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -843,18 +843,22 @@ public void onResponse(boolean success) {
843843
@Override
844844
public void run() {
845845
try {
846-
NotificationCompat.Builder mBuilder =
847-
new NotificationCompat.Builder(currentContext)
848-
.setSmallIcon(android.R.drawable.star_on)
849-
.setContentTitle("Leanplum")
850-
.setContentText("Your device is registered.");
851-
mBuilder.setContentIntent(PendingIntent.getActivity(
846+
NotificationCompat.Builder builder =
847+
LeanplumNotificationHelper.getDefaultNotificationBuilder(context,
848+
LeanplumNotificationChannel.isNotificationChannelSupported(context));
849+
if (builder == null) {
850+
return;
851+
}
852+
builder.setSmallIcon(android.R.drawable.star_on)
853+
.setContentTitle("Leanplum")
854+
.setContentText("Your device is registered.");
855+
builder.setContentIntent(PendingIntent.getActivity(
852856
currentContext.getApplicationContext(), 0, new Intent(), 0));
853857
NotificationManager mNotificationManager =
854858
(NotificationManager) currentContext.getSystemService(
855859
Context.NOTIFICATION_SERVICE);
856860
// mId allows you to update the notification later on.
857-
mNotificationManager.notify(0, mBuilder.build());
861+
mNotificationManager.notify(0, builder.build());
858862
} catch (Throwable t) {
859863
Log.i("Device is registered.");
860864
}

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: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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 helper class for push notifications.
35+
*
36+
* @author Anna Orlova
37+
*/
38+
class LeanplumNotificationHelper {
39+
/**
40+
* If notification channels are supported this method will try to create
41+
* NotificationCompat.Builder with default notification channel if default channel id is provided.
42+
* If notification channels not supported this method will return NotificationCompat.Builder for
43+
* context.
44+
*
45+
* @param context The application context.
46+
* @param isNotificationChannelSupported True if notification channels are supported.
47+
* @return NotificationCompat.Builder for provided context or null.
48+
*/
49+
// NotificationCompat.Builder(Context context) constructor was deprecated in API level 26.
50+
@SuppressWarnings("deprecation")
51+
static NotificationCompat.Builder getDefaultNotificationBuilder(Context context,
52+
boolean isNotificationChannelSupported) {
53+
if (!isNotificationChannelSupported) {
54+
return new NotificationCompat.Builder(context);
55+
}
56+
String channelId = LeanplumNotificationChannel.getDefaultNotificationChannelId(context);
57+
if (!TextUtils.isEmpty(channelId)) {
58+
return new NotificationCompat.Builder(context, channelId);
59+
} else {
60+
Log.w("Failed to post notification, there are no notification channels configured.");
61+
return null;
62+
}
63+
}
64+
65+
/**
66+
* If notification channels are supported this method will try to create a channel with
67+
* information from the message if it doesn't exist and return NotificationCompat.Builder for this
68+
* channel. In the case where no channel information inside the message, we will try to get a
69+
* channel with default channel id. If notification channels not supported this method will return
70+
* NotificationCompat.Builder for context.
71+
*
72+
* @param context The application context.
73+
* @param message Push notification Bundle.
74+
* @return NotificationCompat.Builder or null.
75+
*/
76+
// NotificationCompat.Builder(Context context) constructor was deprecated in API level 26.
77+
@SuppressWarnings("deprecation")
78+
static NotificationCompat.Builder getNotificationBuilder(Context context, Bundle message) {
79+
NotificationCompat.Builder builder = null;
80+
// If we are targeting API 26, try to find supplied channel to post notification.
81+
if (LeanplumNotificationChannel.isNotificationChannelSupported(context)) {
82+
try {
83+
String channel = message.getString("lp_channel");
84+
if (!TextUtils.isEmpty(channel)) {
85+
// Create channel if it doesn't exist and post notification to that channel.
86+
Map<String, Object> channelDetails = JsonConverter.fromJson(channel);
87+
String channelId = LeanplumNotificationChannel.createNotificationChannel(context,
88+
channelDetails);
89+
if (!TextUtils.isEmpty(channelId)) {
90+
builder = new NotificationCompat.Builder(context, channelId);
91+
} else {
92+
Log.w("Failed to post notification to specified channel.");
93+
}
94+
} else {
95+
// If channel isn't supplied, try to look up for default channel.
96+
builder = LeanplumNotificationHelper.getDefaultNotificationBuilder(context, true);
97+
}
98+
} catch (Exception e) {
99+
Log.e("Failed to post notification to specified channel.");
100+
}
101+
} else {
102+
builder = new NotificationCompat.Builder(context);
103+
}
104+
return builder;
105+
}
106+
}

AndroidSDK/src/com/leanplum/LeanplumPushService.java

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -318,38 +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-
if (!TextUtils.isEmpty(channelId)) {
332-
builder = new NotificationCompat.Builder(context, channelId);
333-
} else {
334-
Log.w("Failed to post notification to specified channel.");
335-
return;
336-
}
337-
} else {
338-
// If channel isn't supplied, try to look up for default channel.
339-
String channelId = LeanplumNotificationChannel.getDefaultNotificationChannelId(context);
340-
if (!TextUtils.isEmpty(channelId)) {
341-
builder = new NotificationCompat.Builder(context, channelId);
342-
} else {
343-
Log.w("Failed to post notification, there are no notification channels configured.");
344-
return;
345-
}
346-
}
347-
} catch (Exception e) {
348-
Log.e("Failed to post notification to specified channel.");
349-
}
350-
} else {
351-
builder = new NotificationCompat.Builder(context);
352-
}
321+
NotificationCompat.Builder builder =
322+
LeanplumNotificationHelper.getNotificationBuilder(context, message);
353323

354324
if (builder == null) {
355325
return;
@@ -376,8 +346,10 @@ private static void showNotification(Context context, Bundle message) {
376346
}
377347
}
378348

379-
// Try to put notification on top of notification area.
380-
if (Build.VERSION.SDK_INT >= 16) {
349+
// Try to put a notification on top of the notification area. This method was deprecated in API
350+
// level 26. For API level 26 and above we must use setImportance(int) for each notification
351+
// channel, not for each notification message.
352+
if (Build.VERSION.SDK_INT >= 16 && Build.VERSION.SDK_INT < 26) {
381353
builder.setPriority(Notification.PRIORITY_MAX);
382354
}
383355
builder.setAutoCancel(true);

0 commit comments

Comments
 (0)