-
-
Notifications
You must be signed in to change notification settings - Fork 80
V1 docs #111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
V1 docs #111
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
cf223d5
First pass at docs
shanerbaner82 902415a
Merge branch 'main' into v1-docs
shanerbaner82 cda319e
Update v1 alert
shanerbaner82 d595a8b
cleanup and nfc/deeplink docs
shanerbaner82 b820ceb
remove coming soon items
shanerbaner82 543cf67
updates docs for v1
shanerbaner82 44747bc
Merge branch 'main' into v1-docs
shanerbaner82 d9b550b
Updates
shanerbaner82 1c0518a
Cleanup
simonhamp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
resources/views/docs/mobile/1/digging-deeper/push-notifications.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
--- | ||
title: Push Notifications | ||
order: 400 | ||
--- | ||
|
||
|
||
## Overview | ||
|
||
NativePHP for Mobile uses [Firebase Cloud Messaging](https://firebase.google.com/docs/cloud-messaging) to send push notifications to your users. | ||
|
||
## Setting up your app | ||
|
||
Once you create a Firebase account and create a project you will be offered to download a `google-services.json` file. | ||
|
||
This file contains the configuration for your app and is used by the Firebase SDK to retrieve tokens for each device using your app. | ||
|
||
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. | ||
|
||
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. | ||
|
||
## Receiving Push Tokens | ||
|
||
To receive push notifications, you must register a listener for the event. For example, | ||
take a look at how easy it is to listen for a `TokenGenerated` event in Livewire: | ||
|
||
```php | ||
use Livewire\Attributes\On; | ||
use Livewire\Component; | ||
use Native\Mobile\Facades\System; | ||
use Native\Mobile\Events\PushNotification\TokenGenerated; | ||
|
||
class PushNotifications extends Component | ||
{ | ||
public function render() | ||
{ | ||
return view('livewire.system.push-notifications'); | ||
} | ||
|
||
#[On('native:' . TokenGenerated::class)] | ||
public function handlePushNotifications(string $token) | ||
{ | ||
// Do something with the token... | ||
} | ||
} | ||
``` | ||
|
||
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 | ||
`POST` the token to your server, on the server side you need to associate the token with the "user" that owns the device. | ||
|
||
We **strongly** recommend using [Sanctum](https://laravel.com/docs/12.x/sanctum#main-content) to handle this for you. | ||
|
||
## The flow | ||
|
||
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. | ||
|
||
The token is stored in your apps `session` and is used on subsequent requests to the api server. | ||
|
||
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. | ||
|
||
> Optionally, you can have a `HasMany` relationship between your users and devices, | ||
> this allows you to associate a device with a user and then use the device's token | ||
> to send push notifications to that users devices. | ||
|
||
## Sending Push Notifications | ||
|
||
Once you have the token, you may use it from your server-based applications to trigger Push Notifications directly to | ||
your user's device. We use a package like [google/apiclient](https://github.com/googleapis/google-api-php-client) to send the notifications. | ||
|
||
This is the exact code used by the NativePHP Kitchen Sink App (available soon on all app stores and GitHub): | ||
|
||
```php | ||
Route::group(['middleware' => 'auth:sanctum'], function () { | ||
Route::post('send-push-notification', PushNotificationController::class)->name('send-push-notification'); | ||
}); | ||
``` | ||
|
||
```php | ||
namespace App\Http\Controllers; | ||
|
||
use App\Jobs\SendPushNotification; | ||
use Illuminate\Http\Request; | ||
|
||
class PushNotificationController extends Controller | ||
{ | ||
public function __invoke(Request $request) | ||
{ | ||
$token = $request->get('token'); | ||
|
||
$request->user()->update([ | ||
'push_token' => $token | ||
]); | ||
|
||
SendPushNotification::dispatch($token)->delay(now()->addMinutes(1)); | ||
} | ||
} | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe it's the same for Mac, so likely no need to reference Windows specifically. :)