Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5.04 receiving a message #69

Open
wants to merge 7 commits into
base: 5.04_Receiving_a_Message
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ gen/
# Can explicitly add if we want, but shouldn't do so blindly. Licenses, bloat, etc.
/libs


# Build stuff (auto-generated by android update project ...)
build.xml
ant.properties
Expand Down
8 changes: 5 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

android {
compileSdkVersion 21
Expand Down Expand Up @@ -28,5 +27,8 @@ dependencies {
compile 'com.github.bumptech.glide:glide:3.5.2'
compile 'com.android.support:appcompat-v7:21.0.2'
compile 'com.android.support:support-annotations:22.0.0'
compile 'com.google.android.gms:play-services-gcm:8.3.0'
}
compile 'com.google.android.gms:play-services-base:10.0.1'
compile 'com.google.firebase:firebase-core:10.0.1'
compile 'com.google.firebase:firebase-messaging:10.0.1'}

apply plugin: 'com.google.gms.google-services'
34 changes: 6 additions & 28 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE" />

<!-- Permissions required for Google Cloud Messaging -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<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" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand Down Expand Up @@ -101,35 +94,20 @@
android:resource="@xml/syncadapter" />
</service>

<!-- The Google Cloud Messaging receiver and services -->
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.example.android.sunshine.app" />
</intent-filter>
</receiver>
<!-- Firebase Cloud Messaging Services -->
<service
android:name=".gcm.MyGcmListenerService"
android:exported="false" >
android:name=".firebase.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>

<service
android:name=".gcm.MyInstanceIDListenerService"
android:exported="false">
android:name=".firebase.MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID"/>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<service
android:name=".gcm.RegistrationIntentService"
android:exported="false" >
</service>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,18 @@
package com.example.android.sunshine.app;

import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import com.example.android.sunshine.app.gcm.RegistrationIntentService;
import com.example.android.sunshine.app.sync.SunshineSyncAdapter;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;


public class MainActivity extends ActionBarActivity implements ForecastFragment.Callback {

private final String LOG_TAG = MainActivity.class.getSimpleName();
Expand All @@ -43,6 +41,7 @@ public class MainActivity extends ActionBarActivity implements ForecastFragment.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mLocation = Utility.getPreferredLocation(this);

setContentView(R.layout.activity_main);
Expand Down Expand Up @@ -70,22 +69,10 @@ protected void onCreate(Bundle savedInstanceState) {

SunshineSyncAdapter.initializeSyncAdapter(this);

// If Google Play Services is up to date, we'll want to register GCM. If it is not, we'll
// skip the registration and this device will not receive any downstream messages from
// our fake server. Because weather alerts are not a core feature of the app, this should
// not affect the behavior of the app, from a user perspective.
if (checkPlayServices()) {
// Because this is the initial creation of the app, we'll want to be certain we have
// a token. If we do not, then we will start the IntentService that will register this
// application with GCM.
SharedPreferences sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(this);
boolean sentToken = sharedPreferences.getBoolean(SENT_TOKEN_TO_SERVER, false);
if (!sentToken) {
Intent intent = new Intent(this, RegistrationIntentService.class);
startService(intent);
}
}
// For informing user of Google Play Services availability
// FCM now auto registers token
checkPlayServices();

}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (C) 2016 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.firebase;

import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;

import com.example.android.sunshine.app.MainActivity;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService{
private static final String TAG = "FcmInstanceIdService";

/**
* Called if InstanceID token is updated. This may occur if the security of
* the previous token had been compromised. Note that this is also called
* when the InstanceID token is initially generated, so this is where
* you retrieve the token.
*/
@Override
public void onTokenRefresh() {

Log.w(TAG,"onTokenRefresh of instance id service!");

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

String token = FirebaseInstanceId.getInstance().getToken();
sendRegistrationToServer(token);

// You should store a boolean that indicates whether the generated token has been
// sent to your server. If the boolean is false, send the token to your server,
// otherwise your server should have already received the token.
sharedPreferences.edit().putBoolean(MainActivity.SENT_TOKEN_TO_SERVER, true).apply();
}

/**
* Normally, you would want to persist the registration to third-party servers. Because we do
* not have a server, and are faking it with a website, you'll want to log the token instead.
* That way you can see the value in logcat, and note it for future use in the website.
*
* @param token The new token.
*/
private void sendRegistrationToServer(String token) {
Log.i(TAG, "FCM Registration Token: " + token);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2015 The Android Open Source Project
* Copyright (C) 2016 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.
Expand All @@ -14,26 +14,27 @@
* limitations under the License.
*/

package com.example.android.sunshine.app.gcm;
package com.example.android.sunshine.app.firebase;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;

import com.example.android.sunshine.app.MainActivity;
import com.example.android.sunshine.app.R;
import com.google.android.gms.gcm.GcmListenerService;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

public class MyGcmListenerService extends GcmListenerService {
import java.util.Map;

private static final String TAG = "MyGcmListenerService";
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFcmMessagingService";

private static final String EXTRA_DATA = "data";
private static final String EXTRA_WEATHER = "weather";
Expand All @@ -44,12 +45,16 @@ public class MyGcmListenerService extends GcmListenerService {
/**
* Called when message is received.
*
* @param from SenderID of the sender.
* @param data Data bundle containing message data as key/value pairs.
* For Set of keys use data.keySet().
* @param message RemoteMessage sent from server - use getData() for key/value pairs
* & getFrom() for sender
*/

@Override
public void onMessageReceived(String from, Bundle data) {
public void onMessageReceived(RemoteMessage message) {
Log.w(TAG,"Message Received!");
Map<String,String> data = message.getData();
String from = message.getFrom();

// Time to unparcel the bundle!
if (!data.isEmpty()) {
// TODO: gcm_default sender ID comes from the API console
Expand All @@ -60,13 +65,13 @@ public void onMessageReceived(String from, Bundle data) {
// Not a bad idea to check that the message is coming from your server.
if ((senderId).equals(from)) {
// Process message and then post a notification of the received message.
String weather = data.getString(EXTRA_WEATHER);
String location = data.getString(EXTRA_LOCATION);
String weather = data.get(EXTRA_WEATHER);
String location = data.get(EXTRA_LOCATION);
String alert =
String.format(getString(R.string.gcm_weather_alert), weather, location);
String.format(getString(R.string.fcm_weather_alert), weather, location);
sendNotification(alert);
}
Log.i(TAG, "Received: " + data.toString());
Log.i(TAG, "Received: " + message.toString());
}
}

Expand Down Expand Up @@ -98,4 +103,4 @@ private void sendNotification(String message) {
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
}
}

This file was deleted.

Loading