Skip to content

Commit de79041

Browse files
committed
First Step for push notification update
1 parent a994160 commit de79041

File tree

10 files changed

+241
-30
lines changed

10 files changed

+241
-30
lines changed

.idea/gradle.xml

+1-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/Project_Default.xml

-11
This file was deleted.

.idea/inspectionProfiles/profiles_settings.xml

-7
This file was deleted.

.idea/misc.xml

+6-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/app.iml

+11-5
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,11 @@
7171
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dependency-cache" />
7272
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dex" />
7373
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dex-cache" />
74-
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/appcompat-v7/22.1.1/jars" />
75-
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/support-v4/22.1.1/jars" />
74+
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/appcompat-v7/22.2.1/jars" />
75+
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/support-v4/22.2.1/jars" />
76+
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.google.android.gms/play-services-base/8.1.0/jars" />
77+
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.google.android.gms/play-services-basement/8.1.0/jars" />
78+
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.google.android.gms/play-services-gcm/8.1.0/jars" />
7679
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" />
7780
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/jacoco" />
7881
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/javaResources" />
@@ -90,9 +93,12 @@
9093
</content>
9194
<orderEntry type="jdk" jdkName="Android API 22 Platform" jdkType="Android SDK" />
9295
<orderEntry type="sourceFolder" forTests="false" />
93-
<orderEntry type="library" exported="" name="appcompat-v7-22.1.1" level="project" />
96+
<orderEntry type="library" exported="" name="play-services-base-8.1.0" level="project" />
97+
<orderEntry type="library" exported="" name="play-services-basement-8.1.0" level="project" />
98+
<orderEntry type="library" exported="" name="play-services-gcm-8.1.0" level="project" />
99+
<orderEntry type="library" exported="" name="support-v4-22.2.1" level="project" />
94100
<orderEntry type="library" exported="" name="glide-3.5.2" level="project" />
95-
<orderEntry type="library" exported="" name="support-v4-22.1.1" level="project" />
96-
<orderEntry type="library" exported="" name="support-annotations-23.0.1" level="project" />
101+
<orderEntry type="library" exported="" name="support-annotations-22.2.1" level="project" />
102+
<orderEntry type="library" exported="" name="appcompat-v7-22.2.1" level="project" />
97103
</component>
98104
</module>

app/build.gradle

