Skip to content

Commit 4803953

Browse files
iernienatario1
authored andcommitted
Add support for Android O (#713)
* Add Notification Channels configuration for Android O * Revert build tools version * Update travis.yml to android-26 * Switch to making a notification channel instead based on feedback * Add comment on recreating notification channels * Only call getNotificationChannel on SDK >= 26 * Extract createNotificationChannel to own method and call it from getNotification * Update to build-tools 26.0.1 * Save channelId instead of whole channel in NotificationCompat * Update robolectric to support API 26 * Downgrade to robolectric 3.3.2
1 parent e637667 commit 4803953

File tree

7 files changed

+67
-13
lines changed

7 files changed

+67
-13
lines changed

.travis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ android:
1313
components:
1414
- tools
1515
- platform-tools
16-
- build-tools-25.0.3
17-
- android-25
18-
- doc-25
16+
- build-tools-26.0.1
17+
- android-26
18+
- doc-26
1919

2020
before_install:
2121
- pip install --user codecov

Parse/build.gradle

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,7 @@ dependencies {
5959
compile 'com.parse.bolts:bolts-tasks:1.4.0'
6060
compile "com.squareup.okhttp3:okhttp:$okhttpVersion"
6161

62-
//Be aware, tests fail on 3.3.2 Wait to update until
63-
//java.lang.NoClassDefFoundError: android/content/pm/VersionedPackage
64-
//issue is fixed in Robolectric
65-
//https://github.com/robolectric/robolectric/issues/2562
66-
testCompile 'org.robolectric:robolectric:3.3'
62+
testCompile 'org.robolectric:robolectric:3.3.2'
6763
testCompile 'org.skyscreamer:jsonassert:1.5.0'
6864
testCompile 'org.mockito:mockito-core:1.10.19'
6965
testCompile "com.squareup.okhttp3:mockwebserver:$okhttpVersion"

Parse/src/main/java/com/parse/NotificationCompat.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import android.annotation.TargetApi;
2020
import android.app.Notification;
21+
import android.app.NotificationChannel;
2122
import android.app.NotificationManager;
2223
import android.app.PendingIntent;
2324
import android.content.Context;
@@ -95,6 +96,9 @@ public Notification build(Builder b) {
9596
}
9697
}
9798
}
99+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
100+
postJellyBeanBuilder.setChannelId(b.mNotificationChannelId);
101+
}
98102
return postJellyBeanBuilder.build();
99103
}
100104
}
@@ -124,6 +128,7 @@ public static class Builder {
124128
Bitmap mLargeIcon;
125129
int mPriority;
126130
Style mStyle;
131+
String mNotificationChannelId;
127132

128133
Notification mNotification = new Notification();
129134

@@ -188,6 +193,14 @@ public Builder setContentTitle(CharSequence title) {
188193
return this;
189194
}
190195

196+
/**
197+
* Set the notification channel of the notification, in a standard notification.
198+
*/
199+
public Builder setNotificationChannel(String notificationChannelId) {
200+
mNotificationChannelId = notificationChannelId;
201+
return this;
202+
}
203+
191204
/**
192205
* Set the text (second row) of the notification, in a standard notification.
193206
*/

Parse/src/main/java/com/parse/ParseNotificationManager.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import android.app.Activity;
1414
import android.app.Notification;
15+
import android.app.NotificationChannel;
1516
import android.app.NotificationManager;
1617
import android.app.PendingIntent;
1718
import android.content.ComponentName;
@@ -20,6 +21,7 @@
2021
import android.content.res.Resources;
2122
import android.content.res.Resources.NotFoundException;
2223
import android.graphics.drawable.Drawable;
24+
import android.os.Build;
2325
import android.os.Bundle;
2426
import android.util.SparseIntArray;
2527

Parse/src/main/java/com/parse/ParsePushBroadcastReceiver.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88
*/
99
package com.parse;
1010

11+
import android.annotation.TargetApi;
1112
import android.app.Activity;
1213
import android.app.Notification;
14+
import android.app.NotificationChannel;
15+
import android.app.NotificationManager;
1316
import android.app.PendingIntent;
1417
import android.content.BroadcastReceiver;
1518
import android.content.Context;
@@ -260,6 +263,37 @@ protected Class<? extends Activity> getActivity(Context context, Intent intent)
260263
return cls;
261264
}
262265

266+
/**
267+
* Retrieves the channel to be used in a {@link Notification} if API >= 26, if not null. The default returns a new channel
268+
* with id "parse_push", name "Push notifications" and default importance.
269+
*
270+
* @param context
271+
* The {@code Context} in which the receiver is running.
272+
* @param intent
273+
* An {@code Intent} containing the channel and data of the current push notification.
274+
* @return
275+
* The notification channel
276+
*/
277+
@TargetApi(Build.VERSION_CODES.O)
278+
protected NotificationChannel getNotificationChannel(Context context, Intent intent) {
279+
return new NotificationChannel("parse_push", "Push notifications", NotificationManager.IMPORTANCE_DEFAULT);
280+
}
281+
282+
/**
283+
* Creates the notification channel with the NotificationManager. Channel is not recreated
284+
* if the channel properties are unchanged.
285+
*
286+
* @param context
287+
* The {@code Context} in which the receiver is running.
288+
* @param notificationChannel
289+
* The {@code NotificationChannel} to be created.
290+
*/
291+
@TargetApi(Build.VERSION_CODES.O)
292+
protected void createNotificationChannel(Context context, NotificationChannel notificationChannel) {
293+
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
294+
nm.createNotificationChannel(notificationChannel);
295+
}
296+
263297
/**
264298
* Retrieves the small icon to be used in a {@link Notification}. The default implementation uses
265299
* the icon specified by {@code com.parse.push.notification_icon} {@code meta-data} in your
@@ -364,6 +398,8 @@ protected Notification getNotification(Context context, Intent intent) {
364398
PendingIntent pDeleteIntent = PendingIntent.getBroadcast(context, deleteIntentRequestCode,
365399
deleteIntent, PendingIntent.FLAG_UPDATE_CURRENT);
366400

401+
402+
367403
// The purpose of setDefaults(Notification.DEFAULT_ALL) is to inherit notification properties
368404
// from system defaults
369405
NotificationCompat.Builder parseBuilder = new NotificationCompat.Builder(context);
@@ -376,6 +412,13 @@ protected Notification getNotification(Context context, Intent intent) {
376412
.setDeleteIntent(pDeleteIntent)
377413
.setAutoCancel(true)
378414
.setDefaults(Notification.DEFAULT_ALL);
415+
416+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
417+
NotificationChannel notificationChannel = getNotificationChannel(context, intent);
418+
createNotificationChannel(context, notificationChannel);
419+
parseBuilder.setNotificationChannel(notificationChannel.getId());
420+
}
421+
379422
if (alert != null
380423
&& alert.length() > ParsePushBroadcastReceiver.SMALL_NOTIFICATION_MAX_CHARACTER_LIMIT) {
381424
parseBuilder.setStyle(new NotificationCompat.Builder.BigTextStyle().bigText(alert));

Parse/src/test/java/com/parse/ParseClientConfigurationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import static org.mockito.Mockito.when;
2828

2929
@RunWith(RobolectricTestRunner.class)
30-
@Config(constants = BuildConfig.class)
30+
@Config(constants = BuildConfig.class, sdk = TestHelper.ROBOLECTRIC_SDK_VERSION)
3131
public class ParseClientConfigurationTest {
3232

3333
private final String serverUrl = "http://example.com/parse";

build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ buildscript {
44
jcenter()
55
}
66
dependencies {
7-
classpath 'com.android.tools.build:gradle:2.3.2'
7+
classpath 'com.android.tools.build:gradle:2.3.3'
88
}
99
}
1010

@@ -22,11 +22,11 @@ allprojects {
2222
}
2323

2424
ext {
25-
compileSdkVersion = 25
26-
buildToolsVersion = "25.0.3"
25+
compileSdkVersion = 26
26+
buildToolsVersion = "26.0.1"
2727

2828
supportLibVersion = '25.3.1'
2929

3030
minSdkVersion = 9
31-
targetSdkVersion = 25
31+
targetSdkVersion = 26
3232
}

0 commit comments

Comments
 (0)