-
-
Notifications
You must be signed in to change notification settings - Fork 736
Add Notification Channels configuration for Android O #713
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
de45313
9b3a7cf
876d5d9
20aec3f
ca9b340
8f5a349
b33d776
e151f30
49e3003
d62981c
4aa9252
e808ab9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,8 +8,11 @@ | |
*/ | ||
package com.parse; | ||
|
||
import android.annotation.TargetApi; | ||
import android.app.Activity; | ||
import android.app.Notification; | ||
import android.app.NotificationChannel; | ||
import android.app.NotificationManager; | ||
import android.app.PendingIntent; | ||
import android.content.BroadcastReceiver; | ||
import android.content.Context; | ||
|
@@ -260,6 +263,27 @@ protected Class<? extends Activity> getActivity(Context context, Intent intent) | |
return cls; | ||
} | ||
|
||
/** | ||
* Retrieves the channel to be used in a {@link Notification} if API >= 26, if not null. The default returns a new channel | ||
* with id "parse_push", name "Push notifications" and default importance. | ||
* | ||
* @param context | ||
* The {@code Context} in which the receiver is running. | ||
* @param intent | ||
* An {@code Intent} containing the channel and data of the current push notification. | ||
* @return | ||
* The notification channel | ||
*/ | ||
@TargetApi(Build.VERSION_CODES.O) | ||
protected NotificationChannel getNotificationChannel(Context context, Intent intent) { | ||
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); | ||
NotificationChannel notificationChannel = new NotificationChannel("parse_push", "Push notifications", NotificationManager.IMPORTANCE_DEFAULT); | ||
|
||
// Android doesn't create a new channel if the properties of the channel hasn't changed | ||
nm.createNotificationChannel(notificationChannel); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you move the channel creation outside, maybe in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
return notificationChannel; | ||
} | ||
|
||
/** | ||
* Retrieves the small icon to be used in a {@link Notification}. The default implementation uses | ||
* the icon specified by {@code com.parse.push.notification_icon} {@code meta-data} in your | ||
|
@@ -364,6 +388,8 @@ protected Notification getNotification(Context context, Intent intent) { | |
PendingIntent pDeleteIntent = PendingIntent.getBroadcast(context, deleteIntentRequestCode, | ||
deleteIntent, PendingIntent.FLAG_UPDATE_CURRENT); | ||
|
||
|
||
|
||
// The purpose of setDefaults(Notification.DEFAULT_ALL) is to inherit notification properties | ||
// from system defaults | ||
NotificationCompat.Builder parseBuilder = new NotificationCompat.Builder(context); | ||
|
@@ -376,6 +402,12 @@ protected Notification getNotification(Context context, Intent intent) { | |
.setDeleteIntent(pDeleteIntent) | ||
.setAutoCancel(true) | ||
.setDefaults(Notification.DEFAULT_ALL); | ||
|
||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
NotificationChannel notificationChannel = getNotificationChannel(context, intent); | ||
parseBuilder.setNotificationChannel(notificationChannel); | ||
} | ||
|
||
if (alert != null | ||
&& alert.length() > ParsePushBroadcastReceiver.SMALL_NOTIFICATION_MAX_CHARACTER_LIMIT) { | ||
parseBuilder.setStyle(new NotificationCompat.Builder.BigTextStyle().bigText(alert)); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ buildscript { | |
jcenter() | ||
} | ||
dependencies { | ||
classpath 'com.android.tools.build:gradle:2.3.2' | ||
classpath 'com.android.tools.build:gradle:2.3.3' | ||
} | ||
} | ||
|
||
|
@@ -22,11 +22,11 @@ allprojects { | |
} | ||
|
||
ext { | ||
compileSdkVersion = 25 | ||
compileSdkVersion = 26 | ||
buildToolsVersion = "25.0.3" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be changed? Also in travis. I'm not sure, just asking. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As before, shouldn't we build with something starting with 26? Here and in travis file... No? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't want to change the build tools since I don't know what the routines are here on that, but I can change to 26.0.1 I could also upgrade the support library to 26.0.1, but then I'd have to raise the minSDK to 14, since they removed support for <14 |
||
|
||
supportLibVersion = '25.3.1' | ||
|
||
minSdkVersion = 9 | ||
targetSdkVersion = 25 | ||
targetSdkVersion = 26 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry man, I just noticed this... I am not sure if this might throw at runtime on older APIs, even if not used. Could we just store
String mNotificationChannelId
here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right again, this will crash. Sending just the Id is better.