Skip to content

Commit

Permalink
How to handle notification received and app opened by user tapping th…
Browse files Browse the repository at this point in the history
…e notification #8
  • Loading branch information
EddyVerbruggen committed Sep 6, 2019
1 parent 413eaf5 commit 6300d43
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 7 deletions.
2 changes: 2 additions & 0 deletions demo/app/app.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as app from "tns-core-modules/application";

require("nativescript-local-notifications");

// Depending on your app's structure, this may be required in order to do some startup wiring on iOS.
// In this demo it's not needed because the plugin is not lazily loaded (AoT), but just to be safe..
require("nativescript-pushy");
Expand Down
1 change: 1 addition & 0 deletions demo/app/main-page.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Page xmlns="http://schemas.nativescript.org/tns.xsd" loaded="pageLoaded" class="page">
<StackLayout class="p-30">
<Button text="get push token" tap="{{ doGetDevicePushToken }}" class="button" />
<Button text="schedule local notification" tap="{{ doScheduleLocalNotification }}" class="button" />
<Label text="{{ message }}" class="m-t-15 t-20 text-center c-black" textWrap="true"/>
</StackLayout>
</Page>
11 changes: 10 additions & 1 deletion demo/app/main-view-model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { LocalNotifications } from "nativescript-local-notifications";
import { getDevicePushToken, setNotificationHandler } from "nativescript-pushy";
import { Observable } from "tns-core-modules/data/observable";
import { alert } from "tns-core-modules/ui/dialogs";

export class HelloWorldModel extends Observable {
public message: string;
Expand All @@ -21,6 +21,15 @@ export class HelloWorldModel extends Observable {
});
}

public doScheduleLocalNotification(): void {
LocalNotifications.schedule([{
id: 1,
title: "Local FTW",
body: "I'm a local notification",
at: new Date(new Date().getTime() + (10 * 1000)) // 10 seconds from now
}]).then(() => console.log("Will show a local notification in 10 seconds"));
}

public doGetDevicePushToken(): void {
getDevicePushToken()
.then(token => {
Expand Down
3 changes: 2 additions & 1 deletion demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
}
},
"dependencies": {
"nativescript-local-notifications": "~4.0.1",
"nativescript-pushy": "file:../src",
"nativescript-theme-core": "^1.0.4",
"nativescript-unit-test-runner": "0.7.0",
"nativescript-pushy": "file:../src",
"tns-core-modules": "~6.1.0"
},
"devDependencies": {
Expand Down
9 changes: 4 additions & 5 deletions src/pushy.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,13 @@ if (UIApplication.sharedApplication) {

let _userNotificationCenterDelegate;
if (parseInt(device.osVersion) >= 10) {
// adding a little delay to give other code time to wire the delegate so we can play nice.. also doesn't affect usage as we only need it for foreground scenarios
// adding a little delay to give other code time to wire the delegate
setTimeout(() => {
if (UNUserNotificationCenter.currentNotificationCenter().delegate) {
console.log("The Pushy plugin won't override the notification handler because it was already set. This means it's uncertain whether or not foreground notifications will be shown.");
} else {
_userNotificationCenterDelegate = UNUserNotificationCenterDelegateImpl.new().initDelegate();
UNUserNotificationCenter.currentNotificationCenter().delegate = _userNotificationCenterDelegate;
console.log("BEWARE: The Pushy plugin replace an already existing notification delegate. This means other (local) notification plugins may no longer work correctly!");
}
_userNotificationCenterDelegate = UNUserNotificationCenterDelegateImpl.new().initDelegate();
UNUserNotificationCenter.currentNotificationCenter().delegate = _userNotificationCenterDelegate;
}, 100);
}

Expand Down

0 comments on commit 6300d43

Please sign in to comment.