Skip to content

Commit 17fc5a5

Browse files
author
Victor
committed
Fix #14 -> Support nested fragments
1 parent abf5472 commit 17fc5a5

19 files changed

+300
-43
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Add RxGcm dependency and Google Services plugin to project level build.gradle.
1515
apply plugin: 'com.google.gms.google-services'
1616
1717
dependencies {
18-
compile 'com.github.VictorAlbertos:RxGcm:0.2.3'
18+
compile 'com.github.VictorAlbertos:RxGcm:0.2.4'
1919
compile 'io.reactivex:rxjava:1.1.5'
2020
}
2121
```

app/build.gradle

+2
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,5 @@ dependencies {
6565
exclude module: 'recyclerview-v7'
6666
}
6767
}
68+
69+
apply plugin: 'com.google.gms.google-services'

app/google-services.json

+41-22
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
{
22
"project_info": {
3-
"project_id": "api-project-867071343993",
43
"project_number": "867071343993",
5-
"name": "RxGcm"
4+
"project_id": "api-project-867071343993"
65
},
76
"client": [
87
{
98
"client_info": {
109
"mobilesdk_app_id": "1:867071343993:android:97d7999c1214eb2e",
11-
"client_id": "android:victoralbertos.io.rxgcm",
12-
"client_type": 1,
1310
"android_client_info": {
1411
"package_name": "victoralbertos.io.rxgcm"
1512
}
@@ -20,22 +17,19 @@
2017
"client_type": 3
2118
}
2219
],
23-
"api_key": [],
20+
"api_key": [
21+
{
22+
"current_key": "AIzaSyBSq38t9MUGvXsR0URE3NeEpkU6DlzrftM"
23+
}
24+
],
2425
"services": {
2526
"analytics_service": {
2627
"status": 1
2728
},
28-
"cloud_messaging_service": {
29-
"status": 2,
30-
"apns_config": []
31-
},
3229
"appinvite_service": {
3330
"status": 1,
3431
"other_platform_oauth_client": []
3532
},
36-
"google_signin_service": {
37-
"status": 1
38-
},
3933
"ads_service": {
4034
"status": 1
4135
}
@@ -44,8 +38,6 @@
4438
{
4539
"client_info": {
4640
"mobilesdk_app_id": "1:867071343993:android:8343de6aba8c805c",
47-
"client_id": "android:victoralbertos.io.rx_gcm_sample",
48-
"client_type": 1,
4941
"android_client_info": {
5042
"package_name": "victoralbertos.io.rx_gcm_sample"
5143
}
@@ -56,28 +48,55 @@
5648
"client_type": 3
5749
}
5850
],
59-
"api_key": [],
51+
"api_key": [
52+
{
53+
"current_key": "AIzaSyBSq38t9MUGvXsR0URE3NeEpkU6DlzrftM"
54+
}
55+
],
6056
"services": {
6157
"analytics_service": {
6258
"status": 1
6359
},
64-
"cloud_messaging_service": {
65-
"status": 2,
66-
"apns_config": []
67-
},
6860
"appinvite_service": {
6961
"status": 1,
7062
"other_platform_oauth_client": []
7163
},
72-
"google_signin_service": {
64+
"ads_service": {
7365
"status": 1
66+
}
67+
}
68+
},
69+
{
70+
"client_info": {
71+
"mobilesdk_app_id": "1:867071343993:android:00c99bb6a617eb0b",
72+
"android_client_info": {
73+
"package_name": "org.base_app_android"
74+
}
75+
},
76+
"oauth_client": [
77+
{
78+
"client_id": "867071343993-4eqg04da1uki8sinha4lupmmj8umgb3r.apps.googleusercontent.com",
79+
"client_type": 3
80+
}
81+
],
82+
"api_key": [
83+
{
84+
"current_key": "AIzaSyBSq38t9MUGvXsR0URE3NeEpkU6DlzrftM"
85+
}
86+
],
87+
"services": {
88+
"analytics_service": {
89+
"status": 1
90+
},
91+
"appinvite_service": {
92+
"status": 1,
93+
"other_platform_oauth_client": []
7494
},
7595
"ads_service": {
7696
"status": 1
7797
}
7898
}
7999
}
80100
],
81-
"client_info": [],
82-
"ARTIFACT_VERSION": "1"
101+
"configuration_version": "1"
83102
}

app/src/androidTest/java/victoralbertos/io/rxgcm/NotificationsTest.java

+18-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import victoralbertos.io.rxgcm.data.api.GcmServerService;
1414
import victoralbertos.io.rxgcm.presentation.FragmentBase;
15-
import victoralbertos.io.rxgcm.presentation.HostActivityIssues;
15+
import victoralbertos.io.rxgcm.presentation.StartActivity;
1616

1717
import static android.support.test.espresso.Espresso.onView;
1818
import static android.support.test.espresso.action.ViewActions.click;
@@ -30,12 +30,14 @@
3030
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
3131
public class NotificationsTest {
3232
private final static String TITLE = "A tittle", BODY = "A Body";
33-
@Rule public ActivityTestRule<HostActivityIssues> mActivityRule = new ActivityTestRule(HostActivityIssues.class);
33+
@Rule public ActivityTestRule<StartActivity> mActivityRule = new ActivityTestRule(StartActivity.class);
3434

3535
@Test public void _1_Send_And_Receive_Notification_On_Foreground() {
3636
//Conservative delay to wait for token in case app is not previously installed
3737
waitTime(7000);
3838

39+
onView(withId(R.id.bt_no_nested_fragment)).perform(click());
40+
3941
//Send issue
4042
onView(withId(R.id.et_title)).perform(click(), replaceText(TITLE), closeSoftKeyboard());
4143
onView(withId(R.id.et_body)).perform(click(), replaceText(BODY), closeSoftKeyboard());
@@ -60,7 +62,20 @@ public class NotificationsTest {
6062
onView(withId(R.id.rv_notifications)).perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));
6163
}
6264

63-
@Test public void _2_Send_And_Receive_Notification_On_Background() {
65+
@Test public void _2_Send_And_Receive_Notification_On_Foreground_Nested_Fragment() {
66+
onView(withId(R.id.bt_nested_fragment)).perform(click());
67+
68+
//Send nested supply
69+
onView(withId(R.id.et_title)).perform(click(), replaceText(TITLE), closeSoftKeyboard());
70+
onView(withId(R.id.et_body)).perform(click(), replaceText(BODY), closeSoftKeyboard());
71+
onView(withId(R.id.bt_send_supply)).perform(click());
72+
73+
waitTime(3000);
74+
75+
onView(withId(R.id.rv_notifications)).perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));
76+
}
77+
78+
@Test public void _3_Send_And_Receive_Notification_On_Background() {
6479
AppGcmReceiverUIBackground.initTestBackgroundMessage();
6580
mActivityRule.getActivity().finish();
6681

app/src/main/AndroidManifest.xml

+12-3
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,31 @@
1010
android:supportsRtl="true"
1111
android:theme="@style/AppTheme">
1212
<activity
13-
android:name=".presentation.HostActivityIssues"
14-
android:label="Issues"
15-
android:noHistory="true"
13+
android:name=".presentation.StartActivity"
14+
android:label="Start activity"
1615
android:screenOrientation="portrait"
1716
android:theme="@style/AppTheme">
1817
<intent-filter>
1918
<action android:name="android.intent.action.MAIN" />
2019
<category android:name="android.intent.category.LAUNCHER" />
2120
</intent-filter>
2221
</activity>
22+
<activity
23+
android:name=".presentation.HostActivityIssues"
24+
android:label="Issues"
25+
android:screenOrientation="portrait"
26+
android:theme="@style/AppTheme"/>
2327
<activity
2428
android:name=".presentation.HostActivitySupplies"
2529
android:label="Supplies"
2630
android:noHistory="true"
2731
android:screenOrientation="portrait"
2832
android:theme="@style/AppTheme" />
33+
<activity
34+
android:name=".presentation.nested_fragment.HostActivityNestedFragment"
35+
android:label="Nested supplies"
36+
android:screenOrientation="portrait"
37+
android:theme="@style/AppTheme"/>
2938
</application>
3039

3140
</manifest>

app/src/main/java/victoralbertos/io/rxgcm/AppGcmReceiverData.java

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class AppGcmReceiverData implements GcmReceiverData {
2323

2424
if (message.target().equals(GcmServerService.TARGET_ISSUE_GCM)) Cache.Pool.addIssue(new Notification(title, body));
2525
else if (message.target().equals(GcmServerService.TARGET_SUPPLY_GCM)) Cache.Pool.addSupply(new Notification(title, body));
26+
else if (message.target().equals(GcmServerService.TARGET_NESTED_SUPPLY_GCM)) Cache.Pool.addNestedSupply(new Notification(title, body));
2627
});
2728
}
2829
}

app/src/main/java/victoralbertos/io/rxgcm/data/Cache.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,30 @@ public enum Cache {
1313

1414
private final List<Notification> issues = new ArrayList<>();
1515
private final List<Notification> supplies = new ArrayList<>();
16+
private final List<Notification> nestedSupplies = new ArrayList<>();
1617

1718
public void addIssue(Notification issue) {
1819
issues.add(issue);
1920
}
2021

21-
2222
public void addSupply(Notification supply) {
2323
supplies.add(supply);
2424
}
2525

26+
public void addNestedSupply(Notification supply) {
27+
nestedSupplies.add(supply);
28+
}
29+
2630
public List<Notification> getIssues() {
2731
return issues;
2832
}
2933

3034
public List<Notification> getSupplies() {
3135
return supplies;
3236
}
37+
38+
public List<Notification> getNestedSupplies() {
39+
return nestedSupplies;
40+
}
41+
3342
}

app/src/main/java/victoralbertos/io/rxgcm/data/api/GcmServerService.java

+9
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class GcmServerService {
1313
private final ApiGcmServer apiGcmServer;
1414
public final static String TARGET_ISSUE_GCM = "target_issue_gcm";
1515
public final static String TARGET_SUPPLY_GCM = "target_supply_gcm";
16+
public final static String TARGET_NESTED_SUPPLY_GCM = "target_nested_supply_gcm";
1617
public final static String TITLE = "title", BODY = "body";
1718

1819
public GcmServerService() {
@@ -39,6 +40,14 @@ public Observable<Boolean> sendGcmNotificationRequestingSupply(String title, Str
3940
.onErrorReturn(throwable -> false);
4041
}
4142

43+
public Observable<Boolean> sendGcmNotificationRequestingNestedSupply(String title, String body) {
44+
return RxGcm.Notifications.currentToken()
45+
.map(token -> new Payload(token, title, body, TARGET_NESTED_SUPPLY_GCM))
46+
.concatMap(payload -> apiGcmServer.sendNotification(payload))
47+
.map(gcmResponseServerResponse -> gcmResponseServerResponse.body().success())
48+
.onErrorReturn(throwable -> false);
49+
}
50+
4251
static class GcmResponseServer {
4352
private final int success;
4453

app/src/main/java/victoralbertos/io/rxgcm/presentation/FragmentBase.java

+18-2
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,18 @@ private void sendSupplyClickListener() {
7272
});
7373
}
7474

75+
protected void sendNestedSupplyClickListener() {
76+
findViewById(R.id.bt_send_supply).setOnClickListener(view -> {
77+
String title = ((EditText) findViewById(R.id.et_title)).getText().toString();
78+
String body = ((EditText) findViewById(R.id.et_body)).getText().toString();
79+
80+
gcmServerService.sendGcmNotificationRequestingNestedSupply(title, body).subscribe(success -> {
81+
if (success) Log.d("sendGcmNotification", "Message sent");
82+
else Log.e("sendGcmNotification", "Message not sent");
83+
});
84+
});
85+
}
86+
7587
private void goToSuppliesClickListener() {
7688
Button button = (Button) findViewById(R.id.bt_go_to_other_screen);
7789
button.setText(this instanceof FragmentSupplies ? "Go to issues" : "Go to supplies");
@@ -87,7 +99,11 @@ private void setUpRecyclerView() {
8799
rv_notifications.setHasFixedSize(true);
88100
rv_notifications.setLayoutManager(new LinearLayoutManager(getActivity()));
89101

90-
List<Notification> notifications = this instanceof FragmentSupplies ? Cache.Pool.getSupplies() : Cache.Pool.getIssues();
102+
List<Notification> notifications;
103+
if (this instanceof FragmentSupplies) notifications = Cache.Pool.getSupplies();
104+
else if (this instanceof FragmentIssue) notifications = Cache.Pool.getIssues();
105+
else notifications = Cache.Pool.getNestedSupplies();
106+
91107
notificationAdapter = new NotificationAdapter(notifications);
92108
rv_notifications.setAdapter(notificationAdapter);
93109
}
@@ -108,7 +124,7 @@ public void showAlert() {
108124
}
109125

110126

111-
private View findViewById(int id) {
127+
protected View findViewById(int id) {
112128
return getView().findViewById(id);
113129
}
114130
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package victoralbertos.io.rxgcm.presentation;
2+
3+
import android.content.Intent;
4+
import android.os.Bundle;
5+
import android.support.v7.app.AppCompatActivity;
6+
7+
import victoralbertos.io.rxgcm.R;
8+
import victoralbertos.io.rxgcm.presentation.nested_fragment.HostActivityNestedFragment;
9+
10+
/**
11+
* Created by victor on 12/05/16.
12+
*/
13+
public class StartActivity extends AppCompatActivity {
14+
15+
@Override protected void onCreate(Bundle savedInstanceState) {
16+
super.onCreate(savedInstanceState);
17+
setContentView(R.layout.start_activity);
18+
19+
findViewById(R.id.bt_no_nested_fragment).setOnClickListener(v -> {
20+
startActivity(new Intent(StartActivity.this, HostActivityIssues.class));
21+
});
22+
23+
findViewById(R.id.bt_nested_fragment).setOnClickListener(v -> {
24+
startActivity(new Intent(StartActivity.this, HostActivityNestedFragment.class));
25+
});
26+
}
27+
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package victoralbertos.io.rxgcm.presentation.nested_fragment;
2+
3+
import android.os.Bundle;
4+
import android.view.View;
5+
6+
import rx.Observable;
7+
import rx_gcm.Message;
8+
import victoralbertos.io.rxgcm.R;
9+
import victoralbertos.io.rxgcm.data.api.GcmServerService;
10+
import victoralbertos.io.rxgcm.presentation.FragmentBase;
11+
12+
/**
13+
* Created by victor on 08/02/16.
14+
*/
15+
public class FragmentNestedSupplies extends FragmentBase {
16+
17+
@Override public void onActivityCreated(Bundle savedInstanceState) {
18+
super.onActivityCreated(savedInstanceState);
19+
sendNestedSupplyClickListener();
20+
21+
findViewById(R.id.bt_go_to_other_screen).setVisibility(View.GONE);
22+
findViewById(R.id.bt_send_issue).setVisibility(View.GONE);
23+
}
24+
25+
@Override public void onTargetNotification(Observable<Message> oMessage) {
26+
oMessage.subscribe(message -> {
27+
notificationAdapter.notifyDataSetChanged();
28+
});
29+
}
30+
31+
@Override public boolean matchesTarget(String key) {
32+
return GcmServerService.TARGET_NESTED_SUPPLY_GCM.equals(key);
33+
}
34+
35+
}

0 commit comments

Comments
 (0)