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

Commit 2efd4b0

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

File tree

3 files changed

+36
-16
lines changed

3 files changed

+36
-16
lines changed

demo-push/app/main-page.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import * as observable from 'tns-core-modules/data/observable';
22
import * as pages from 'tns-core-modules/ui/page';
3-
import { HelloWorldModel } from './main-view-model';
3+
import { PushViewModel } from './push-view-model';
44

5-
const model = new HelloWorldModel();
5+
const model = new PushViewModel();
66

77
// Event handler for Page 'loaded' event attached in main-page.xml
88
export function pageLoaded(args: observable.EventData) {

demo-push/app/main-page.xml

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,18 @@
1616
<TabViewItem title="Push demo">
1717
<TabViewItem.view>
1818
<ScrollView>
19-
<GridLayout rows="auto, auto, auto, auto, auto, auto, auto" horizontalAlignment="stretch" class="tab-content" marginTop="20">
19+
<GridLayout rows="auto, auto, auto, auto, auto, auto, auto, auto, auto" horizontalAlignment="stretch" class="tab-content" marginTop="20">
2020
<Label row="0" text="Note that none of these functions work on the simulator. Use a real device!" textWrap="true" />
21-
<Button row="1" text="Are notifications enabled?" tap="{{ doGetAreNotificationsEnabled }}" class="button button-messaging"/>
22-
<Button row="2" text="Get current push token" tap="{{ doGetCurrentPushToken }}" class="button button-messaging"/>
23-
<iOS>
24-
<Button row="3" text="Register for interactive push" tap="{{ doRegisterForInteractivePush }}" class="button button-messaging"/>
21+
<Button row="1" text="Request user consent - do this first!" tap="{{ doRequestConsent }}" class="button button-messaging"/>
2522

26-
<Button row="4" text="Unregister for push" tap="{{ doUnregisterForPushNotifications }}" class="button button-messaging"/>
27-
<Label row="5" text="After unregistering you can register again with this function:" textWrap="true" />
28-
<Button row="6" text="Register for push again" tap="{{ doRegisterForPushNotificationsAgain }}" class="button button-messaging"/>
23+
<Label row="2" text="Note that none of these functions work on the simulator. Use a real device!" textWrap="true" />
24+
<Button row="3" text="Are notifications enabled?" tap="{{ doGetAreNotificationsEnabled }}" class="button button-messaging"/>
25+
<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"/>
2931
</iOS>
3032
</GridLayout>
3133
</ScrollView>

demo-push/app/main-view-model.ts renamed to demo-push/app/push-view-model.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { Observable } from "tns-core-modules/data/observable";
22
import * as firebase from "nativescript-plugin-firebase";
33
import { messaging } from "nativescript-plugin-firebase/messaging";
4-
import { alert } from "tns-core-modules/ui/dialogs";
4+
import { alert, confirm } from "tns-core-modules/ui/dialogs";
55
import * as platform from "tns-core-modules/platform";
6+
import * as applicationSettings from "tns-core-modules/application-settings";
67

78
const getCircularReplacer = () => {
89
const seen = new WeakSet;
@@ -17,21 +18,38 @@ const getCircularReplacer = () => {
1718
};
1819
};
1920

20-
export class HelloWorldModel extends Observable {
21+
export class PushViewModel extends Observable {
22+
23+
private static APP_REGISTERED_FOR_NOTIFICATIONS = "APP_REGISTERED_FOR_NOTIFICATIONS";
2124

2225
constructor() {
2326
super();
24-
this.doRegisterPushHandlers();
27+
if (applicationSettings.getBoolean(PushViewModel.APP_REGISTERED_FOR_NOTIFICATIONS, false)) {
28+
this.doRegisterPushHandlers();
29+
}
30+
}
31+
32+
public doRequestConsent(): void {
33+
confirm({
34+
title: "We'd like to send notifications",
35+
message: "Do you agree? Please do, we won't spam you. Promised.",
36+
okButtonText: "Yep!",
37+
cancelButtonText: "Nooo"
38+
}).then(pushAllowed => {
39+
if (pushAllowed) {
40+
applicationSettings.setBoolean(PushViewModel.APP_REGISTERED_FOR_NOTIFICATIONS, true);
41+
this.doRegisterPushHandlers();
42+
}
43+
});
2544
}
2645

2746
public doGetCurrentPushToken(): void {
2847
firebase.getCurrentPushToken()
2948
.then(token => {
30-
// may be null if not known yet
31-
console.log("Current push token: " + token);
49+
// may be null/undefined if not known yet
3250
alert({
3351
title: "Current Push Token",
34-
message: (token === null ? "Not received yet" : token + ("\n\nSee the console log if you want to copy-paste it.")),
52+
message: (!token ? "Not received yet (note that on iOS this does not work on a simulator)" : token + ("\n\nSee the console log if you want to copy-paste it.")),
3553
okButtonText: "OK, thx"
3654
});
3755
})

0 commit comments

Comments
 (0)