姸姸(是分開的姸!)
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Flutter - Firebase 教學 若防火牆有相關限制,可參考[官網](https://firebase.google.com/docs/cloud-messaging/concept-options?hl=zh-tw#messaging-ports-and-your-firewall)將裡面內容設為白名單。 ## Firebase 安裝教學 ### 建立屬於自己的firebase 是圖形介面應該很容易故略,[參考資料](https://support.google.com/firebase/answer/7015592?hl=zh-Hant#zippy=%2C%E6%9C%AC%E6%96%87%E5%85%A7%E5%AE%B9) ### 安裝相關套件 1. [安卓官網](https://firebase.google.com/docs/flutter/setup?platform=android) [](https://) 2. [IOS官網](https://firebase.google.com/docs/flutter/setup?platform=ios) #### 其他常用套件 1. firebase_core ``` flutter pub add firebase_core ``` 2. [firebase_messaging 官網](https://firebase.google.com/docs/cloud-messaging/flutter/client) ``` flutter pub add firebase_messaging ``` 程式範例 main.dart,在APP關閉或是退背景時依舊可以接收訊息 ```dart // app 於背景時收到推播 // flutter 3.0.0 後需添加避免release時被移除 @pragma('vm:entry-point') Future<void> _backgroundMessageHandler(RemoteMessage message) async { debugPrint('main onMessage: ${message.data}'); } //主程式 Future<void> main() async { // async 起手式 WidgetsFlutterBinding.ensureInitialized(); // firebase await Firebase.initializeApp(); if (Platform.isIOS) { await FirebaseMessaging.instance .requestPermission(sound: true, badge: true, alert: true); } runApp(const MyApp()); } class MyApp extends StatefulWidget { const MyApp({Key? key}) : super(key: key); @override State<MyApp> createState() => _MyAppState(); } class _MyAppState extends State<MyApp> { @override void initState() { //註冊背景程式 WidgetsBinding.instance.addPostFrameCallback((_) async { FirebaseMessaging.onBackgroundMessage(_backgroundMessageHandler); }); }); super.initState(); } @override Widget build(BuildContext context) { //略 } } ``` #### 補充資料 1. 此指令會協助你建立安卓及IOS相關設定檔 ``` flutterfire configure ``` ## 使用Postman通知手機 ### Firebase FCM HTTP v1 #### Url ``` https://fcm.googleapis.com/v1/projects/<項目ID>/messages:send ``` ![](https://i.imgur.com/bWXdP9g.png) #### Header ##### Authorization ``` Bearer <access_token> ``` STEP 1 進入[OAuth 2.0 Playground](https://developers.google.com/oauthplayground/)找到 Firebase Cloud Messaging API v1 後,點擊 Authorize API’s ,後允許權限。 ![](https://i.imgur.com/80VOq2R.png) STEP 2 點擊 Exchange authorization code for tokens 就會生成 Access token ![](https://i.imgur.com/shCDS1z.png) ##### Content-Type ``` application/json ``` #### Body ```json { "message": { "token": "<fcm token>", "notification": { "title": "Breaking News", "body": "New news story available." }, "data": { "story_id": "story_12345" } } } ``` #### 參考資料 https://firebase.google.com/docs/cloud-messaging/migrate-v1?hl=zh-cn https://apoorv487.medium.com/testing-fcm-push-notification-http-v1-through-oauth-2-0-playground-postman-terminal-part-2-7d7a6a0e2fa0 ### firebase 舊版 API #### Request ![](https://i.imgur.com/OD1icIJ.png) ``` https://fcm.googleapis.com/fcm/send ``` ##### Header ###### Authorization 將下圖紅匡處填入即可,若未顯示紅匡處可以先點選設定(藍色圈圈處)進行啟用 ![](https://i.imgur.com/3yonqIW.png) ##### body ```json { "to" : "請填入devices的id,可從FirebaseMessaging取得tocken", "content_available": true, "click_action": "FLUTTER_NOTIFICATION_CLICK", //flutter用 "priority": "high", "notification" : { //請自行定義資料 }, //其他節點可自行定義 } ``` 參考影片:[Youtube](https://www.youtube.com/watch?v=rQzexLu0eLU) ## 特別注意事項 <font color=''></font> ## 錯誤訊息 ### File google-services.json is missing. IDE錯誤訊息 ```config Execution failed for task ':app:processDebugGoogleServices'. > File google-services.json is missing. The Google Services Plugin cannot function without it. ``` #### 解決方法 1. 登入 Firebase,然後開啟專案。 2. 點選設定圖示,然後選取 [專案設定]。 3. 在「您的應用程式」卡片中,從清單中選取您要下載設定檔的應用程式的繫結 ID。 4. 點選下載。 ### Notifications to this FCM Token will not be delivered over APNS. ```config APNS device token not set before retrieving FCM Token for Sender ID '899895801464'. Notifications to this FCM Token will not be delivered over APNS.Be sure to re-retrieve the FCM token once the APNS device token is set. ``` #### 解決辦法 1. 加入APN金鑰(專案設定 >> 雲端通訊) ![](https://i.imgur.com/YFGLqSy.png) 2. 確定google-services.json放置位置正確,且Resourse內有該檔案,沒有的話重新拉到該資料夾 ![](https://i.imgur.com/56rlmkH.png) 3. 確定有添加Push Notifications,[參考資料](https://firebase.flutter.dev/docs/messaging/apple-integration)(圖片源自官網) ![](https://i.imgur.com/F0nOdo8.png) ### flutterfire: command not found ``` dart pub global activate flutterfire_cli export PATH="$PATH":"$HOME/.pub-cache/bin" ``` 就可以運行 ``` flutterfire configure ``` ## 補充資料 ### 取得applicationId #### 安卓 /android/app/build.gradle ```c defaultConfig { applicationId "com.example.catcatcat" //下面略 } ``` #### IOS 開啟Xcode查看,Runner > Signing&Capabilities > Bundle Identifier ![](https://i.imgur.com/nidTVm6.png) ###### tags: `flutter` `firebase`

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully