<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Prathvik Sankaliya  on Medium]]></title>
        <description><![CDATA[Stories by Prathvik Sankaliya  on Medium]]></description>
        <link>https://medium.com/@prathvik.sankaliya.2024?source=rss-014e8b835b85------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*nXWz4prZJQ4BOJut</url>
            <title>Stories by Prathvik Sankaliya  on Medium</title>
            <link>https://medium.com/@prathvik.sankaliya.2024?source=rss-014e8b835b85------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 27 Nov 2026 12:52:57 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@prathvik.sankaliya.2024/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Firebase Push Notification: Migrate from Legacy FCM APIs to HTTP v1 | Flutter]]></title>
            <link>https://medium.com/@prathvik.sankaliya.2024/firebase-push-notification-migrate-from-legacy-fcm-apis-to-http-v1-flutter-9fa1e187ab0a?source=rss-014e8b835b85------2</link>
            <guid isPermaLink="false">https://medium.com/p/9fa1e187ab0a</guid>
            <category><![CDATA[dart]]></category>
            <category><![CDATA[firebase]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[firebase-messaging]]></category>
            <category><![CDATA[push-notification]]></category>
            <dc:creator><![CDATA[Prathvik Sankaliya ]]></dc:creator>
            <pubDate>Wed, 10 Jul 2024 09:22:41 GMT</pubDate>
            <atom:updated>2024-07-10T09:22:41.653Z</atom:updated>
            <content:encoded><![CDATA[<h3>Flutter Push Notifications: Upgrade Legacy FCM APIs to HTTP v1 | Firebase Messaging</h3><h3>Why Migrate to HTTP v1?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lXoJM09svXvBJH3GstrmNQ.png" /><figcaption>Deprecated Cloud Messaging API (Legacy)</figcaption></figure><p><strong>Enhanced Security:</strong> HTTP v1 uses OAuth 2.0 for authentication, offering better security compared to legacy API keys. OAuth 2.0 tokens are short-lived and scoped, reducing the risk of unauthorized access.</p><p><strong>Improved Features:</strong> HTTP v1 provides advanced targeting options, richer message payloads, batch messaging, and better error handling. It also integrates with Firebase Analytics for detailed message delivery insights.</p><p><strong>Long-term Support:</strong> Legacy APIs may eventually be deprecated. Migrating to HTTP v1 ensures continued support and access to new features, future-proofing your application and aligning it with modern web standards.</p><h3>Migration Process</h3><blockquote><strong><em>Step 1: Setting Up Google Cloud Project Enable FCM API:</em></strong><br><strong>• </strong>Go to the Google Cloud Console and select your project.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_01xVa9WA5Xi9b_i5a3o1w.png" /></figure><blockquote><strong>Create Service Account:</strong><br>In the Google Cloud Console, go to the “IAM &amp; Admin” section and create a new service account.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZOnS6hb2ySZVHllL6Dht2w.png" /></figure><blockquote>Assign the necessary roles (e.g., owner) and continue or done to the service account.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1008/1*Z_kbvkRxCOczpE0Kwcc14A.png" /></figure><blockquote>Open your recently generated service and create key select JSON or Download the service account key file (JSON) for use in your application.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6jqR_n1YPYE-en0viyEJRA.png" /></figure><blockquote><strong><em>Step 2: Adding Notification Service in Flutter</em></strong></blockquote><ol><li><strong>Add Dependencies:</strong></li></ol><ul><li>Open your pubspec.yaml file and add the necessary dependencies for Firebase and FCM.</li></ul><pre>dependencies:<br>  flutter:<br>    sdk: flutter<br>  firebase_core: latest_version<br>  firebase_messaging: latest_version<br>  http: latest_version<br>  googleapis_auth: latest_version</pre><p><strong>2. Create PushNotificationService Class:</strong></p><ul><li>Create a new Dart file (e.g., push_notification_service.dart)</li></ul><pre>import &#39;dart:convert&#39;;<br>import &#39;package:http/http.dart&#39; as http;<br>import &#39;package:firebase_messaging/firebase_messaging.dart&#39;;<br>import &#39;package:googleapis_auth/auth_io.dart&#39; as auth;<br><br>class PushNotificationService{<br><br>final FirebaseMessaging _firebaseMessaging = FirebaseMessaging.instance;<br><br>  Future&lt;void&gt; initialize() async {<br>    // Request permissions<br>    await _firebaseMessaging.requestPermission();<br><br>    // Get the device token<br>    String? token = await _firebaseMessaging.getToken();<br>    print(&quot;Device Token: $token&quot;);<br>  }<br><br>  static Future&lt;String&gt; getServerAccessToken() async {<br>    final serviceAccountJson = {<br>    // Copy and Paste json file content<br><br>    };<br><br>    List&lt;String&gt; scopes = [<br>      &quot;https://www.googleapis.com/auth/firebase.messaging&quot;,<br>      &quot;https://www.googleapis.com/auth/firebase.database&quot;,<br>    ];<br><br>    http.Client client = await auth.clientViaServiceAccount(<br>      auth.ServiceAccountCredentials.fromJson(serviceAccountJson),<br>      scopes,<br>    );<br><br>    // Get Access Token<br>    auth.AccessCredentials credentials =<br>        await auth.obtainAccessCredentialsViaServiceAccount(auth.ServiceAccountCredentials.fromJson(serviceAccountJson), scopes, client);<br>    client.close();<br>    print(&quot;log server access token: ${credentials.accessToken.data}&quot;);<br>    return credentials.accessToken.data;<br>  }<br><br>  static sendNotification({<br>    required String deviceToken,<br>    required String message,<br>  }) async {<br><br>    final String serverToken = await getServerAccessToken();<br><br>    // You will get Project Id from google-services.json<br>    String endpointFCM = &quot;https://fcm.googleapis.com/v1/projects/&lt;Project Id&gt;/messages:send&quot;;<br>    final Map&lt;String, dynamic&gt; bodyMessage = {<br>      &#39;message&#39;: {<br>        &#39;token&#39;: deviceToken,<br>        &#39;notification&#39;: {<br>          &#39;title&#39;: &#39;App Name&#39;,<br>          &#39;body&#39;: message,<br>        },<br>        // Data is optional if you want to add any payload into notification.<br>        &#39;data&#39;: {}<br>      }<br>    };<br><br>    final http.Response response = await http.post(Uri.parse(endpointFCM),<br>        headers: &lt;String, String&gt;{<br>          &#39;Content-Type&#39;: &#39;application/json&#39;,<br>          &#39;Authorization&#39;: &#39;Bearer $serverToken&#39;,<br>        },<br>        body: jsonEncode(bodyMessage));<br><br>    print(response.statusCode);<br>    if (response.statusCode == 200) {<br>      print(&quot;Sent Notification successfully&quot;);<br>    } else {<br>      print(&quot;Failed Notification&quot;);<br>    }<br>  }<br>}</pre><p><strong>3. Integrate the Notification Service:</strong></p><ul><li>Use the PushNotificationService in your app to send notifications.</li></ul><pre>import &#39;package:flutter/material.dart&#39;;<br>import &#39;package:your_app/push_notification_service.dart&#39;;<br><br>class MyHomePage extends StatefulWidget {<br>  @override<br>  _MyHomePageState createState() =&gt; _MyHomePageState();<br>}<br><br>class _MyHomePageState extends State&lt;MyHomePage&gt; {<br>  final PushNotificationService _pushNotificationService = PushNotificationService();<br><br>  @override<br>  void initState() {<br>    super.initState();<br>    _pushNotificationService.initialize();<br>  }<br><br>  void _sendNotification() {<br>    _pushNotificationService.sendNotification(<br>      deviceToken: &#39;your-device-token&#39;,<br>      message: &#39;Hello World&#39;<br>    );<br>  }<br><br>  @override<br>  Widget build(BuildContext context) {<br>    return Scaffold(<br>      appBar: AppBar(<br>        title: Text(&#39;FCM Demo&#39;),<br>      ),<br>      body: Center(<br>        child: ElevatedButton(<br>          onPressed: _sendNotification,<br>          child: Text(&#39;Send Notification&#39;),<br>        ),<br>      ),<br>    );<br>  }<br>}</pre><h3>Conclusion</h3><p>Migrating to Firebase Cloud Messaging (FCM) HTTP v1 enhances your app’s security, features, and future-proofing. The migration involves setting up your Google Cloud project, generating OAuth 2.0 tokens, and updating your server code. Integrating the notification service into your Flutter app ensures you leverage these benefits. By migrating now, you ensure your app remains secure and efficient, aligned with modern standards, and ready for future updates from Firebase.</p><p><strong>Additional Links:</strong></p><ul><li><a href="https://firebase.google.com/docs/cloud-messaging/migrate-v1#node.js_1"><strong>Firebase Cloud Messaging Documentation (Backend</strong></a><strong>)</strong></li><li><a href="https://console.cloud.google.com/projectselector2/iam-admin/serviceaccounts?supportedpurview=project"><strong>Google Cloud Console</strong></a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9fa1e187ab0a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>