1
1
package me .pushy .sdk ;
2
2
3
+ import android .os .Build ;
3
4
import android .app .Notification ;
4
5
import android .app .PendingIntent ;
5
6
import android .content .Intent ;
6
7
import android .content .Context ;
7
8
import android .media .RingtoneManager ;
8
9
import android .app .NotificationManager ;
9
10
import android .content .BroadcastReceiver ;
11
+ import android .util .Log ;
12
+
13
+ import java .lang .reflect .Constructor ;
14
+ import java .lang .reflect .Method ;
10
15
11
16
public class PushReceiver extends BroadcastReceiver {
12
17
@ Override
@@ -33,6 +38,11 @@ public void onReceive(Context context, Intent intent) {
33
38
// Get an instance of the NotificationManager service
34
39
NotificationManager notificationManager = (NotificationManager ) context .getSystemService (context .NOTIFICATION_SERVICE );
35
40
41
+ // Device is Android O or newer?
42
+ if (Build .VERSION .SDK_INT >= 26 ) {
43
+ configureNotificationChannel (builder , notificationManager );
44
+ }
45
+
36
46
// Build the notification and display it
37
47
notificationManager .notify (1 , builder .build ());
38
48
}
@@ -52,4 +62,42 @@ private PendingIntent getMainActivityPendingIntent(Context context) {
52
62
// Convert intent into pending intent
53
63
return PendingIntent .getActivity (context , 0 , launchIntent , PendingIntent .FLAG_UPDATE_CURRENT );
54
64
}
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
+ }
55
103
}
0 commit comments