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

Commit e7ef841

Browse files
Allow using this plugin as a standalone 'push notification client' - without loading the Firebase SDK #909 (Android)
1 parent 2efd4b0 commit e7ef841

File tree

15 files changed

+192
-145
lines changed

15 files changed

+192
-145
lines changed

demo-ng/app/App_Resources/Android/AndroidManifest.xml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,6 @@
5050
<action android:name="android.intent.action.MAIN" />
5151
<category android:name="android.intent.category.LAUNCHER" />
5252
</intent-filter>
53-
54-
<!-- For Firebase Dynamic Links -->
55-
<intent-filter>
56-
<action android:name="android.intent.action.VIEW"/>
57-
<category android:name="android.intent.category.DEFAULT"/>
58-
<category android:name="android.intent.category.BROWSABLE"/>
59-
<data android:host="www.coolapp.com" android:scheme="http"/>
60-
<data android:host="www.coolapp.com" android:scheme="https"/>
61-
</intent-filter>
62-
6353
</activity>
6454
<activity android:name="com.tns.ErrorReportActivity"/>
6555
</application>

demo-push/app/main-page.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323
<Label row="2" text="Note that none of these functions work on the simulator. Use a real device!" textWrap="true" />
2424
<Button row="3" text="Are notifications enabled?" tap="{{ doGetAreNotificationsEnabled }}" class="button button-messaging"/>
2525
<Button row="4" text="Get current push token" tap="{{ doGetCurrentPushToken }}" class="button button-messaging"/>
26-
<iOS>
27-
<Button row="5" text="Register for interactive push" tap="{{ doRegisterForInteractivePush }}" class="button button-messaging"/>
28-
<Button row="6" text="Unregister for push" tap="{{ doUnregisterForPushNotifications }}" class="button button-messaging"/>
29-
<Label row="7" text="After unregistering you can register again with this function:" textWrap="true" />
30-
<Button row="8" text="Register for push again" tap="{{ doRegisterForPushNotificationsAgain }}" class="button button-messaging"/>
31-
</iOS>
26+
27+
<Button row="5" text="Register for interactive push (iOS)" tap="{{ doRegisterForInteractivePush }}" class="button button-messaging"/>
28+
29+
<Button row="6" text="Unregister for push" tap="{{ doUnregisterForPushNotifications }}" class="button button-messaging"/>
30+
<Label row="7" text="After unregistering you can register again with this function:" textWrap="true" />
31+
<Button row="8" text="Register for push again" tap="{{ doRegisterForPushNotificationsAgain }}" class="button button-messaging"/>
3232
</GridLayout>
3333
</ScrollView>
3434
</TabViewItem.view>

demo-push/app/push-view-model.ts

Lines changed: 59 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -57,75 +57,76 @@ export class PushViewModel extends Observable {
5757
}
5858

