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