5
5
import android .app .PendingIntent ;
6
6
import android .content .Context ;
7
7
import android .content .Intent ;
8
- import android .graphics .BitmapFactory ;
8
+ import android .content .pm .ApplicationInfo ;
9
+ import android .content .pm .PackageManager ;
9
10
import android .os .Bundle ;
10
- //import android.support.v7.app.NotificationCompat;
11
11
import android .support .v4 .app .NotificationCompat ;
12
12
13
13
import java .util .Date ;
17
17
* Created by David Truong [email protected]
18
18
*/
19
19
public class IterableNotification extends NotificationCompat .Builder {
20
+ static final String TAG = "IterableNotification" ;
20
21
private boolean isGhostPush ;
21
22
22
23
protected IterableNotification (Context context ) {
@@ -28,10 +29,9 @@ protected IterableNotification(Context context) {
28
29
* @param context
29
30
* @param extras
30
31
* @param classToOpen
31
- * @param icon
32
32
* @return Returns null if the intent comes from an Iterable ghostPush
33
33
*/
34
- public static IterableNotification createNotification (Context context , Bundle extras , Class classToOpen , int icon ) {
34
+ public static IterableNotification createNotification (Context context , Bundle extras , Class classToOpen ) {
35
35
int stringId = context .getApplicationInfo ().labelRes ;
36
36
String applicationName = context .getString (stringId );
37
37
String notificationBody = null ;
@@ -48,51 +48,32 @@ public static IterableNotification createNotification(Context context, Bundle ex
48
48
PendingIntent notificationClickedIntent = PendingIntent .getActivity (context , 0 ,
49
49
mainIntentWithExtras , PendingIntent .FLAG_UPDATE_CURRENT );
50
50
51
- NotificationCompat . Builder mBuilder = new NotificationCompat . Builder (
51
+ IterableNotification mBuilder = new IterableNotification (
52
52
context );
53
- Notification notification = mBuilder .setSmallIcon (icon ).setTicker (applicationName ).setWhen (0 )
53
+ mBuilder
54
+ .setDefaults (Notification .DEFAULT_SOUND )
55
+ .setSmallIcon (getIconId (context ))
54
56
.setAutoCancel (true )
55
57
.setContentTitle (applicationName )
56
58
.setStyle (new NotificationCompat .BigTextStyle ().bigText (notificationBody ))
57
- .setLargeIcon (BitmapFactory .decodeResource (context .getResources (), icon ))
58
- .setContentText (notificationBody ).build ();
59
-
60
- NotificationManager notificationManager = (NotificationManager ) context .getSystemService (Context .NOTIFICATION_SERVICE );
61
- notificationManager .notify (99999 , notification );
62
-
63
- // NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
64
- // context);
65
- // IterableNotification notification = mBuilder.setSmallIcon(icon).setTicker(applicationName).setWhen(0)
66
- // .setAutoCancel(true)
67
- // .setContentTitle(applicationName)
68
- // .setStyle(new NotificationCompat.BigTextStyle().bigText(notificationBody))
69
- // .setSmallIcon(icon)
70
- // .setContentText(notificationBody).build();
71
-
72
-
73
- IterableNotification notificationBuilder = new IterableNotification (context );
74
- notificationBuilder
75
- // .setSmallIcon(icon)
76
- // .setContentTitle(applicationName)
77
- // .setContentText(notificationBody)
78
- // .setStyle(new NotificationCompat.BigTextStyle()
79
- // .bigText(notificationBody))
80
- // .setAutoCancel(true);
81
-
82
- .setTicker (applicationName ).setWhen (0 )
83
- .setAutoCancel (true )
84
- .setContentTitle (applicationName )
85
- .setStyle (new NotificationCompat .BigTextStyle ().bigText (notificationBody ))
86
- // .setContentIntent(resultPendingIntent)
87
- // .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
88
- .setSmallIcon (icon )
59
+ .setPriority (Notification .PRIORITY_HIGH )
89
60
.setContentText (notificationBody );
90
61
91
- notificationBuilder .setContentIntent (notificationClickedIntent );
62
+ try {
63
+ ApplicationInfo info = context .getPackageManager ().getApplicationInfo (context .getPackageName (), PackageManager .GET_META_DATA );
64
+ mBuilder .setColor (info .metaData .getInt (IterableConstants .NOTIFICATION_COLOR ));
65
+ } catch (PackageManager .NameNotFoundException e ) {
66
+ e .printStackTrace ();
67
+ }
92
68
93
- notificationBuilder .isGhostPush = IterableHelper .isGhostPush (extras );
69
+ PackageManager pm = context .getPackageManager ();
70
+ if (pm .checkPermission ("android.permission.VIBRATE" , context .getPackageName ()) == PackageManager .PERMISSION_GRANTED ) {
71
+ mBuilder .setDefaults (Notification .DEFAULT_ALL );
72
+ } else {
73
+ mBuilder .setVibrate (null );
74
+ }
94
75
95
- return notificationBuilder ;
76
+ return mBuilder ;
96
77
}
97
78
98
79
/**
@@ -110,7 +91,47 @@ public static void postNotificationOnDevice(Context context, IterableNotificatio
110
91
long dateInMilli = new Date ().getTime ();
111
92
int notifID = (int ) (dateInMilli % Integer .MAX_VALUE );
112
93
113
- // mNotificationManager.notify(notifID, iterableNotification.build());
94
+ mNotificationManager .notify (notifID , iterableNotification .build ());
95
+ }
96
+ }
97
+
98
+ /**
99
+ * Returns the iconId from potential resource locations
100
+ * @param context
101
+ * @return
102
+ */
103
+ private static int getIconId (Context context ) {
104
+ int iconId = 0 ;
105
+
106
+ //Get the iconId set in the AndroidManifest.xml
107
+ if (iconId == 0 ) {
108
+ try {
109
+ ApplicationInfo info = context .getPackageManager ().getApplicationInfo (context .getPackageName (), PackageManager .GET_META_DATA );
110
+ iconId = info .metaData .getInt (IterableConstants .NOTIFICATION_ICON_NAME , 0 );
111
+ } catch (PackageManager .NameNotFoundException e ) {
112
+ e .printStackTrace ();
113
+ }
114
+ }
115
+
116
+ //Get the iconId set in code
117
+ if (iconId == 0 ) {
118
+ iconId = context .getResources ().getIdentifier (
119
+ IterableApi .getNotificationIcon (context ),
120
+ IterableConstants .ICON_FOLDER_IDENTIFIER ,
121
+ context .getPackageName ());
122
+ }
123
+
124
+ //Get id from the default app settings
125
+ if (iconId == 0 ) {
126
+ if (context .getApplicationInfo ().icon != 0 ) {
127
+ IterableLogger .d (TAG , "No Notification Icon defined - defaulting to app icon" );
128
+ iconId = context .getApplicationInfo ().icon ;
129
+ }
130
+ else {
131
+ IterableLogger .w (TAG , "No Notification Icon defined - push notifications will not be displayed" );
132
+ }
114
133
}
134
+
135
+ return iconId ;
115
136
}
116
137
}
0 commit comments