5959
public doRegisterForInteractivePush(): void {
60-
if (platform.isIOS) {
61-
const model = new messaging.PushNotificationModel();
62-
model.iosSettings = new messaging.IosPushSettings();
63-
model.iosSettings.badge = false;
64-
model.iosSettings.alert = true;
65-
model.iosSettings.interactiveSettings = new messaging.IosInteractivePushSettings();
66-
model.iosSettings.interactiveSettings.actions = [
67-
{
68-
identifier: "OPEN_ACTION",
69-
title: "Open the app (if closed)",
70-
options: messaging.IosInteractiveNotificationActionOptions.foreground
71-
},
72-
{
73-
identifier: "AUTH",
74-
title: "Open the app, but only if device is not locked with a passcode",
75-
options: messaging.IosInteractiveNotificationActionOptions.foreground | messaging.IosInteractiveNotificationActionOptions.authenticationRequired
76-
},
77-
{
78-
identifier: "INPUT_ACTION",
79-
title: "Tap to reply without opening the app",
80-
type: "input",
81-
submitLabel: "Fire!",
82-
placeholder: "Load the gun..."
83-
},
84-
{
85-
identifier: "INPUT_ACTION",
86-
title: "Tap to reply and open the app",
87-
options: messaging.IosInteractiveNotificationActionOptions.foreground,
88-
type: "input",
89-
submitLabel: "OK, send it",
90-
placeholder: "Type here, baby!"
91-
},
92-
{
93-
identifier: "DELETE_ACTION",
94-
title: "Delete without opening the app",
95-
options: messaging.IosInteractiveNotificationActionOptions.destructive
96-
}
97-
];
98-
99-
model.iosSettings.interactiveSettings.categories = [{
100-
identifier: "GENERAL"
101-
}];
60+
if (!platform.isIOS) {
61+
console.log("##### Interactive push messaging is currently iOS-only!");
62+
console.log("##### Also, please make sure you don't include the 'click_action' notification property when pusing to Android.");
63+
}
10264

103-
model.onNotificationActionTakenCallback = (actionIdentifier: string, message: firebase.Message, inputText?: string) => {
104-
console.log(`onNotificationActionTakenCallback fired! \n\r Message: ${JSON.stringify(message)}, \n\r Action taken: ${actionIdentifier}`);
65+
const model = new messaging.PushNotificationModel();
66+
model.iosSettings = new messaging.IosPushSettings();
67+
model.iosSettings.badge = false;
68+
model.iosSettings.alert = true;
69+
model.iosSettings.interactiveSettings = new messaging.IosInteractivePushSettings();
70+
model.iosSettings.interactiveSettings.actions = [
71+
{
72+
identifier: "OPEN_ACTION",
73+
title: "Open the app (if closed)",
74+
options: messaging.IosInteractiveNotificationActionOptions.foreground
75+
},
76+
{
77+
identifier: "AUTH",
78+
title: "Open the app, but only if device is not locked with a passcode",
79+
options: messaging.IosInteractiveNotificationActionOptions.foreground | messaging.IosInteractiveNotificationActionOptions.authenticationRequired
80+
},
81+
{
82+
identifier: "INPUT_ACTION",
83+
title: "Tap to reply without opening the app",
84+
type: "input",
85+
submitLabel: "Fire!",
86+
placeholder: "Load the gun..."
87+
},
88+
{
89+
identifier: "INPUT_ACTION",
90+
title: "Tap to reply and open the app",
91+
options: messaging.IosInteractiveNotificationActionOptions.foreground,
92+
type: "input",
93+
submitLabel: "OK, send it",
94+
placeholder: "Type here, baby!"
95+
},
96+
{
97+
identifier: "DELETE_ACTION",
98+
title: "Delete without opening the app",
99+
options: messaging.IosInteractiveNotificationActionOptions.destructive
100+
}
101+
];
105102

106-
alert({
107-
title: "Interactive push action",
108-
message: `Message: ${JSON.stringify(message)}, \n\r Action taken: ${actionIdentifier}` + (inputText ? `, \n\r Input text: ${inputText}` : ""),
109-
okButtonText: "Nice!"
110-
});
111-
};
103+
model.iosSettings.interactiveSettings.categories = [{
104+
identifier: "GENERAL"
105+
}];
112106

113-
firebase.registerForInteractivePush(model);
107+
model.onNotificationActionTakenCallback = (actionIdentifier: string, message: firebase.Message, inputText?: string) => {
108+
console.log(`onNotificationActionTakenCallback fired! \n\r Message: ${JSON.stringify(message)}, \n\r Action taken: ${actionIdentifier}`);
114109

115-
console.log("Registered for interactive push");
116110
alert({
117-
title: "Registered for interactive push",
118-
okButtonText: "Thx!"
111+
title: "Interactive push action",
112+
message: `Message: ${JSON.stringify(message)}, \n\r Action taken: ${actionIdentifier}` + (inputText ? `, \n\r Input text: ${inputText}` : ""),
113+
okButtonText: "Nice!"
119114
});
115+
};
120116

121-
} else {
122-
console.log("Interactive push messaging is iOS-only!");
123-
}
117+
firebase.registerForInteractivePush(model);
118+
119+
console.log("Registered for interactive push");
120+
alert({
121+
title: "Registered for interactive push",
122+
okButtonText: "Thx!"
123+
});
124124
}
125125

