19
19
public class GCMIntentService extends GCMBaseIntentService {
20
20
21
21
private static final String TAG = "GCMIntentService" ;
22
-
22
+
23
23
public GCMIntentService () {
24
24
super ("GCMIntentService" );
25
25
}
@@ -64,19 +64,30 @@ protected void onMessage(Context context, Intent intent) {
64
64
if (extras != null )
65
65
{
66
66
// if we are in the foreground, just surface the payload, else post it to the statusbar
67
- if (PushPlugin .isInForeground ()) {
67
+ if (PushPlugin .isInForeground ()) {
68
68
extras .putBoolean ("foreground" , true );
69
- PushPlugin .sendExtras (extras );
69
+ PushPlugin .sendExtras (extras );
70
70
}
71
71
else {
72
72
extras .putBoolean ("foreground" , false );
73
-
74
- // Send a notification if there is a message
75
- if (extras .getString ("message" ) != null && extras .getString ("message" ).length () != 0 ) {
76
- createNotification (context , extras );
77
- }
78
- }
79
- }
73
+ String message = extras .getString ("message" );
74
+
75
+ if (message == null ) {
76
+ // Providers like Parse always send a 'data' as root object, so "Parse" that
77
+ try {
78
+ JSONObject object_example = new JSONObject (extras .getString ("data" ));
79
+ message = object_example .getString ("alert" );
80
+ extras .putString ("message" , message );
81
+ }
82
+ catch (JSONException e ) {}
83
+ }
84
+
85
+ // Send a notification if there is a message
86
+ if (message != null && message .length () != 0 ) {
87
+ createNotification (context , extras );
88
+ }
89
+ }
90
+ }
80
91
}
81
92
82
93
public void createNotification (Context context , Bundle extras )
@@ -89,15 +100,15 @@ public void createNotification(Context context, Bundle extras)
89
100
notificationIntent .putExtra ("pushBundle" , extras );
90
101
91
102
PendingIntent contentIntent = PendingIntent .getActivity (this , 0 , notificationIntent , PendingIntent .FLAG_UPDATE_CURRENT );
92
-
103
+
93
104
int defaults = Notification .DEFAULT_ALL ;
94
105
95
106
if (extras .getString ("defaults" ) != null ) {
96
107
try {
97
108
defaults = Integer .parseInt (extras .getString ("defaults" ));
98
109
} catch (NumberFormatException e ) {}
99
110
}
100
-
111
+
101
112
NotificationCompat .Builder mBuilder =
102
113
new NotificationCompat .Builder (context )
103
114
.setDefaults (defaults )
@@ -119,9 +130,9 @@ public void createNotification(Context context, Bundle extras)
119
130
if (msgcnt != null ) {
120
131
mBuilder .setNumber (Integer .parseInt (msgcnt ));
121
132
}
122
-
133
+
123
134
int notId = 0 ;
124
-
135
+
125
136
try {
126
137
notId = Integer .parseInt (extras .getString ("notId" ));
127
138
}
@@ -131,20 +142,20 @@ public void createNotification(Context context, Bundle extras)
131
142
catch (Exception e ) {
132
143
Log .e (TAG , "Number format exception - Error parsing Notification ID" + e .getMessage ());
133
144
}
134
-
145
+
135
146
mNotificationManager .notify ((String ) appName , notId , mBuilder .build ());
136
147
}
137
-
148
+
138
149
private static String getAppName (Context context )
139
150
{
140
- CharSequence appName =
151
+ CharSequence appName =
141
152
context
142
153
.getPackageManager ()
143
154
.getApplicationLabel (context .getApplicationInfo ());
144
-
155
+
145
156
return (String )appName ;
146
157
}
147
-
158
+
148
159
@ Override
149
160
public void onError (Context context , String errorId ) {
150
161
Log .e (TAG , "onError - errorId: " + errorId );
0 commit comments