+4-3
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ android {
2121

2222
dependencies {
2323
compile fileTree(dir: 'libs', include: ['*.jar'])
24-
compile 'com.android.support:appcompat-v7:22.1.1'
24+
compile 'com.android.support:appcompat-v7:22.+'
2525
compile 'com.github.bumptech.glide:glide:3.5.2'
26-
compile 'com.android.support:support-v4:22.1.1'
27-
compile 'com.android.support:support-annotations:23.0.1'
26+
compile 'com.android.support:support-v4:22.0.1'
27+
compile 'com.android.support:support-annotations:22.0.1'
28+
compile 'com.google.android.gms:play-services-gcm:8.1.0'
2829
}

app/src/main/AndroidManifest.xml

+15
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
99
<uses-permission android:name="android.permission.VIBRATE"/>
1010
<uses-permission android:name="android.permission.INTERNET" />
11+
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
12+
<permission android:name="softwaremobility.darkgeat.sunshine.permission.C2D_MESSAGE"
13+
android:protectionLevel="signature" />
14+
<uses-permission android:name="softwaremobility.darkgeat.sunshine.permission.C2D_MESSAGE" />
15+
1116

1217
<application
1318
android:allowBackup="true"
@@ -54,6 +59,16 @@
5459
<meta-data android:name="android.content.SyncAdapter"
5560
android:resource="@xml/sync_adapter"/>
5661
</service>
62+
63+
<!-- GCM receiver - Commented out until we write the class -->
64+
<receiver
65+
android:name=".receivers.GcmBroadcastReceiver"
66+
android:permission="com.google.android.c2dm.permission.SEND" >
67+
<intent-filter>
68+
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
69+
<category android:name="softwaremobility.darkgeat.sunshine" />
70+
</intent-filter>
71+
</receiver>
5772
</application>
5873

5974
<permission android:name="softwaremobility.darkgeat.LICENCE_TO_KILL"

app/src/main/java/softwaremobility/darkgeat/sunshine/MainActivity.java

+117
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
11
package softwaremobility.darkgeat.sunshine;
22

3+
import android.content.Context;
34
import android.content.Intent;
45
import android.content.SharedPreferences;
6+
import android.content.pm.PackageInfo;
7+
import android.content.pm.PackageManager;
58
import android.net.Uri;
9+
import android.os.AsyncTask;
610
import android.preference.PreferenceManager;
711
import android.support.v7.app.ActionBarActivity;
812
import android.os.Bundle;
13+
import android.support.v7.app.AlertDialog;
14+
import android.util.Log;
915
import android.view.Menu;
1016
import android.view.MenuItem;
1117

18+
import com.google.android.gms.common.ConnectionResult;
19+
import com.google.android.gms.common.GooglePlayServicesUtil;
20+
import com.google.android.gms.gcm.GoogleCloudMessaging;
21+
22+
import java.io.IOException;
23+
import java.sql.Connection;
24+
1225
import softwaremobility.darkgeat.sunshine.sync.SyncAdapter;
1326

1427

@@ -17,12 +30,20 @@ public class MainActivity extends ActionBarActivity implements ForecastFragment.
1730
private String mLocation;
1831
private boolean mTwoPane;
1932
public static final String DETAILFRAGMENT_TAG = "DetailFragmentTAG";
33+
public static final String LOG_TAG = MainActivity.class.getSimpleName();
34+
public static final String PROPERTY_REG_ID = "registration_id";
35+
private static final String PROPERTY_APP_VERSION = "appVersion";
36+
private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
37+
private String SENDER_ID;
38+
private GoogleCloudMessaging mGcm;
2039

2140
@Override
2241
protected void onCreate(Bundle savedInstanceState) {
2342
super.onCreate(savedInstanceState);
2443
setContentView(R.layout.activity_main);
2544

45+
SENDER_ID = getString(R.string.projectIdNumber);
46+
2647
if(findViewById(R.id.detail_container) != null){
2748
mTwoPane = true;
2849
if(savedInstanceState == null){
@@ -34,6 +55,38 @@ protected void onCreate(Bundle savedInstanceState) {
3455
mTwoPane = false;
3556
}
3657
SyncAdapter.initializeSyncAdapter(this);
58+
if (checkPlayServices()) {
59+
mGcm = GoogleCloudMessaging.getInstance(this);
60+
String regId = getRegistrationId(this);
61+
62+
if (SENDER_ID.equals(getString(R.string.projectIdNumber))) {
63+
new AlertDialog.Builder(this)
64+
.setTitle("Needs Sender ID")
65+
.setMessage("GCM will not function in Sunshine until you replace your Sender ID with a Sender ID from the Google Developers Console.")
66+
.setPositiveButton(android.R.string.ok, null)
67+
.create().show();
68+
} else if (regId.isEmpty()) {
69+
registerInBackground(this);
70+
}
71+
} else {
72+
Log.i(LOG_TAG, "No valid Google Play Services APK. Weather alerts will be disabled.");
73+
// Store regID as null
74+
storeRegistrationId(this, null);
75+
}
76+
}
77+
78+
private boolean checkPlayServices() {
79+
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
80+
if (resultCode != ConnectionResult.SUCCESS){
81+
if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)){
82+
GooglePlayServicesUtil.getErrorDialog(resultCode,this,PLAY_SERVICES_RESOLUTION_REQUEST).show();
83+
}else {
84+
Log.i(LOG_TAG,"This device is not supported.");
85+
finish();
86+
}
87+
return false;
88+
}
89+
return true;
3790
}
3891

3992

@@ -105,4 +158,68 @@ public void onItemSelected(Uri dateUri) {
105158
startActivity(intent);
106159
}
107160
}
161+
162+
private void registerInBackground(final Context context){
163+
new AsyncTask<Void,Void,Void>(){
164+
165+
@Override
166+
protected Void doInBackground(Void... params) {
167+
String msg = "";
168+
try {
169+
if (mGcm == null){
170+
mGcm = GoogleCloudMessaging.getInstance(context);
171+
}
172+
String regId = mGcm.register(SENDER_ID);
173+
msg = "Device registered, registration ID = " + regId;
174+
175+
storeRegistrationId(context,regId);
176+
Log.d(LOG_TAG, msg);
177+
}catch (IOException e){}
178+
return null;
179+
}
180+
}.execute();
181+
}
182+
183+
private String getRegistrationId(Context context){
184+
final SharedPreferences preferences = getGCMPreferences();
185+
String registrationId = preferences.getString(PROPERTY_REG_ID,"");
186+
if (registrationId.isEmpty()){
187+
Log.i(LOG_TAG,"GCM Registration not Found");
188+
return "";
189+
}
190+
int registeredVersion = preferences.getInt(PROPERTY_APP_VERSION,Integer.MIN_VALUE);
191+
int currentVersion = getAppVersion(context);
192+
if (registeredVersion != currentVersion){
193+
Log.i(LOG_TAG,"App version changed.");
194+
return "";
195+
}
196+
return registrationId;
197+
}
198+
199+
private void storeRegistrationId(Context context, String regId) {
200+
final SharedPreferences preferences = getGCMPreferences();
201+
int appVersion = getAppVersion(context);
202+
Log.i(LOG_TAG, "Saving regId on app version " + appVersion);
203+
SharedPreferences.Editor editor = preferences.edit();
204+
editor.putString(PROPERTY_REG_ID,regId);
205+
editor.putInt(PROPERTY_APP_VERSION, appVersion);
206+
editor.commit();
207+
}
208+
209+
public static int getAppVersion(Context context) {
210+
try {
211+
PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(),0);
212+
return packageInfo.versionCode;
213+
} catch (PackageManager.NameNotFoundException e) {
214+
e.printStackTrace();
215+
throw new RuntimeException("Could not get package name: " + e);
216+
}
217+
}
218+
219+
private SharedPreferences getGCMPreferences() {
220+
// Sunshine persists the registration ID in shared preferences, but
221+
// how you store the registration ID in your app is up to you. Just make sure
222+
// that it is private!
223+
return getSharedPreferences(LOG_TAG, Context.MODE_PRIVATE);
224+
}
108225
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package softwaremobility.darkgeat.sunshine.receivers;
2+
3+
import android.app.NotificationManager;
4+
import android.app.PendingIntent;
5+
import android.content.BroadcastReceiver;
6+
import android.content.Context;
7+
import android.content.Intent;
8+
import android.os.Bundle;
9+
import android.support.v4.app.NotificationCompat;
10+
import android.util.Log;
11+
12+
import com.google.android.gms.gcm.GoogleCloudMessaging;
13+
14+
import softwaremobility.darkgeat.sunshine.MainActivity;
15+
import softwaremobility.darkgeat.sunshine.R;
16+
17+
/**
18+
* Created by darkgeat on 10/14/15.
19+
*/
20+
public class GcmBroadcastReceiver extends BroadcastReceiver {
21+
private final String LOG_TAG = BroadcastReceiver.class.getSimpleName();
22+
23+
private static final String EXTRA_SENDER = "from";
24+
private static final String EXTRA_WEATHER = "weather";
25+
private static final String EXTRA_LOCATION = "location";
26+
27+
public static final int NOTIFICATION_ID = 1;
28+
private NotificationManager mNotificationManager;
29+
30+
public GcmBroadcastReceiver() {
31+
super();
32+
}
33+
34+
@Override
35+
public void onReceive(Context context, Intent intent) {
36+
Bundle extras = intent.getExtras();
37+
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
38+
String messageType = gcm.getMessageType(intent);
39+
40+
if (!extras.isEmpty()) { // has effect of unparcelling Bundle
41+
/*
42+
* Filter messages based on message type. Since it is likely that GCM
43+
* will be extended in the future with new message types, just ignore
44+
* any message types you're not interested in, or that you don't
45+
* recognize.
46+
*/
47+
if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
48+
// Is this our message?? Better be if you're going to act on it!
49+
if (context.getString(R.string.projectIdNumber).equals(extras.getString(EXTRA_SENDER))) {
50+
// Process message and then post a notification of the received message.
51+
String weather = extras.getString(EXTRA_WEATHER);
52+
String location = extras.getString(EXTRA_LOCATION);
53+
String alert = "Heads up: " + weather + " in " + location + "!";
54+
55+
sendNotification(context, alert);
56+
}
57+
58+
Log.i(LOG_TAG, "Received: " + extras.toString());
59+
}
60+
}
61+
}
62+
63+
// Put the message into a notification and post it.
64+
// This is just one simple example of what you might choose to do with a GCM message.
65+
private void sendNotification(Context context, String msg) {
66+
mNotificationManager = (NotificationManager)
67+
context.getSystemService(Context.NOTIFICATION_SERVICE);
68+
69+
PendingIntent contentIntent =
70+
PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);
71+
72+
NotificationCompat.Builder mBuilder =
73+
new NotificationCompat.Builder(context)
74+
.setSmallIcon(R.drawable.art_storm)
75+
.setContentTitle("Weather Alert!")
76+
.setStyle(new NotificationCompat.BigTextStyle()
77+
.bigText(msg))
78+
.setContentText(msg)
79+
.setPriority(NotificationCompat.PRIORITY_HIGH);
80+
81+
mBuilder.setContentIntent(contentIntent);
82+
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
83+
}
84+
}

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

+3
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,7 @@
4242
<string name="empty_forecast_list_server_down">No weather information available. The server is not returning data.</string>
4343
<string name="empty_forecast_list_server_error">No weather information available. The server is not returning valid data. Please check for an updated version of Sunshine.</string>
4444
<string name="empty_forecast_list_invalid_location">No weather information available. The location in settings is not recognized by the weather server.</string>
45+
<!-- About GCM -->
46+
<string name="projectIdNumber" translatable="false">1048103592468</string>
47+
<string name="api_key_cloud">AIzaSyD_nNaPDP-dsV2B_tj8Sf81a9cMoXDzGNg</string>
4548
</resources>

0 commit comments

Comments
 (0)