126126
// You would normally add these handlers in 'init', but if you want you can do it seperately as well.
127127
// The benefit being your user will not be confronted with the "Allow notifications" consent popup when 'init' runs.
128128
public doRegisterPushHandlers(): void {
129+
// note that this will implicitly register for push notifications, so there's no need to call 'registerForPushNotifications'
129130
firebase.addOnPushTokenReceivedCallback(
130131
token => {
131132
// you can use this token to send to your own backend server,
@@ -137,7 +138,7 @@ export class PushViewModel extends Observable {
137138
);
138139
firebase.addOnMessageReceivedCallback(
139140
message => {
140-
console.log("------------------- push message received: " + JSON.stringify(message, getCircularReplacer()));
141+
console.log("Push message received in push-view-model: " + JSON.stringify(message, getCircularReplacer()));
141142

142143
setTimeout(() => {
143144
alert({

demo-push/app_resources/Android/src/main/AndroidManifest.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
<action android:name="android.intent.action.MAIN" />
3636
<category android:name="android.intent.category.LAUNCHER" />
3737
</intent-filter>
38-
3938
</activity>
4039
<activity android:name="com.tns.ErrorReportActivity"/>
4140
</application>
Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,5 @@
11
{
2-
"using_ios": true,
3-
"using_android": true,
4-
"firestore": false,
5-
"realtimedb": false,
6-
"authentication": false,
7-
"remote_config": false,
8-
"messaging": false,
9-
"crashlytics": false,
10-
"crash_reporting": false,
11-
"storage": false,
12-
"facebook_auth": false,
13-
"google_auth": false,
14-
"admob": false,
15-
"invites": false,
16-
"dynamic_links": false,
17-
"ml_kit": false
2+
"external_push_client_only": true,
3+
"using_ios": true,
4+
"using_android": true
185
}

docs/MESSAGING.md

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,14 @@ Version 3.3.0 of this plugin added FCM support (which is the successor of GCM).
55

66
Although using push messages in your Firebase app is really easy setting it up is not. Traditionally, especially for iOS.
77

8+
If you didn't choose this feature during installation you can add `"messaging": true` to `<app-root>/firebase.nativescript.json` and remove the `<app-root>/platforms` folder.
9+
810
### Android
9-
If you didn't choose this feature during installation you can uncomment `firebase-messaging` in [include.gradle](../platforms/android/include.gradle)
11+
No additional setup required.
1012

11-
You will not get the title and body if the notification was received while the application was in the background, but you will get the *data* payload.
12-
13-
Add the following services in the `app/App_Resources/Android/AndroidManifest.xml` to enable advanced FCM messaging:
14-
```
15-
<manifest ... >
16-
<application ... >
17-
...
18-
<service android:name="org.nativescript.plugins.firebase.MyFirebaseInstanceIDService">
19-
<intent-filter>
20-
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
21-
</intent-filter>
22-
</service>
23-
<service android:name="org.nativescript.plugins.firebase.MyFirebaseMessagingService">
24-
<intent-filter>
25-
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
26-
</intent-filter>
27-
</service>
28-
</application>
29-
</manifest>
30-
```
13+
There is a little quirk: you will currently not get the title and body if the notification was received while the application was in the background, but you will get the *data* payload.
3114

3215
### iOS
33-
If you didn't choose this feature during installation you can run the "post install" script again (see the main readme) to add messaging support.
3416

3517
#### Enable push support in Xcode
3618

@@ -280,6 +262,8 @@ The payload to trigger the notification in the screenshots above is:
280262
}
281263
```
282264

265+
> *IMPORTANT* Use the `click_action` only for push notifications on iOS. When such a message is tapped in the Android notification center the app WON'T be opened. This will probably be fixed in the future.
266+
283267
### (iOS) showing a notification while the app is in the foreground
284268
Add the `showWhenInForeground` flag to your payload:
285269

publish/scripts/installer.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,6 @@ function promptQuestionsResult(result) {
254254
writeBuildscriptHook(isSelected(result.crashlytics));
255255
}
256256

257-
// TODO figure out what we need for externalPushClientOnly==true.. but this is prolly ok:
258257
if (usingAndroid) {
259258
writeGradleFile(result);
260259
writeGoogleServiceCopyHook();
@@ -610,7 +609,7 @@ dependencies {
610609
` + (isSelected(result.crashlytics) ? `` : `//`) + ` compile "com.crashlytics.sdk.android:crashlytics:2.9.3"
611610
612611
// Firebase Cloud Messaging (FCM)
613-
` + (isSelected(result.messaging) ? `` : `//`) + ` compile "com.google.firebase:firebase-messaging:17.3.0"
612+
` + (isSelected(result.messaging) || externalPushClientOnly ? `` : `//`) + ` compile "com.google.firebase:firebase-messaging:17.3.0"
614613
615614
// Cloud Storage
616615
` + (isSelected(result.storage) ? `` : `//`) + ` compile "com.google.firebase:firebase-storage:16.0.1"

0 commit comments

Comments
 (0)