11package com .iterable .iterableapi ;
22
33import android .app .Notification ;
4+ import android .app .NotificationChannel ;
45import android .app .NotificationManager ;
56import android .app .PendingIntent ;
67import android .content .Context ;
2021import org .json .JSONObject ;
2122
2223/**
23- *
2424 * Created by David Truong [email protected] 2525 */
2626public class IterableNotification extends NotificationCompat .Builder {
2727 static final String TAG = "IterableNotification" ;
2828 private boolean isGhostPush ;
29- private String imageUrl ;
29+ private String imageUrl ;
3030 int requestCode ;
3131 IterableNotificationData iterableNotificationData ;
3232
33- protected IterableNotification (Context context ) {
34- super (context );
33+ protected IterableNotification (Context context , String channelId ) {
34+ super (context , channelId );
3535 }
3636
3737 /**
@@ -67,21 +67,26 @@ public void run() {
6767
6868 /**
6969 * Creates and returns an instance of IterableNotification.
70+ *
7071 * @param context
7172 * @param extras
7273 * @param classToOpen
7374 * @return Returns null if the intent comes from an Iterable ghostPush
7475 */
7576 public static IterableNotification createNotification (Context context , Bundle extras , Class classToOpen ) {
7677 int stringId = context .getApplicationInfo ().labelRes ;
77- String applicationName = context .getString (stringId );
78+ String applicationName = context .getString (stringId );
7879 String notificationBody = null ;
7980 String soundName = null ;
8081 String messageId = null ;
8182 String pushImage = null ;
83+ //TODO: When backend supports channels, these strings needs to change (channelName, channelId, channelDescription).
84+ String channelName = "iterable channel" ;
85+ String channelId = context .getPackageName ();
86+ String channelDescription = "" ;
8287
83- IterableNotification notificationBuilder = new IterableNotification ( context );
84-
88+ registerChannelIfEmpty ( context , channelId , channelName , channelDescription );
89+ IterableNotification notificationBuilder = new IterableNotification ( context , context . getPackageName ());
8590 if (extras .containsKey (IterableConstants .ITERABLE_DATA_KEY )) {
8691 applicationName = extras .getString (IterableConstants .ITERABLE_DATA_TITLE , applicationName );
8792 notificationBody = extras .getString (IterableConstants .ITERABLE_DATA_BODY );
@@ -110,20 +115,20 @@ public static IterableNotification createNotification(Context context, Bundle ex
110115 notifPermissions .defaults |= Notification .DEFAULT_LIGHTS ;
111116
112117 notificationBuilder
113- .setSmallIcon (getIconId (context ))
114- .setTicker (applicationName ).setWhen (0 )
115- .setAutoCancel (true )
116- .setContentTitle (applicationName )
117- .setPriority (Notification .PRIORITY_HIGH )
118- .setContentText (notificationBody );
118+ .setSmallIcon (getIconId (context ))
119+ .setTicker (applicationName ).setWhen (0 )
120+ .setAutoCancel (true )
121+ .setContentTitle (applicationName )
122+ .setPriority (Notification .PRIORITY_HIGH )
123+ .setContentText (notificationBody );
119124
120125 if (pushImage != null ) {
121126 notificationBuilder .imageUrl = pushImage ;
122127 notificationBuilder .setContentText (notificationBody )
123- .setStyle (new NotificationCompat .BigPictureStyle ()
124- .setBigContentTitle (applicationName )
125- .setSummaryText (notificationBody )
126- );
128+ .setStyle (new NotificationCompat .BigPictureStyle ()
129+ .setBigContentTitle (applicationName )
130+ .setSummaryText (notificationBody )
131+ );
127132 } else {
128133 notificationBuilder .setStyle (new NotificationCompat .BigTextStyle ().bigText (notificationBody ));
129134 }
@@ -133,7 +138,7 @@ public static IterableNotification createNotification(Context context, Bundle ex
133138 String [] soundFile = soundName .split ("\\ ." );
134139 soundName = soundFile [0 ];
135140
136- if (!soundName .equalsIgnoreCase (IterableConstants .DEFAULT_SOUND )){
141+ if (!soundName .equalsIgnoreCase (IterableConstants .DEFAULT_SOUND )) {
137142 int soundID = context .getResources ().getIdentifier (soundName , IterableConstants .SOUND_FOLDER_IDENTIFIER , context .getPackageName ());
138143 Uri soundUri = Uri .parse (IterableConstants .ANDROID_RESOURCE_PATH + context .getPackageName () + "/" + soundID );
139144 notificationBuilder .setSound (soundUri );
@@ -175,6 +180,7 @@ public static IterableNotification createNotification(Context context, Bundle ex
175180 /**
176181 * Posts the notification on device.
177182 * Only sets the notification if it is not a ghostPush/null iterableNotification.
183+ *
178184 * @param context
179185 * @param iterableNotification Function assumes that the iterableNotification is a ghostPush
180186 * if the IterableNotification passed in is null.
@@ -187,8 +193,40 @@ public static void postNotificationOnDevice(Context context, IterableNotificatio
187193 }
188194 }
189195
196+ /**
197+ * Creates the notification channel on device.
198+ * Only creates the notification channel if application does not have notification channel created.
199+ *
200+ * @param context
201+ * @param channelId Determines the channel Id. This distinguishes if the app has different channel or not.
202+ * @param channelName Sets the channel name that is shown to the user.
203+ * @param channelDescription Sets the channel description that is shown to the user.
204+ */
205+ private static void registerChannelIfEmpty (Context context , String channelId , String channelName , String channelDescription ) {
206+ NotificationManager mNotificationManager = (NotificationManager )
207+ context .getApplicationContext ().getSystemService (Context .NOTIFICATION_SERVICE );
208+ if (android .os .Build .VERSION .SDK_INT >= android .os .Build .VERSION_CODES .O
209+ && mNotificationManager != null
210+ && mNotificationManager .getNotificationChannel (channelId ) == null ) {
211+ IterableLogger .d (TAG , "Creating notification: channelId = " + channelId + " channelName = "
212+ + channelName + " channelDescription = " + channelDescription );
213+ mNotificationManager .createNotificationChannel (createNotificationChannel (channelId , channelName , channelDescription ));
214+ }
215+ }
216+
217+ private static NotificationChannel createNotificationChannel (String channelId , String channelName , String channelDescription ) {
218+ NotificationChannel notificationChannel = null ;
219+ if (android .os .Build .VERSION .SDK_INT >= android .os .Build .VERSION_CODES .O ) {
220+ notificationChannel = new NotificationChannel (channelId , channelName , NotificationManager .IMPORTANCE_HIGH );
221+ notificationChannel .setDescription (channelDescription );
222+ notificationChannel .enableLights (true );
223+ }
224+ return notificationChannel ;
225+ }
226+
190227 /**
191228 * Returns the iconId from potential resource locations
229+ *
192230 * @param context
193231 * @return
194232 */
@@ -200,7 +238,7 @@ private static int getIconId(Context context) {
200238 try {
201239 ApplicationInfo info = context .getPackageManager ().getApplicationInfo (context .getPackageName (), PackageManager .GET_META_DATA );
202240 iconId = info .metaData .getInt (IterableConstants .NOTIFICATION_ICON_NAME , 0 );
203- IterableLogger .d (TAG , "iconID: " + info .metaData .get (IterableConstants .NOTIFICATION_ICON_NAME ));
241+ IterableLogger .d (TAG , "iconID: " + info .metaData .get (IterableConstants .NOTIFICATION_ICON_NAME ));
204242 } catch (PackageManager .NameNotFoundException e ) {
205243 e .printStackTrace ();
206244 }
@@ -209,18 +247,17 @@ private static int getIconId(Context context) {
209247 //Get the iconId set in code
210248 if (iconId == 0 ) {
211249 iconId = context .getResources ().getIdentifier (
212- IterableApi .getNotificationIcon (context ),
213- IterableConstants .ICON_FOLDER_IDENTIFIER ,
214- context .getPackageName ());
250+ IterableApi .getNotificationIcon (context ),
251+ IterableConstants .ICON_FOLDER_IDENTIFIER ,
252+ context .getPackageName ());
215253 }
216254
217255 //Get id from the default app settings
218256 if (iconId == 0 ) {
219257 if (context .getApplicationInfo ().icon != 0 ) {
220258 IterableLogger .d (TAG , "No Notification Icon defined - defaulting to app icon" );
221259 iconId = context .getApplicationInfo ().icon ;
222- }
223- else {
260+ } else {
224261 IterableLogger .w (TAG , "No Notification Icon defined - push notifications will not be displayed" );
225262 }
226263 }
@@ -230,6 +267,7 @@ private static int getIconId(Context context) {
230267
231268 /**
232269 * Returns if the given notification is a ghost/silent push notification
270+ *
233271 * @param extras
234272 * @return
235273 */
0 commit comments