From 3a0b035c69acb09849cccf28922f2ce9936d5448 Mon Sep 17 00:00:00 2001 From: Dan Galpin Date: Sun, 24 May 2015 23:52:11 -0700 Subject: [PATCH] Better message parsing and notification building --- app/src/main/AndroidManifest.xml | 49 +++++----- .../sunshine/app/GcmBroadcastReceiver.java | 93 +++++++++++++++++++ .../android/sunshine/app/MainActivity.java | 15 ++- 3 files changed, 124 insertions(+), 33 deletions(-) create mode 100644 app/src/main/java/com/example/android/sunshine/app/GcmBroadcastReceiver.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 0ca2809..f492e8c 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -21,20 +21,17 @@ - - - + + + - + - @@ -46,8 +43,8 @@ android:supportsRtl="true"> + android:label="@string/app_name" + android:theme="@style/ForecastTheme" > @@ -66,22 +63,24 @@ android:name=".SettingsActivity" android:label="@string/title_activity_settings" android:parentActivityName=".MainActivity" - android:theme="@style/SettingsTheme"> + android:theme="@style/SettingsTheme" > + - + + @@ -90,25 +89,25 @@ + android:exported="true" > + - - - - - - - - - + + + + + + + diff --git a/app/src/main/java/com/example/android/sunshine/app/GcmBroadcastReceiver.java b/app/src/main/java/com/example/android/sunshine/app/GcmBroadcastReceiver.java new file mode 100644 index 0000000..2b74880 --- /dev/null +++ b/app/src/main/java/com/example/android/sunshine/app/GcmBroadcastReceiver.java @@ -0,0 +1,93 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.example.android.sunshine.app; + +import android.app.NotificationManager; +import android.app.PendingIntent; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.os.Bundle; +import android.support.v4.app.NotificationCompat; +import android.util.Log; + +import com.google.android.gms.gcm.GoogleCloudMessaging; + +public class GcmBroadcastReceiver extends BroadcastReceiver { + private final String LOG_TAG = BroadcastReceiver.class.getSimpleName(); + + private static final String EXTRA_SENDER = "from"; + private static final String EXTRA_WEATHER = "weather"; + private static final String EXTRA_LOCATION = "location"; + + public static final int NOTIFICATION_ID = 1; + private NotificationManager mNotificationManager; + + public GcmBroadcastReceiver() { + super(); + } + + @Override + public void onReceive(Context context, Intent intent) { + Bundle extras = intent.getExtras(); + GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context); + String messageType = gcm.getMessageType(intent); + + if (!extras.isEmpty()) { // has effect of unparcelling Bundle + /* + * Filter messages based on message type. Since it is likely that GCM + * will be extended in the future with new message types, just ignore + * any message types you're not interested in, or that you don't + * recognize. + */ + if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) { + // Is this our message?? Better be if you're going to act on it! + if (MainActivity.PROJECT_NUMBER.equals(extras.getString(EXTRA_SENDER))) { + // Process message and then post a notification of the received message. + String weather = extras.getString(EXTRA_WEATHER); + String location = extras.getString(EXTRA_LOCATION); + String alert = "Heads up: " + weather + " in " + location + "!"; + + sendNotification(context, alert); + } + + Log.i(LOG_TAG, "Received: " + extras.toString()); + } + } + } + + // Put the message into a notification and post it. + // This is just one simple example of what you might choose to do with a GCM message. + private void sendNotification(Context context, String msg) { + mNotificationManager = (NotificationManager) + context.getSystemService(Context.NOTIFICATION_SERVICE); + + PendingIntent contentIntent = + PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0); + + NotificationCompat.Builder mBuilder = + new NotificationCompat.Builder(context) + .setSmallIcon(R.drawable.art_storm) + .setContentTitle("Weather Alert!") + .setStyle(new NotificationCompat.BigTextStyle() + .bigText(msg)) + .setContentText(msg) + .setPriority(NotificationCompat.PRIORITY_HIGH); + + mBuilder.setContentIntent(contentIntent); + mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); + } +} diff --git a/app/src/main/java/com/example/android/sunshine/app/MainActivity.java b/app/src/main/java/com/example/android/sunshine/app/MainActivity.java index 07068f5..ba22ac3 100644 --- a/app/src/main/java/com/example/android/sunshine/app/MainActivity.java +++ b/app/src/main/java/com/example/android/sunshine/app/MainActivity.java @@ -35,7 +35,6 @@ import com.google.android.gms.common.GooglePlayServicesUtil; import com.google.android.gms.gcm.GoogleCloudMessaging; import java.io.IOException; -import java.util.concurrent.atomic.AtomicInteger; public class MainActivity extends ActionBarActivity implements ForecastFragment.Callback { @@ -46,10 +45,10 @@ public class MainActivity extends ActionBarActivity implements ForecastFragment. private static final String PROPERTY_APP_VERSION = "appVersion"; /** - * Substitute you own sender ID here. This is the project number you got - * from the API Console. + * Substitute you own project number here. This project number comes + * from the Google Developers Console. */ - String SENDER_ID = "Your-Sender-ID"; + static final String PROJECT_NUMBER = "Your Project Number"; private boolean mTwoPane; private String mLocation; @@ -91,10 +90,10 @@ public class MainActivity extends ActionBarActivity implements ForecastFragment. mGcm = GoogleCloudMessaging.getInstance(this); String regId = getRegistrationId(this); - if (SENDER_ID.equals("Your-Sender-ID")) { + if (PROJECT_NUMBER.equals("Your Project Number")) { new AlertDialog.Builder(this) - .setTitle("Needs Sender ID") - .setMessage("GCM will not function in Sunshine until you replace your Sender ID with a Sender ID from the Google Developers Console.") + .setTitle("Needs Project Number") + .setMessage("GCM will not function in Sunshine until you set the Project Number to the one from the Google Developers Console.") .setPositiveButton(android.R.string.ok, null) .create().show(); } else if (regId.isEmpty()) { @@ -263,7 +262,7 @@ public class MainActivity extends ActionBarActivity implements ForecastFragment. if (mGcm == null) { mGcm = GoogleCloudMessaging.getInstance(context); } - String regId = mGcm.register(SENDER_ID); + String regId = mGcm.register(PROJECT_NUMBER); msg = "Device registered, registration ID=" + regId; // You should send the registration ID to your server over HTTP,