Skip to content

Commit 5356ded

Browse files
committed
Support for Android O Notification Channels
1 parent e1ab9b0 commit 5356ded

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

Diff for: receiver/src/android/PushReceiver.java

+48
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
package me.pushy.sdk;
22

3+
import android.os.Build;
34
import android.app.Notification;
45
import android.app.PendingIntent;
56
import android.content.Intent;
67
import android.content.Context;
78
import android.media.RingtoneManager;
89
import android.app.NotificationManager;
910
import android.content.BroadcastReceiver;
11+
import android.util.Log;
12+
13+
import java.lang.reflect.Constructor;
14+
import java.lang.reflect.Method;
1015

1116
public class PushReceiver extends BroadcastReceiver {
1217
@Override
@@ -33,6 +38,11 @@ public void onReceive(Context context, Intent intent) {
3338
// Get an instance of the NotificationManager service
3439
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
3540

41+
// Device is Android O or newer?
42+
if (Build.VERSION.SDK_INT >= 26) {
43+
configureNotificationChannel(builder, notificationManager);
44+
}
45+
3646
// Build the notification and display it
3747
notificationManager.notify(1, builder.build());
3848
}
@@ -52,4 +62,42 @@ private PendingIntent getMainActivityPendingIntent(Context context) {
5262
// Convert intent into pending intent
5363
return PendingIntent.getActivity(context, 0, launchIntent, PendingIntent.FLAG_UPDATE_CURRENT);
5464
}
65+
66+
private void configureNotificationChannel(Notification.Builder builder, NotificationManager notificationManager) {
67+
// Channel details
68+
String channelId = "pushy";
69+
String channelName = "Push Notifications";
70+
71+
// Channel importance (4 means high importance)
72+
int channelImportance = 4;
73+
74+
try {
75+
// Get NotificationChannel class via reflection (only available on API level 26+)
76+
Class notificationChannelClass = Class.forName("android.app.NotificationChannel");
77+
78+
// Get NotificationChannel constructor
79+
Constructor<?> notificationChannelConstructor = notificationChannelClass.getDeclaredConstructor(String.class, CharSequence.class, int.class);
80+
81+
// Instantiate new notification channel
82+
Object notificationChannel = notificationChannelConstructor.newInstance(channelId, channelName, channelImportance);
83+
84+
// Get notification channel creation method via reflection
85+
Method createNotificationChannelMethod = notificationManager.getClass().getDeclaredMethod("createNotificationChannel", notificationChannelClass);
86+
87+
// Invoke method on NotificationManager, passing in the channel object
88+
createNotificationChannelMethod.invoke(notificationManager, notificationChannel);
89+
90+
// Get "setChannelId" method for Notification.Builder (only for AppCompat v26+)
91+
Method setChannelIdMethod = builder.getClass().getDeclaredMethod("setChannelId", String.class);
92+
93+
// Invoke method, passing in the channel ID
94+
setChannelIdMethod.invoke(builder, channelId);
95+
96+
// Log success to console
97+
Log.d("Pushy", "Notification channel set successfully");
98+
} catch (Exception exc) {
99+
// Log exception to console
100+
Log.e("Pushy", "Creating notification channel failed", exc);
101+
}
102+
}
55103
}

0 commit comments

Comments
 (0)