Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit 6caf824

Browse files
#518 Background Messages Android: fix foreground=true/false flag, removed notifications without 'from'.
1 parent 1167bb4 commit 6caf824

File tree

7 files changed

+89
-8
lines changed

7 files changed

+89
-8
lines changed

demo/app/main-view-model.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,10 @@ export class HelloWorldModel extends Observable {
255255
}
256256
},
257257
onMessageReceivedCallback: message => {
258-
console.log("--- message received: " + message);
259258
setTimeout(() => {
260259
alert({
261260
title: "Push message!",
262-
message: (message.title !== undefined ? message.title : ""),
261+
message: (message.title !== undefined ? message.title : "") + (message.data ? " Data: " + JSON.stringify(message.data) : ""),
263262
okButtonText: "Sw33t"
264263
});
265264
}, 500);

demo/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
22
"nativescript": {
33
"id": "org.nativescript.firebasedemo",
4-
"tns-android": {
5-
"version": "3.3.1"
6-
},
74
"tns-ios": {
85
"version": "3.3.0"
6+
},
7+
"tns-android": {
8+
"version": "3.3.1"
99
}
1010
},
1111
"dependencies": {

docs/MESSAGING.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,16 @@ curl -X POST --header "Authorization: key=SERVER_KEY" --Header "Content-Type: ap
168168
* SERVER_KEY: see below
169169
* DEVICE_TOKEN: the one you got in `addOnPushTokenReceivedCallback` or `init`'s `onPushTokenReceivedCallback`
170170

171+
Examples:
172+
173+
```
174+
// noti + data
175+
curl -X POST --header "Authorization: key=AAAA9SHtZvM:APA91bGoY0H2nS8GlzzypDXSiUkNY3nrti4st4WOUs_w1A0Rttcx31U90YGv-p3U4Oql-vh-FzZzWUUPEwl47uvwhI4tB5yz4wwzrJA2fVqLEKZpDU42AQppYnU2-dsURqkyc9sKcjay2egWbfyNK2b-G2JQCqrLVA" --Header "Content-Type: application/json" https://fcm.googleapis.com/fcm/send -d "{\"notification\":{\"title\": \"My title\", \"text\": \"My text\", \"badge\": \"1\", \"sound\": \"default\"}, \"data\":{\"foo\":\"bar\"}, \"priority\": \"High\", \"to\": \"fmt3SiVgJH4:APA91bFNvamGmOr_ELaa2oFPunF7PzVfjINe3Hdu5UyqJpfdisCXZUFLyVwISqu7j5Ff9yGh3iPrMHHg3rEXwMRpQE9oCG85YV5pQ1pGjIjpAOAJCU31RKIqGLC5bIrTHxk1Dz--cmGE\"}"
176+
177+
// data only
178+
curl -X POST --header "Authorization: key=AAAA9SHtZvM:APA91bGoY0H2nS8GlzzypDXSiUkNY3nrti4st4WOUs_w1A0Rttcx31U90YGv-p3U4Oql-vh-FzZzWUUPEwl47uvwhI4tB5yz4wwzrJA2fVqLEKZpDU42AQppYnU2-dsURqkyc9sKcjay2egWbfyNK2b-G2JQCqrLVA" --Header "Content-Type: application/json" https://fcm.googleapis.com/fcm/send -d "{\"data\":{\"foo\":\"bar\"}, \"priority\": \"High\", \"to\": \"fmt3SiVgJH4:APA91bFNvamGmOr_ELaa2oFPunF7PzVfjINe3Hdu5UyqJpfdisCXZUFLyVwISqu7j5Ff9yGh3iPrMHHg3rEXwMRpQE9oCG85YV5pQ1pGjIjpAOAJCU31RKIqGLC5bIrTHxk1Dz--cmGE\"}"
179+
```
180+
171181
If you don't want a badge on the app icon, remove the `badge` property or set it to 0. Note that launching the app clears the badge anyway.
172182

173183
<img src="images/push-server-key.png" width="459px" height="220px" alt="Push server key"/>

src/firebase.android.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,17 @@ const messagingEnabled = lazy(() => typeof(com.google.firebase.messaging) !== "u
2525
const dynamicLinksEnabled = lazy(() => typeof(com.google.android.gms.appinvite) !== "undefined");
2626

2727
(() => {
28-
appModule.on("launch", args => {
28+
// note that this means we need to require the plugin before the app is loaded
29+
appModule.on(appModule.launchEvent, args => {
30+
org.nativescript.plugins.firebase.FirebasePluginLifecycleCallbacks.registerCallbacks(appModule.android.nativeApp);
31+
2932
const intent = args.android;
3033
const isLaunchIntent = "android.intent.action.VIEW" === intent.getAction();
3134

3235
if (!isLaunchIntent && messagingEnabled()) {
3336
const extras = intent.getExtras();
34-
if (extras !== null) {
37+
// filter out any rubbish that doesn't have a 'from' key
38+
if (extras !== null && extras.keySet().contains("from")) {
3539
let result = {
3640
foreground: false,
3741
data: {}
1.14 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package org.nativescript.plugins.firebase;
2+
3+
import android.app.Activity;
4+
import android.app.Application;
5+
import android.os.Bundle;
6+
import android.util.Log;
7+
8+
/**
9+
* Subscribe to the Pause and Resume activity events in order to toggle the PushPlugin's status.
10+
* When the Plugin is not in active state - i.e. at foreground, notifications should be created
11+
* and published in the Notification Center, otherwise they're passed directly to the application
12+
* by invoking the onMessageReceived callback.
13+
*/
14+
public class FirebasePluginLifecycleCallbacks implements Application.ActivityLifecycleCallbacks {
15+
static final String TAG = "FirebasePluginCB";
16+
17+
private static FirebasePluginLifecycleCallbacks callbacks = new FirebasePluginLifecycleCallbacks();
18+
19+
/**
20+
* Register for the application's events
21+
* @param app
22+
*/
23+
public static void registerCallbacks(Application app) {
24+
if (app == null) {
25+
Log.d(TAG, "The application is null, it's not passed correctly!");
26+
throw new RuntimeException("The application is null, it's not passed correctly!");
27+
}
28+
29+
// clean up, not to leak and register it N times...
30+
Log.d(TAG, "Unregistering the activity lifecycle callbacks...");
31+
app.unregisterActivityLifecycleCallbacks(callbacks);
32+
33+
Log.d(TAG, "Registering the activity lifecycle callbacks...");
34+
app.registerActivityLifecycleCallbacks(callbacks);
35+
}
36+
37+
public void onActivityPaused(Activity activity) {
38+
Log.d(MyFirebaseMessagingService.TAG, "onActivityPaused: Application has been stopped.");
39+
40+
// the application is being stopped -> the push plugin is not in active/foreground state anymore
41+
MyFirebaseMessagingService.isActive = false;
42+
}
43+
44+
public void onActivityResumed(Activity activity) {
45+
Log.d(MyFirebaseMessagingService.TAG, "onActivityResumed: Application has been started");
46+
47+
// the application has been resumed-> the push plugin is now in active/foreground state
48+
MyFirebaseMessagingService.isActive = true;
49+
}
50+
51+
public void onActivityCreated(Activity activity, Bundle bundle) {
52+
}
53+
54+
public void onActivityDestroyed(Activity activity) {
55+
}
56+
57+
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
58+
}
59+
60+
public void onActivityStarted(Activity activity) {
61+
}
62+
63+
public void onActivityStopped(Activity activity) {
64+
}
65+
}

src/platforms/android/libraryproject/firebase/src/main/java/org/nativescript/plugins/firebase/MyFirebaseMessagingService.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@
1212
* This class takes care of notifications received while the app is in the foreground.
1313
*/
1414
public class MyFirebaseMessagingService extends FirebaseMessagingService {
15+
static final String TAG = "FirebasePlugin";
16+
17+
static boolean isActive = false;
1518

1619
@Override
1720
public void onMessageReceived(RemoteMessage remoteMessage) {
1821
try {
1922
final JSONObject json = new JSONObject()
20-
.put("foreground", true)
23+
.put("foreground", isActive)
2124
.put("from", remoteMessage.getFrom());
2225

2326
final RemoteMessage.Notification not = remoteMessage.getNotification();

0 commit comments

Comments
 (0)