Better message parsing and notification building

This commit is contained in:
Dan Galpin
2015-05-24 23:52:11 -07:00
parent 83cfcaff0f
commit 3a0b035c69
3 changed files with 124 additions and 33 deletions

View File

@@ -21,20 +21,17 @@
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
<!-- Permissions required by the sync adapter --> <!-- Permissions required by the sync adapter -->
<uses-permission <uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
android:name="android.permission.READ_SYNC_SETTINGS"/> <uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />
<uses-permission <uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
android:name="android.permission.WRITE_SYNC_SETTINGS"/>
<uses-permission
android:name="android.permission.AUTHENTICATE_ACCOUNTS"/>
<!-- Permissions required to make our UI more friendly --> <!-- Permissions required to make our UI more friendly -->
<uses-permission <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- Permissions required for Google Cloud Messaging --> <!-- Permissions required for Google Cloud Messaging -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="com.example.android.sunshine.app.permission.C2D_MESSAGE" <permission
android:name="com.example.android.sunshine.app.permission.C2D_MESSAGE"
android:protectionLevel="signature" /> android:protectionLevel="signature" />
<uses-permission android:name="com.example.android.sunshine.app.permission.C2D_MESSAGE" /> <uses-permission android:name="com.example.android.sunshine.app.permission.C2D_MESSAGE" />
@@ -46,8 +43,8 @@
android:supportsRtl="true"> android:supportsRtl="true">
<activity <activity
android:name=".MainActivity" android:name=".MainActivity"
android:theme="@style/ForecastTheme" android:label="@string/app_name"
android:label="@string/app_name" > android:theme="@style/ForecastTheme" >
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
@@ -66,22 +63,24 @@
android:name=".SettingsActivity" android:name=".SettingsActivity"
android:label="@string/title_activity_settings" android:label="@string/title_activity_settings"
android:parentActivityName=".MainActivity" android:parentActivityName=".MainActivity"
android:theme="@style/SettingsTheme"> android:theme="@style/SettingsTheme" >
<meta-data <meta-data
android:name="android.support.PARENT_ACTIVITY" android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.android.sunshine.app.MainActivity" /> android:value="com.example.android.sunshine.app.MainActivity" />
</activity> </activity>
<provider <provider
android:authorities="@string/content_authority"
android:name=".data.WeatherProvider" android:name=".data.WeatherProvider"
android:authorities="@string/content_authority"
android:exported="false" android:exported="false"
android:syncable="true" /> android:syncable="true" />
<!-- SyncAdapter's dummy authentication service --> <!-- SyncAdapter's dummy authentication service -->
<service android:name=".sync.SunshineAuthenticatorService"> <service android:name=".sync.SunshineAuthenticatorService" >
<intent-filter> <intent-filter>
<action android:name="android.accounts.AccountAuthenticator" /> <action android:name="android.accounts.AccountAuthenticator" />
</intent-filter> </intent-filter>
<meta-data <meta-data
android:name="android.accounts.AccountAuthenticator" android:name="android.accounts.AccountAuthenticator"
android:resource="@xml/authenticator" /> android:resource="@xml/authenticator" />
@@ -90,25 +89,25 @@
<!-- The SyncAdapter service --> <!-- The SyncAdapter service -->
<service <service
android:name=".sync.SunshineSyncService" android:name=".sync.SunshineSyncService"
android:exported="true" android:exported="true" >
>
<intent-filter> <intent-filter>
<action android:name="android.content.SyncAdapter" /> <action android:name="android.content.SyncAdapter" />
</intent-filter> </intent-filter>
<meta-data <meta-data
android:name="android.content.SyncAdapter" android:name="android.content.SyncAdapter"
android:resource="@xml/syncadapter" /> android:resource="@xml/syncadapter" />
</service> </service>
<!-- GCM receiver - Commented out until we write the class --> <!-- GCM receiver -->
<!--<receiver--> <receiver
<!--android:name=".GcmBroadcastReceiver"--> android:name=".GcmBroadcastReceiver"
<!--android:permission="com.google.android.c2dm.permission.SEND" >--> android:permission="com.google.android.c2dm.permission.SEND" >
<!--<intent-filter>--> <intent-filter>
<!--<action android:name="com.google.android.c2dm.intent.RECEIVE" />--> <action android:name="com.google.android.c2dm.intent.RECEIVE" />
<!--<category android:name="com.example.android.sunshine.app" />--> <category android:name="com.example.android.sunshine.app" />
<!--</intent-filter>--> </intent-filter>
<!--</receiver>--> </receiver>
</application> </application>
</manifest> </manifest>

View File

@@ -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());
}
}

View File

@@ -35,7 +35,6 @@ import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.gcm.GoogleCloudMessaging; import com.google.android.gms.gcm.GoogleCloudMessaging;
import java.io.IOException; import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;
public class MainActivity extends ActionBarActivity implements ForecastFragment.Callback { 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"; private static final String PROPERTY_APP_VERSION = "appVersion";
/** /**
* Substitute you own sender ID here. This is the project number you got * Substitute you own project number here. This project number comes
* from the API Console. * from the Google Developers Console.
*/ */
String SENDER_ID = "Your-Sender-ID"; static final String PROJECT_NUMBER = "Your Project Number";
private boolean mTwoPane; private boolean mTwoPane;
private String mLocation; private String mLocation;
@@ -91,10 +90,10 @@ public class MainActivity extends ActionBarActivity implements ForecastFragment.
mGcm = GoogleCloudMessaging.getInstance(this); mGcm = GoogleCloudMessaging.getInstance(this);
String regId = getRegistrationId(this); String regId = getRegistrationId(this);
if (SENDER_ID.equals("Your-Sender-ID")) { if (PROJECT_NUMBER.equals("Your Project Number")) {
new AlertDialog.Builder(this) new AlertDialog.Builder(this)
.setTitle("Needs Sender ID") .setTitle("Needs Project Number")
.setMessage("GCM will not function in Sunshine until you replace your Sender ID with a Sender ID from the Google Developers Console.") .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) .setPositiveButton(android.R.string.ok, null)
.create().show(); .create().show();
} else if (regId.isEmpty()) { } else if (regId.isEmpty()) {
@@ -263,7 +262,7 @@ public class MainActivity extends ActionBarActivity implements ForecastFragment.
if (mGcm == null) { if (mGcm == null) {
mGcm = GoogleCloudMessaging.getInstance(context); mGcm = GoogleCloudMessaging.getInstance(context);
} }
String regId = mGcm.register(SENDER_ID); String regId = mGcm.register(PROJECT_NUMBER);
msg = "Device registered, registration ID=" + regId; msg = "Device registered, registration ID=" + regId;
// You should send the registration ID to your server over HTTP, // You should send the registration ID to your server over HTTP,