1
+ /*
2
+ * Copyright (C) 2015 The Android Open Source Project
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ package com .example .android .sunshine .app .gcm ;
18
+
19
+ import android .app .NotificationManager ;
20
+ import android .app .PendingIntent ;
21
+ import android .content .Context ;
22
+ import android .content .Intent ;
23
+ import android .graphics .Bitmap ;
24
+ import android .graphics .BitmapFactory ;
25
+ import android .os .Bundle ;
26
+ import android .support .v4 .app .NotificationCompat ;
27
+ import android .util .Log ;
28
+ import android .widget .Toast ;
29
+
30
+ import com .example .android .sunshine .app .MainActivity ;
31
+ import com .example .android .sunshine .app .R ;
32
+ import com .google .android .gms .gcm .GcmListenerService ;
33
+
34
+ import org .json .JSONException ;
35
+ import org .json .JSONObject ;
36
+
37
+ public class MyGcmListenerService extends GcmListenerService {
38
+
39
+ private static final String TAG = "MyGcmListenerService" ;
40
+
41
+ private static final String EXTRA_DATA = "data" ;
42
+ private static final String EXTRA_WEATHER = "weather" ;
43
+ private static final String EXTRA_LOCATION = "location" ;
44
+
45
+ public static final int NOTIFICATION_ID = 1 ;
46
+
47
+ /**
48
+ * Called when message is received.
49
+ *
50
+ * @param from SenderID of the sender.
51
+ * @param data Data bundle containing message data as key/value pairs.
52
+ * For Set of keys use data.keySet().
53
+ */
54
+ @ Override
55
+ public void onMessageReceived (String from , Bundle data ) {
56
+ // Time to unparcel the bundle!
57
+ if (!data .isEmpty ()) {
58
+ // TODO: gcm_default sender ID comes from the API console
59
+ String senderId = getString (R .string .gcm_defaultSenderId );
60
+ if (senderId .length () == 0 ) {
61
+ Toast .makeText (this , "SenderID string needs to be set" , Toast .LENGTH_LONG ).show ();
62
+ }
63
+ // Not a bad idea to check that the message is coming from your server.
64
+ if ((senderId ).equals (from )) {
65
+ // Process message and then post a notification of the received message.
66
+ try {
67
+ JSONObject jsonObject = new JSONObject (data .getString (EXTRA_DATA ));
68
+ String weather = jsonObject .getString (EXTRA_WEATHER );
69
+ String location = jsonObject .getString (EXTRA_LOCATION );
70
+ String alert =
71
+ String .format (getString (R .string .gcm_weather_alert ), weather , location );
72
+ sendNotification (alert );
73
+ } catch (JSONException e ) {
74
+ // JSON parsing failed, so we just let this message go, since GCM is not one
75
+ // of our critical features.
76
+ }
77
+ }
78
+ Log .i (TAG , "Received: " + data .toString ());
79
+ }
80
+ }
81
+
82
+ /**
83
+ * Put the message into a notification and post it.
84
+ * This is just one simple example of what you might choose to do with a GCM message.
85
+ *
86
+ * @param message The alert message to be posted.
87
+ */
88
+ private void sendNotification (String message ) {
89
+ NotificationManager mNotificationManager =
90
+ (NotificationManager ) getSystemService (Context .NOTIFICATION_SERVICE );
91
+ PendingIntent contentIntent =
92
+ PendingIntent .getActivity (this , 0 , new Intent (this , MainActivity .class ), 0 );
93
+
94
+ // Notifications using both a large and a small icon (which yours should!) need the large
95
+ // icon as a bitmap. So we need to create that here from the resource ID, and pass the
96
+ // object along in our notification builder. Generally, you want to use the app icon as the
97
+ // small icon, so that users understand what app is triggering this notification.
98
+ Bitmap largeIcon = BitmapFactory .decodeResource (this .getResources (), R .drawable .art_storm );
99
+ NotificationCompat .Builder mBuilder =
100
+ new NotificationCompat .Builder (this )
101
+ .setSmallIcon (R .drawable .art_clear )
102
+ .setLargeIcon (largeIcon )
103
+ .setContentTitle ("Weather Alert!" )
104
+ .setStyle (new NotificationCompat .BigTextStyle ().bigText (message ))
105
+ .setContentText (message )
106
+ .setPriority (NotificationCompat .PRIORITY_HIGH );
107
+ mBuilder .setContentIntent (contentIntent );
108
+ mNotificationManager .notify (NOTIFICATION_ID , mBuilder .build ());
109
+ }
110
+ }
0 commit comments