Skip to content

Commit 543cf67

Browse files
committed
updates docs for v1
1 parent b820ceb commit 543cf67

File tree

12 files changed

+252
-158
lines changed

12 files changed

+252
-158
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
---
2+
title: Push Notifications
3+
order: 400
4+
---
5+
6+
7+
## Overview
8+
9+
NativePHP for Mobile uses [Firebase Cloud Messaging](https://firebase.google.com/docs/cloud-messaging) to send push notifications to your users.
10+
11+
## Setting up your app
12+
13+
Once you create a Firebase account and create a project you will be offered to download a `google-services.json` file.
14+
15+
This file contains the configuration for your app and is used by the Firebase SDK to retrieve tokens for each device using your app.
16+
17+
Simply drag this file into the root of your Laravel project, enable `push_notifications` in the [config](/docs/mobile/1/getting-started/configuration) it will be used automatically.
18+
19+
You will see more instructions on how to configure your app in the Firebase documentation, you can ignore all of those, NativePHP handles all of that for you.
20+
21+
## Receiving Push Tokens
22+
23+
To receive push notifications, you must register a listener for the event. For example,
24+
take a look at how easy it is to listen for a `TokenGenerated` event in Livewire:
25+
26+
```php
27+
use Livewire\Attributes\On;
28+
use Livewire\Component;
29+
use Native\Mobile\Facades\System;
30+
use Native\Mobile\Events\PushNotification\TokenGenerated;
31+
32+
class PushNotifications extends Component
33+
{
34+
public function render()
35+
{
36+
return view('livewire.system.push-notifications');
37+
}
38+
39+
#[On('native:' . TokenGenerated::class)]
40+
public function handlePushNotifications(string $token)
41+
{
42+
// Do something with the token...
43+
}
44+
}
45+
```
46+
47+
Because of the nature of mobile applications you need an api server to handle these tokens. You can use Laravel's built-in `Http` facade to
48+
`POST` the token to your server, on the server side you need to associate the token with the "user" that owns the device.
49+
50+
We **strongly** recommend using [Sanctum](https://laravel.com/docs/12.x/sanctum#main-content) to handle this for you.
51+
52+
## The flow
53+
54+
Your app authenticates users against your own api server, when users create an account or login the server validates and authenticates the user and passes back a Sanctum token.
55+
56+
The token is stored in your apps `session` and is used on subsequent requests to the api server.
57+
58+
When a push notification is received, the token is sent to your api server and the server stores it for the user who sent it.
59+
60+
> Optionally, you can have a `HasMany` relationship between your users and devices,
61+
> this allows you to associate a device with a user and then use the device's token
62+
> to send push notifications to that users devices.
63+
64+
## Sending Push Notifications
65+
66+
Once you have the token, you may use it from your server-based applications to trigger Push Notifications directly to
67+
your user's device. We use a package like [google/apiclient](https://github.com/googleapis/google-api-php-client) to send the notifications.
68+
69+
This is the exact code used by the NativePHP Kitchen Sink App (available soon on all app stores and GitHub):
70+
71+
```php
72+
Route::group(['middleware' => 'auth:sanctum'], function () {
73+
Route::post('send-push-notification', PushNotificationController::class)->name('send-push-notification');
74+
});
75+
```
76+
77+
```php
78+
namespace App\Http\Controllers;
79+
80+
use App\Jobs\SendPushNotification;
81+
use Illuminate\Http\Request;
82+
83+
class PushNotificationController extends Controller
84+
{
85+
public function __invoke(Request $request)
86+
{
87+
$token = $request->get('token');
88+
89+
$request->user()->update([
90+
'push_token' => $token
91+
]);
92+
93+
SendPushNotification::dispatch($token)->delay(now()->addMinutes(1));
94+
}
95+
}
96+
```

resources/views/docs/mobile/1/getting-started/debugging.md

Lines changed: 0 additions & 6 deletions
This file was deleted.

resources/views/docs/mobile/1/getting-started/development.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,7 @@ order: 300
66

77
## The `nativephp` Directory
88

9-
After running:
10-
11-
```bash
12-
php artisan native:install
13-
```
14-
15-
you’ll see a new `nativephp` directory at the root of your Laravel project as well as a nativephp.php config file in your config folder in the Laravel root.
9+
After running: `php artisan native:install` you’ll see a new `nativephp` directory at the root of your Laravel project as well as a `nativephp.php` config file in your config folder in the Laravel root.
1610

1711
This folder contains all the native project files that NativePHP generates for you.
1812

resources/views/docs/mobile/1/getting-started/env-files.md

Lines changed: 0 additions & 6 deletions
This file was deleted.

resources/views/docs/mobile/1/getting-started/installation.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,18 @@ order: 100
2323

2424
#### For macOS
2525
```shell
26-
export JAVA_HOME=$(/usr/libexec/java_home -v 17)
26+
export JAVA_HOME=$(/usr/libexec/java_home -v 17) // This isn't required if JAVA_HOME is already set in the Windows Env Variables
2727
export ANDROID_SDK_ROOT=$HOME/Library/Android/sdk
2828
export PATH=$PATH:$JAVA_HOME/bin:$ANDROID_HOME/emulator:$ANDROID_HOME/tools:$ANDROID_HOME/tools/bin:$ANDROID_HOME/platform-tools
2929
```
3030
3131
#### For Windows
3232
```shell
33-
set JAVA_HOME=C:\Program Files\Microsoft\jdk-17.0.8.7-hotspot
3433
set ANDROID_SDK_ROOT=C:\Users\yourname\AppData\Local\Android\Sdk
3534
set PATH=%PATH%;%JAVA_HOME%\bin;%ANDROID_SDK_ROOT%\platform-tools
35+
36+
# This isn't required if JAVA_HOME is already set in the Windows Env Variables
37+
set JAVA_HOME=C:\Program Files\Microsoft\jdk-17.0.8.7-hotspot
3638
```
3739
3840
> **Note** You cannot build iOS apps on Windows or Linux
@@ -47,13 +49,14 @@ NativePHP for mobile is built to work with Laravel. You can install it into an e
4749
[start a new one](https://laravel.com/docs/installation). The most painless way to get PHP and Node up and running on your system is with
4850
[Laravel Herd](https://herd.laravel.com). It's fast and free!
4951
50-
## Private package
52+
53+
## Install NativePHP for mobile
5154
5255
To make NativePHP for mobile a reality has taken a lot of work and will continue to require even more. For this reason,
5356
it's not open source, and you are not free to distribute or modify its source code.
5457
5558
Before you begin, you will need to purchase a license.
56-
Licenses can be obtained via [Anystack](https://checkout.anystack.sh/nativephp-ios).
59+
Licenses can be obtained via [Anystack](https://checkout.anystack.sh/nativephp).
5760
5861
Once you have your license, you will need to add the following to your `composer.json`:
5962
@@ -66,8 +69,7 @@ Once you have your license, you will need to add the following to your `composer
6669
],
6770
```
6871
69-
## Install NativePHP for mobile
70-
72+
Then run:
7173
```shell
7274
composer require nativephp/mobile
7375
```
@@ -77,7 +79,7 @@ If this is the first time you're installing the package, you will be prompted to
7779
This package contains all the libraries, classes, commands, and interfaces that your application will need to work with
7880
iOS and Android.
7981
80-
Before running the `install` command it is important to set the following variables in your `.env`:
82+
**Before** running the `install` command it is important to set the following variables in your `.env`:
8183
8284
```shell
8385
NATIVEPHP_APP_ID=com.nativephp.yourapp

resources/views/docs/mobile/1/getting-started/status.md renamed to resources/views/docs/mobile/1/getting-started/roadmap.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Presently, NativePHP for mobile offers the following "native" functionality:
2222
- Deep Links
2323
- NFC
2424

25-
We're working on adding more and more features, including:
25+
We're working on adding more and more features, including (in no particular order):
2626
- Secure Storage
2727
- Microphone access
2828
- Location

resources/views/docs/mobile/1/the-basics/app-lifecycle.md

Lines changed: 0 additions & 6 deletions
This file was deleted.

resources/views/docs/mobile/1/the-basics/deep-links.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ NativePHP for Mobile supports both **deep linking** and **web-based linking** in
1010
There are two types of link integrations you can configure:
1111

1212
- **Deep Links** (myapp://some/path)
13-
- **Universal Links (iOS)** and **App Links (Android)** (https://yourdomain.com/some/path)
13+
- **Universal Links (iOS)** and **App Links (Android)** (https://example.net/some/path)
1414

1515
Each method has its use case, and NativePHP allows you to configure and handle both easily.
1616

@@ -49,7 +49,7 @@ Universal Links and App Links allow real HTTPS URLs to open your app instead of
4949

5050
For example:
5151
```dotenv
52-
https://bifrost.tech/property/456
52+
https://example.net/property/456
5353
```
5454

5555
When a user taps this link:
@@ -72,22 +72,22 @@ NativePHP for Mobile handles all of this for you.
7272

7373
To enable Universal Links and App Links, you must define:
7474

75-
- **Host**: The domain name (e.g., bifrost-tech.com)
75+
- **Host**: The domain name (e.g., example.net)
7676

7777
These are configured in your .env:
7878

7979
```dotenv
80-
NATIVEPHP_DEEPLINK_HOST=bifrost-tech.com
80+
NATIVEPHP_DEEPLINK_HOST=example.net
8181
```
8282

8383
## Handling Universal/App Links
8484

8585
Once you've configured your deep link settings, you can handle the link in your app.
8686

87-
Simply setup a route in your web.php file and the deeplink will redirect to your route.
87+
Simply set up a route in your web.php file and the deeplink will redirect to your route.
8888

8989
```dotenv
90-
https://bifrost-tech.com/profile/123
90+
https://example.net/profile/123
9191
```
9292

9393
```php

resources/views/docs/mobile/1/the-basics/dialogs.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ NativePHP allows you to trigger many native dialogs.
1010
Dialogs are created using the `Dialog` facade.
1111

1212
```php
13-
use Native\Ios\Facades\Dialog;
13+
use Native\Mobile\Facades\Dialog;
1414
```
1515

1616
### The Share Dialog
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
---
2+
title: Native Functions
3+
order: 1
4+
---
5+
6+
Nearly any basic Laravel app will work as a mobile app with NativePHP for mobile. However, what makes NativePHP
7+
unique is that it allows you to call native functions from your PHP code.
8+
9+
These functions are called from your PHP code using one of an ever-growing list of facades.
10+
11+
Currently, there are two facades available:
12+
13+
- `Native\Mobile\Facades\System`
14+
- `Native\Mobile\Facades\Dialog`
15+
16+
## System
17+
18+
The `System` facade is used to call native functions that access system resources.
19+
20+
For example, you may use the `System::camera()` method to request access to the device's camera.
21+
22+
23+
## Synchronous vs. Asynchronous Methods
24+
25+
It is important to understand the difference between synchronous and asynchronous methods. Some methods
26+
like `flashlight` and `vibrate` are synchronous, meaning that they will block the current thread until the
27+
operation is complete.
28+
29+
Other methods like `camera` and `biometric` are asynchronous, meaning that they
30+
will return immediately and the operation will be performed in the background. When the operation is
31+
complete, the method will `broadcast an event` to your frontend via an injected javascript event as well
32+
as a traditional [Laravel Event](https://laravel.com/docs/12.x/events#main-content) that you can listen for within your app.
33+
34+
In order to receive these events, you must register a listener for the event. For example,
35+
take a look at how easy it is to listen for a `PhotoTaken` event in Livewire:
36+
37+
```php
38+
use Livewire\Attributes\On;
39+
use Livewire\Component;
40+
use Native\Mobile\Facades\System;
41+
use Native\Mobile\Events\Camera\PhotoTaken;
42+
43+
class Camera extends Component
44+
{
45+
public string $photoDataUrl = '';
46+
47+
public function camera()
48+
{
49+
System::camera();
50+
}
51+
52+
#[On('native:' . PhotoTaken::class)]
53+
public function handleCamera($path)
54+
{
55+
$data = base64_encode(file_get_contents($path));
56+
$mime = mime_content_type($path);
57+
58+
$this->photoDataUrl = "data:$mime;base64,$data";
59+
}
60+
61+
public function render()
62+
{
63+
return view('livewire.system.camera');
64+
}
65+
}
66+
```
67+
68+
All Livewire/front end events are prefixed with `native:` to avoid collisions with other events.
69+
In the following pages we will highlight the available native functions, whether they are asynchronous or not
70+
and which events they fire.

0 commit comments

Comments
 (0)