Skip to content

Commit fbe3d43

Browse files
committed
"Merged from 6.11_So_Much_Real_Estate_Part_3_Start"
2 parents 6c74117 + 62a5c1b commit fbe3d43

File tree

3 files changed

+125
-4
lines changed

3 files changed

+125
-4
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
}

app/src/main/java/com/example/android/sunshine/app/gcm/RegistrationIntentService.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import android.content.SharedPreferences;
2121
import android.preference.PreferenceManager;
2222
import android.util.Log;
23+
import android.widget.Toast;
2324

2425
import com.example.android.sunshine.app.MainActivity;
2526
import com.example.android.sunshine.app.R;
@@ -45,9 +46,14 @@ protected void onHandleIntent(Intent intent) {
4546
// Initially this call goes out to the network to retrieve the token, subsequent calls
4647
// are local.
4748
InstanceID instanceID = InstanceID.getInstance(this);
48-
String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
49-
GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
50-
sendRegistrationToServer(token);
49+
50+
// TODO: gcm_default sender ID comes from the API console
51+
String senderId = getString(R.string.gcm_defaultSenderId);
52+
if ( senderId.length() != 0 ) {
53+
String token = instanceID.getToken(senderId,
54+
GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
55+
sendRegistrationToServer(token);
56+
}
5157

5258
// You should store a boolean that indicates whether the generated token has been
5359
// sent to your server. If the boolean is false, send the token to your server,

app/src/main/res/values/strings.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,4 +211,9 @@
211211
<string name="condition_962">Hurricane</string>
212212

213213
<string name="condition_unknown">Unknown (<xliff:g id="low">%1$s</xliff:g>)</string>
214-
</resources>
214+
215+
<!-- Used to form a severe weather alert that reads "Heads up: <weather> in <location>!" -->
216+
<string name="gcm_weather_alert">Heads up: %1$s in %2$s!</string>
217+
// TODO: Get the SenderID from the Developer Console
218+
<string name="gcm_defaultSenderId" translatable="false"></string>
219+
</resources>

0 commit comments

Comments
 (0)