Web Development Framework7 এর Notifications এবং Push Notifications গাইড ও নোট

346

Framework7 মোবাইল এবং ওয়েব অ্যাপ্লিকেশনগুলিতে নোটিফিকেশন (Notifications) এবং পুশ নোটিফিকেশন (Push Notifications) ব্যবহারের জন্য শক্তিশালী সমর্থন প্রদান করে। এই ফিচারগুলি ব্যবহারকারীদের সাথে ইন্টারঅ্যাকশন বাড়াতে এবং গুরুত্বপূর্ণ তথ্য দ্রুত পৌঁছাতে সহায়ক। নিচে Framework7 এর Notifications এবং Push Notifications সম্পর্কে বিস্তারিত আলোচনা করা হলো।


১. Notifications (নোটিফিকেশন)

Notifications কী?

Notifications হল ছোট পপআপ মেসেজ যা ব্যবহারকারীকে কোনো ইভেন্ট, আপডেট বা গুরুত্বপূর্ণ তথ্য সম্পর্কে অবহিত করে। Framework7 এর Notifications API ব্যবহার করে সহজেই বিভিন্ন ধরনের নোটিফিকেশন তৈরি এবং প্রদর্শন করা যায়।

নোটিফিকেশন তৈরি করা

Framework7 এর Notifications API ব্যবহার করে বিভিন্ন ধরণের নোটিফিকেশন তৈরি করা সম্ভব, যেমন:

  • Alert Notification: সাধারণ এলার্ট মেসেজ
  • Confirm Notification: কনফার্মেশন ডায়ালগ
  • Prompt Notification: ইউজার ইনপুট নিতে
  • Toast Notification: স্ক্রিনের নিচে ছোট মেসেজ
Alert Notification উদাহরণ
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Framework7 Alert Notification</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/framework7/framework7-bundle.min.css">
</head>
<body>
  <div id="app">
    <div class="view view-main">
      <div class="page">
        <div class="page-content">
          <button class="button button-fill" id="alertBtn">Show Alert</button>
        </div>
      </div>
    </div>
  </div>

  <script src="https://cdn.jsdelivr.net/npm/framework7/framework7-bundle.min.js"></script>
  <script>
    var app = new Framework7();

    // Alert Button Click ইভেন্ট
    document.getElementById('alertBtn').addEventListener('click', function () {
      app.dialog.alert('এইটা একটি এলার্ট নোটিফিকেশন!', function () {
        console.log('Alert বন্ধ হয়েছে');
      });
    });
  </script>
</body>
</html>
Toast Notification উদাহরণ
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Framework7 Toast Notification</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/framework7/framework7-bundle.min.css">
</head>
<body>
  <div id="app">
    <div class="view view-main">
      <div class="page">
        <div class="page-content">
          <button class="button button-fill" id="toastBtn">Show Toast</button>
        </div>
      </div>
    </div>
  </div>

  <script src="https://cdn.jsdelivr.net/npm/framework7/framework7-bundle.min.js"></script>
  <script>
    var app = new Framework7();

    // Toast Button Click ইভেন্ট
    document.getElementById('toastBtn').addEventListener('click', function () {
      app.toast.create({
        text: 'এইটা একটি টোস্ট নোটিফিকেশন!',
        position: 'bottom',
        closeTimeout: 3000,
      }).open();
    });
  </script>
</body>
</html>

নোটিফিকেশন কাস্টমাইজেশন

Framework7 এর Notifications API বিভিন্ন প্রপার্টি ব্যবহার করে নোটিফিকেশন কাস্টমাইজ করার সুযোগ দেয়, যেমন:

  • title: নোটিফিকেশনের শিরোনাম
  • text: মেসেজ কনটেন্ট
  • button: বোতাম যুক্ত করা
  • position: নোটিফিকেশনের অবস্থান (top, bottom, center)
  • closeTimeout: নির্দিষ্ট সময় পর স্বয়ংক্রিয়ভাবে বন্ধ হওয়া
কাস্টম Alert উদাহরণ
app.dialog.alert('এইটা একটি কাস্টম এলার্ট!', 'নোটিফিকেশন শিরোনাম', function () {
  console.log('কাস্টম এলার্ট বন্ধ হয়েছে');
});

২. Push Notifications (পুশ নোটিফিকেশন)

Push Notifications কী?

Push Notifications হল সার্ভার থেকে সরাসরি ব্যবহারকারীর ডিভাইসে পাঠানো নোটিফিকেশন, যা ব্যবহারকারী অ্যাপটি ব্যবহার না করলেও দেখতে পারে। এগুলো বিশেষ করে গুরুত্বপূর্ণ আপডেট, নতুন বার্তা বা বিজ্ঞপ্তি পাঠাতে ব্যবহৃত হয়।

Push Notifications সেটআপ করা

Push Notifications সেটআপ করতে কিছু অতিরিক্ত ধাপ অনুসরণ করতে হয়, যা সাধারণত Cordova বা Capacitor ব্যবহার করে মোবাইল অ্যাপ্লিকেশনের সাথে ইন্টিগ্রেট করা হয়। নিচে Firebase Cloud Messaging (FCM) ব্যবহার করে Push Notifications সেটআপ করার প্রক্রিয়া বর্ণনা করা হলো।

ধাপ ১: Firebase প্রোজেক্ট তৈরি করা
  1. Firebase Console এ যান।
  2. নতুন প্রোজেক্ট তৈরি করুন।
  3. আপনার অ্যাপ (Android/iOS/Web) যুক্ত করুন এবং প্রয়োজনীয় কনফিগারেশন ফাইল ডাউনলোড করুন (google-services.json বা GoogleService-Info.plist).
ধাপ ২: Cordova/Capacitor ইনস্টল করা

Framework7 মোবাইল অ্যাপ তৈরি করতে Cordova বা Capacitor ব্যবহার করে থাকে। এখানে Cordova এর উদাহরণ দেওয়া হলো:

npm install -g cordova
ধাপ ৩: Push Notifications প্লাগইন ইনস্টল করা

Cordova এর জন্য phonegap-plugin-push প্লাগইন ব্যবহার করা যেতে পারে।

cordova plugin add phonegap-plugin-push
ধাপ ৪: Firebase সার্ভিস কনফিগার করা

Firebase Console থেকে সার্ভিস কনফিগারেশন ফাইল ডাউনলোড করে আপনার অ্যাপের রুট ডিরেক্টরিতে যুক্ত করুন।

ধাপ ৫: JavaScript কোডে Push Notifications হ্যান্ডল করা
document.addEventListener('deviceready', function () {
  var push = PushNotification.init({
    android: {
      senderID: "YOUR_FIREBASE_SENDER_ID"
    },
    ios: {
      alert: "true",
      badge: "true",
      sound: "true"
    },
    windows: {}
  });

  push.on('registration', function(data) {
    console.log("Device registered with ID:", data.registrationId);
    // সার্ভারে রেজিস্ট্রেশন আইডি পাঠান
  });

  push.on('notification', function(data) {
    console.log("Notification received:", data);
    // নোটিফিকেশন প্রদর্শন করুন
    app.notification.create({
      title: data.title,
      text: data.message,
      closeButton: true,
    }).open();
  });

  push.on('error', function(e) {
    console.error("Push plugin error:", e.message);
  });
}, false);
ধাপ ৬: সার্ভার সাইড Push Notifications পাঠানো

Firebase Admin SDK ব্যবহার করে সার্ভার থেকে Push Notifications পাঠানো যায়। নিচে Node.js উদাহরণ দেওয়া হলো:

const admin = require('firebase-admin');
const serviceAccount = require('./path/to/serviceAccountKey.json');

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount)
});

const message = {
  notification: {
    title: 'নতুন আপডেট!',
    body: 'আপনার অ্যাপে নতুন ফিচার যুক্ত হয়েছে।'
  },
  token: 'USER_DEVICE_TOKEN'
};

admin.messaging().send(message)
  .then((response) => {
    console.log('Successfully sent message:', response);
  })
  .catch((error) => {
    console.error('Error sending message:', error);
  });

Push Notifications এর নিরাপত্তা ও বেস্ট প্র্যাকটিস

  • ব্যবহারকারীর সম্মতি নিন: Push Notifications পাঠানোর আগে ব্যবহারকারীর সম্মতি নেওয়া উচিত।
  • নিরাপদ সার্ভার সাইড ইন্টিগ্রেশন: সার্ভার সাইড থেকে Push Notifications পাঠানোর সময় নিরাপদ API ব্যবহার করুন।
  • ফিল্টারিং ও টার্গেটিং: প্রাসঙ্গিক ব্যবহারকারীদের লক্ষ্য করে নোটিফিকেশন পাঠান।
  • অ্যানিমেশন ও কাস্টমাইজেশন: নোটিফিকেশনের স্টাইল ও ইফেক্ট কাস্টমাইজ করুন যাতে তা ব্যবহারকারীর অভিজ্ঞতা উন্নত করে।

Push Notifications এর জন্য Framework7 এর উদাহরণ

নিচে একটি সম্পূর্ণ উদাহরণ দেওয়া হলো যা Cordova এবং Push Notifications প্লাগইন ব্যবহার করে Framework7 অ্যাপে Push Notifications সেটআপ করে:

// index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Framework7 Push Notification Example</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/framework7/framework7-bundle.min.css">
</head>
<body>
  <div id="app">
    <div class="view view-main">
      <div class="page">
        <div class="navbar">
          <div class="navbar-inner">
            <div class="title">Push Notification</div>
          </div>
        </div>
        <div class="page-content">
          <button class="button button-fill" id="notifyBtn">Send Notification</button>
        </div>
      </div>
    </div>
  </div>

  <script src="cordova.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/framework7/framework7-bundle.min.js"></script>
  <script>
    var app = new Framework7();

    document.addEventListener('deviceready', function () {
      var push = PushNotification.init({
        android: {
          senderID: "YOUR_FIREBASE_SENDER_ID"
        },
        ios: {
          alert: "true",
          badge: "true",
          sound: "true"
        },
        windows: {}
      });

      push.on('registration', function(data) {
        console.log("Device registered with ID:", data.registrationId);
        // এখানে আপনার সার্ভারে data.registrationId পাঠান
      });

      push.on('notification', function(data) {
        console.log("Notification received:", data);
        app.notification.create({
          title: data.title,
          text: data.message,
          closeButton: true,
        }).open();
        push.finish(function() {
          console.log('Processing of push data is finished');
        });
      });

      push.on('error', function(e) {
        console.error("Push plugin error:", e.message);
      });

      // নোটিফিকেশন পাঠানোর বোতাম
      document.getElementById('notifyBtn').addEventListener('click', function () {
        // এই উদাহরণে আমরা ক্লায়েন্ট সাইড থেকে নোটিফিকেশন ট্রিগার করছি না।
        // সার্ভার সাইড থেকে নোটিফিকেশন পাঠাতে হবে।
        alert('Push Notifications সার্ভার সাইড থেকে পাঠাতে হবে।');
      });
    }, false);
  </script>
</body>
</html>

নোট: Push Notifications সম্পূর্ণরূপে কার্যকর করার জন্য সার্ভার সাইড ইন্টিগ্রেশন এবং Firebase কনফিগারেশন প্রয়োজন। উপরোক্ত উদাহরণটি শুধুমাত্র ক্লায়েন্ট সাইড কোড দেখাচ্ছে, সার্ভার সাইড কোড সংযোজন করতে হবে Push Notifications পাঠানোর জন্য।


সারাংশ

Framework7 এর Notifications এবং Push Notifications ফিচারগুলি ব্যবহার করে আপনি আপনার অ্যাপ্লিকেশনকে আরও ইন্টারঅ্যাকটিভ, ব্যবহার-বান্ধব এবং তথ্যপূর্ণ করতে পারেন। Notifications API ব্যবহার করে সহজেই নোটিফিকেশন তৈরি এবং প্রদর্শন করা যায়, আর Push Notifications সেটআপ করে সার্ভার থেকে সরাসরি ব্যবহারকারীদের ডিভাইসে তথ্য পৌঁছানো সম্ভব। এই ফিচারগুলি সঠিকভাবে ইন্টিগ্রেট এবং কাস্টমাইজ করে আপনার অ্যাপের ইউজার এক্সপেরিয়েন্স উল্লেখযোগ্যভাবে উন্নত করা যায়।


অতিরিক্ত রিসোর্স

এই রিসোর্সগুলো আরও বিস্তারিত তথ্য এবং উদাহরণ কোড সরবরাহ করে, যা আপনাকে Push Notifications এবং Notifications ফিচারগুলি আরও গভীরভাবে বুঝতে এবং ইন্টিগ্রেট করতে সাহায্য করবে।

Content added By

Local Notifications তৈরি করা

247

Framework7 এর মাধ্যমে মোবাইল অ্যাপ্লিকেশনে লোকাল নোটিফিকেশন (Local Notifications) যোগ করা যায়, যা ব্যবহারকারীদের ডিভাইসে নির্দিষ্ট সময়ে বা ইভেন্টের ভিত্তিতে নোটিফিকেশন পাঠাতে সাহায্য করে। লোকাল নোটিফিকেশনগুলি সার্ভার সাইডের কোন সহায়তা ছাড়াই অ্যাপ্লিকেশন দ্বারা সরাসরি পরিচালিত হয়।

লোকাল নোটিফিকেশন কেন ব্যবহার করবেন?

  • ইউজার এনগেজমেন্ট: নির্দিষ্ট সময়ে বা ইভেন্টের ভিত্তিতে ব্যবহারকারীদের তথ্য বা রিমাইন্ডার পাঠানো।
  • ইন্টারঅ্যাকটিভিটি: অ্যাপ্লিকেশনের সাথে ব্যবহারকারীদের নিয়মিত যোগাযোগ বজায় রাখা।
  • নোটিফিকেশন পারফরম্যান্স: সার্ভার সাইড নোটিফিকেশন ছাড়াই দ্রুত এবং কার্যকর নোটিফিকেশন পরিচালনা করা।

প্রয়োজনীয় টুলস এবং প্লাগইন

লোকাল নোটিফিকেশন বাস্তবায়নের জন্য Cordova বা Capacitor প্ল্যাটফর্মের সাথে Local Notifications প্লাগইন ব্যবহার করা হয়। এখানে আমরা Cordova প্ল্যাটফর্ম এবং Cordova Local Notifications প্লাগইন ব্যবহার করে কীভাবে লোকাল নোটিফিকেশন তৈরি করা যায় তা আলোচনা করব।

ধাপ ১: প্রজেক্টে প্লাগইন ইনস্টল করা

প্রথমে, আপনাকে আপনার Framework7 প্রজেক্টে Cordova Local Notifications প্লাগইন ইনস্টল করতে হবে।

Cordova Local Notifications প্লাগইন ইনস্টল:

cordova plugin add cordova-plugin-local-notifications
npm install @awesome-cordova-plugins/local-notifications

ধাপ ২: প্লাগইন কনফিগার করা

Framework7 প্রজেক্টে প্লাগইন ব্যবহার করার জন্য আপনাকে JavaScript ফাইলে প্লাগইন ইমপোর্ট এবং কনফিগার করতে হবে।

উদাহরণ: app.js ফাইলে প্লাগইন ইমপোর্ট এবং কনফিগার

import Framework7 from 'framework7/lite-bundle';
import { LocalNotifications } from '@awesome-cordova-plugins/local-notifications';

var app = new Framework7({
  root: '#app',
  // অন্যান্য কনফিগারেশন
});

// Cordova প্লাগইন লোড করার জন্য ডিভাইস প্রস্তুত হওয়ার ইভেন্টে কোড যোগ করুন
document.addEventListener('deviceready', function () {
  // প্লাগইন প্রস্তুত
  console.log('Device is ready');

  // লোকাল নোটিফিকেশন শিডিউল করা
  LocalNotifications.schedule({
    id: 1,
    title: 'হ্যালো!',
    text: 'এটি একটি লোকাল নোটিফিকেশন উদাহরণ।',
    trigger: { at: new Date(new Date().getTime() + 5 * 1000) }, // ৫ সেকেন্ড পর নোটিফিকেশন দেখাবে
    foreground: true
  }).then(() => {
    console.log('Notification scheduled');
  });
}, false);

ধাপ ৩: ফর্মে নোটিফিকেশন শিডিউল করা

একটি বাটন ক্লিক করে নোটিফিকেশন শিডিউল করার উদাহরণ নিচে দেওয়া হলো।

উদাহরণ: HTML এবং JavaScript

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Framework7 Local Notifications Example</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/framework7/framework7-bundle.min.css">
</head>
<body>
  <div id="app">
    <div class="view view-main">
      <div class="page">
        <div class="navbar">
          <div class="navbar-inner">
            <div class="title">Local Notifications</div>
          </div>
        </div>
        <div class="page-content">
          <div class="block">
            <button id="schedule-btn" class="button button-fill">নোটিফিকেশন শিডিউল করুন</button>
          </div>
        </div>
      </div>
    </div>
  </div>
  
  <script src="https://cdn.jsdelivr.net/npm/framework7/framework7-bundle.min.js"></script>
  <script src="cordova.js"></script> <!-- Cordova স্ক্রিপ্ট -->
  <script>
    var app = new Framework7();

    document.addEventListener('deviceready', function () {
      var scheduleBtn = document.getElementById('schedule-btn');

      scheduleBtn.addEventListener('click', function () {
        LocalNotifications.schedule({
          id: 2,
          title: 'স্মরণ করিয়ে দিন!',
          text: 'এটি আপনার নির্ধারিত নোটিফিকেশন।',
          trigger: { at: new Date(new Date().getTime() + 10 * 1000) }, // ১০ সেকেন্ড পর নোটিফিকেশন দেখাবে
          foreground: true
        }).then(() => {
          app.dialog.alert('নোটিফিকেশন শিডিউল করা হয়েছে!');
        }).catch((error) => {
          app.dialog.alert('নোটিফিকেশন শিডিউল করতে ব্যর্থ হয়েছে: ' + error);
        });
      });
    }, false);
  </script>
</body>
</html>

ধাপ ৪: প্রজেক্ট বিল্ড এবং টেস্ট করা

লোকাল নোটিফিকেশনগুলি মোবাইল ডিভাইসে কাজ করে, তাই আপনাকে প্রকৃত ডিভাইসে অ্যাপ্লিকেশনটি বিল্ড এবং টেস্ট করতে হবে।

Android এর জন্য বিল্ড:

cordova build android

iOS এর জন্য বিল্ড:

cordova build ios

প্রজেক্টটি সফলভাবে বিল্ড হলে, আপনি আপনার মোবাইল ডিভাইসে অ্যাপটি ইনস্টল করে নোটিফিকেশনগুলি পরীক্ষা করতে পারেন।

অতিরিক্ত বিবেচনা

  • পারমিশন: মোবাইল ডিভাইসে লোকাল নোটিফিকেশন ব্যবহারের আগে ব্যবহারকারীর কাছ থেকে অনুমতি নেওয়া আবশ্যক। Cordova Local Notifications প্লাগইন স্বয়ংক্রিয়ভাবে পারমিশন অনুরোধ করে, তবে নিশ্চিত হন যে এটি সঠিকভাবে কাজ করছে।
  • নোটিফিকেশন আইডি: প্রতিটি নোটিফিকেশনের জন্য একটি ইউনিক আইডি ব্যবহার করুন যাতে একই নোটিফিকেশন পুনরায় শিডিউল বা আপডেট করা যায়।
  • ব্যাকগ্রাউন্ডে নোটিফিকেশন: কিছু ডিভাইসে অ্যাপ ব্যাকগ্রাউন্ডে থাকলেও নোটিফিকেশন শিডিউল করা যায়। তবে ডিভাইসের পলিসি এবং OS-এর উপর নির্ভর করে এটি বিভিন্নভাবে কাজ করতে পারে।

সারাংশ

Framework7 এর সাথে লোকাল নোটিফিকেশন যোগ করা একটি সহজ এবং কার্যকর প্রক্রিয়া। Cordova বা Capacitor প্ল্যাটফর্মের মাধ্যমে প্লাগইন ইনস্টল এবং কনফিগার করে, আপনি সহজেই আপনার মোবাইল অ্যাপ্লিকেশনে নোটিফিকেশন ফিচার যুক্ত করতে পারেন। এটি ইউজারদের সাথে অ্যাপ্লিকেশনের ইন্টারঅ্যাকশন এবং এনগেজমেন্ট বাড়াতে সাহায্য করে।

সম্পদ এবং রেফারেন্স

Content added By

Push Notifications সেটআপ এবং কনফিগার করা

312

Push Notifications (পুশ নোটিফিকেশন) ব্যবহারকারীদের রিয়েল-টাইমে তথ্য প্রদান করতে এবং অ্যাপ্লিকেশনের ইঙ্গেজমেন্ট বৃদ্ধি করতে সাহায্য করে। Framework7-এর মাধ্যমে Push Notifications সেটআপ এবং কনফিগার করা বেশ সহজ, তবে এটি নির্ভর করে আপনি কোন প্ল্যাটফর্মে অ্যাপটি ডেভেলপ করছেন তার উপর—যেমন PWA/Web অ্যাপ, Cordova মোবাইল অ্যাপ, অথবা Capacitor মোবাইল অ্যাপ।

এই গাইডে আমরা দুটি প্রধান পদ্ধতি আলোচনা করব:

  1. PWA/Web অ্যাপের জন্য Push Notifications: Firebase Cloud Messaging (FCM) ব্যবহার করে।
  2. মোবাইল অ্যাপের জন্য Push Notifications: Cordova বা Capacitor প্লাগইন ব্যবহার করে।

১. PWA/Web অ্যাপের জন্য Push Notifications সেটআপ

Firebase Cloud Messaging (FCM) হলো Google-এর একটি ফ্রি সার্ভিস যা Push Notifications পাঠাতে এবং গ্রহণ করতে ব্যবহার করা হয়। এখানে Framework7 PWA/Web অ্যাপে FCM সেটআপ করার ধাপগুলো আলোচনা করা হলো।

ধাপ ১: Firebase প্রজেক্ট তৈরি করা

  1. Firebase Console এ যান এবং একটি নতুন প্রজেক্ট তৈরি করুন।
  2. প্রজেক্টের নাম দিন এবং প্রয়োজনীয় সেটিংস নির্বাচন করুন।
  3. প্রজেক্ট তৈরি হওয়ার পর, প্রোজেক্ট ড্যাশবোর্ডে যান।

ধাপ ২: Firebase Cloud Messaging সক্রিয় করা

  1. Firebase Console-এ গিয়ে Cloud Messaging সেকশনটি নির্বাচন করুন।
  2. এখানে আপনি Server key এবং Sender ID পাবেন, যা পরবর্তীতে প্রয়োজন হবে।

ধাপ ৩: সার্ভিস ওয়ার্কার (Service Worker) তৈরি করা

PWA/Web অ্যাপের জন্য Push Notifications কার্যকর করতে একটি সার্ভিস ওয়ার্কার প্রয়োজন। নিচে একটি সাধারণ সার্ভিস ওয়ার্কার উদাহরণ দেওয়া হলো:

service-worker.js

importScripts('https://www.gstatic.com/firebasejs/9.6.10/firebase-app-compat.js');
importScripts('https://www.gstatic.com/firebasejs/9.6.10/firebase-messaging-compat.js');

const firebaseConfig = {
  apiKey: "YOUR_API_KEY",
  authDomain: "YOUR_PROJECT_ID.firebaseapp.com",
  projectId: "YOUR_PROJECT_ID",
  storageBucket: "YOUR_PROJECT_ID.appspot.com",
  messagingSenderId: "YOUR_SENDER_ID",
  appId: "YOUR_APP_ID",
};

firebase.initializeApp(firebaseConfig);
const messaging = firebase.messaging();

// Handle background messages
messaging.onBackgroundMessage(function(payload) {
  console.log('[service-worker.js] Received background message ', payload);
  const notificationTitle = payload.notification.title;
  const notificationOptions = {
    body: payload.notification.body,
    icon: '/firebase-logo.png'
  };

  self.registration.showNotification(notificationTitle,
    notificationOptions);
});

ধাপ ৪: ফ্রন্টএন্ডে Firebase এবং Push Notifications ইন্টিগ্রেট করা

index.html অথবা আপনার প্রধান HTML ফাইলে Firebase এবং সার্ভিস ওয়ার্কার লোড করুন:

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Framework7 PWA Push Notifications</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/framework7/framework7-bundle.min.css">
</head>
<body>
  <div id="app">
    <!-- Framework7 App Structure -->
  </div>
  <script src="https://cdn.jsdelivr.net/npm/framework7/framework7-bundle.min.js"></script>
  <script src="https://www.gstatic.com/firebasejs/9.6.10/firebase-app-compat.js"></script>
  <script src="https://www.gstatic.com/firebasejs/9.6.10/firebase-messaging-compat.js"></script>
  <script>
    // Firebase configuration
    const firebaseConfig = {
      apiKey: "YOUR_API_KEY",
      authDomain: "YOUR_PROJECT_ID.firebaseapp.com",
      projectId: "YOUR_PROJECT_ID",
      storageBucket: "YOUR_PROJECT_ID.appspot.com",
      messagingSenderId: "YOUR_SENDER_ID",
      appId: "YOUR_APP_ID",
    };

    // Initialize Firebase
    firebase.initializeApp(firebaseConfig);
    const messaging = firebase.messaging();

    // Request permission to send notifications
    Notification.requestPermission().then((permission) => {
      if (permission === 'granted') {
        console.log('Notification permission granted.');

        // Get registration token
        messaging.getToken({ vapidKey: 'YOUR_PUBLIC_VAPID_KEY' }).then((currentToken) => {
          if (currentToken) {
            console.log('FCM Token:', currentToken);
            // Send the token to your server and update the UI if necessary
          } else {
            console.log('No registration token available. Request permission to generate one.');
          }
        }).catch((err) => {
          console.log('An error occurred while retrieving token. ', err);
        });
      } else {
        console.log('Unable to get permission to notify.');
      }
    });

    // Handle incoming messages
    messaging.onMessage((payload) => {
      console.log('Message received. ', payload);
      // Customize notification here
      app.dialog.alert(payload.notification.body, payload.notification.title);
    });

    // Register service worker
    if ('serviceWorker' in navigator) {
      navigator.serviceWorker.register('/service-worker.js')
        .then(function(registration) {
          console.log('Service Worker registered with scope:', registration.scope);
        }).catch(function(err) {
          console.log('Service Worker registration failed:', err);
        });
    }

    // Initialize Framework7
    var app = new Framework7({
      root: '#app',
      name: 'My App',
      theme: 'auto',
      // Additional parameters
    });
  </script>
</body>
</html>

নোট:

  • YOUR_API_KEY, YOUR_PROJECT_ID, YOUR_SENDER_ID, YOUR_APP_ID, এবং YOUR_PUBLIC_VAPID_KEY আপনার Firebase প্রজেক্টের তথ্য অনুযায়ী প্রতিস্থাপন করুন।
  • VAPID কী তৈরির জন্য Firebase Console-এ Cloud Messaging সেকশনে যান এবং VAPID কী তৈরি করুন।

ধাপ ৫: Push Notifications পাঠানো

Push Notifications পাঠাতে Firebase Console-এ যান এবং Cloud Messaging সেকশনে একটি নতুন নোটিফিকেশন তৈরি করুন। আপনি এখানে টাইটেল, বডি, এবং অন্যান্য সেটিংস কনফিগার করতে পারবেন।

২. মোবাইল অ্যাপের জন্য Push Notifications সেটআপ

মোবাইল অ্যাপ (iOS ও Android) তৈরি করার জন্য Framework7-এর সাথে Cordova বা Capacitor ব্যবহার করতে পারেন। এখানে Cordova প্লাগইন ব্যবহার করে Push Notifications সেটআপ করার ধাপগুলো আলোচনা করা হলো।

ধাপ ১: Cordova ইনস্টল করা

প্রথমে নিশ্চিত করুন যে আপনার সিস্টেমে Cordova ইনস্টল করা আছে।

npm install -g cordova

ধাপ ২: নতুন Cordova প্ল্যাটফর্ম যোগ করা

আপনার Framework7 প্রজেক্টে Cordova প্ল্যাটফর্ম যোগ করুন:

cordova platform add android
cordova platform add ios

ধাপ ৩: Push Notifications প্লাগইন ইনস্টল করা

Cordova Push Notifications প্লাগইন ইনস্টল করুন:

cordova plugin add phonegap-plugin-push

ধাপ ৪: Firebase প্রজেক্ট সেটআপ করা

  1. Firebase Console এ যান এবং একটি নতুন প্রজেক্ট তৈরি করুন।
  2. আপনার অ্যাপ (Android এবং/অথবা iOS) Firebase প্রজেক্টে যোগ করুন।
  3. Cloud Messaging সেকশন থেকে প্রয়োজনীয় সেটিংস সংগ্রহ করুন, যেমন google-services.json (Android) এবং GoogleService-Info.plist (iOS)।

ধাপ ৫: ফ্রন্টএন্ডে Push Notifications ইন্টিগ্রেট করা

index.html অথবা আপনার প্রধান JavaScript ফাইলে Push Notifications কোড যোগ করুন:

document.addEventListener('deviceready', function () {
  var push = PushNotification.init({
    android: {
      senderID: "YOUR_SENDER_ID"
    },
    ios: {
      alert: "true",
      badge: "true",
      sound: "true"
    }
  });

  push.on('registration', function(data) {
    console.log("Registration ID:", data.registrationId);
    // Send registrationId to your server to send push notifications
  });

  push.on('notification', function(data) {
    console.log("Notification received:", data);
    app.dialog.alert(data.message, data.title);
    push.finish(function() {
      console.log('processing of push data is finished');
    });
  });

  push.on('error', function(e) {
    console.error("Push error:", e.message);
  });
}, false);

নোট:

  • YOUR_SENDER_ID আপনার Firebase প্রজেক্টের Sender ID যা FCM থেকে পাওয়া যায়।
  • Android এবং iOS এর জন্য আলাদা কনফিগারেশন ফাইল যোগ করতে ভুলবেন না (যেমন google-services.json এবং GoogleService-Info.plist)।

ধাপ ৬: Android ও iOS কনফিগারেশন

Android:

  1. Firebase Console থেকে google-services.json ডাউনলোড করুন।
  2. এটি আপনার Cordova প্রজেক্টের platforms/android/app/ ডিরেক্টরিতে রাখুন।

iOS:

  1. Firebase Console থেকে GoogleService-Info.plist ডাউনলোড করুন।
  2. এটি Xcode-এ আপনার প্রজেক্টে যোগ করুন।

ধাপ ৭: অ্যাপ বিল্ড এবং রান করা

Android এবং iOS এর জন্য অ্যাপ বিল্ড করুন:

cordova build android
cordova build ios

এবার আপনার মোবাইল ডিভাইসে অ্যাপটি ইনস্টল করুন এবং Push Notifications টেস্ট করুন।

উপসংহার

Framework7-এর সাথে Push Notifications সেটআপ এবং কনফিগার করা বেশ সহজ। আপনি PWA/Web অ্যাপের জন্য Firebase Cloud Messaging ব্যবহার করতে পারেন এবং মোবাইল অ্যাপের জন্য Cordova বা Capacitor প্লাগইন ব্যবহার করতে পারেন। Push Notifications আপনার অ্যাপের ইঙ্গেজমেন্ট এবং ব্যবহারকারীদের অভিজ্ঞতা উন্নত করতে গুরুত্বপূর্ণ ভূমিকা পালন করে।

সতর্কতা:

  • Push Notifications ব্যবহারের জন্য ব্যবহারকারীর অনুমতি প্রয়োজন।
  • নিরাপত্তা এবং ডেটা সুরক্ষা নিশ্চিত করতে Push Notifications-কে সঠিকভাবে হ্যান্ডল করুন।
  • Firebase Cloud Messaging এবং Cordova প্লাগইনের ডকুমেন্টেশন অনুসরণ করে আরও বিস্তারিত কনফিগারেশন ও ফিচার ইন্টিগ্রেশন করুন।

রেফারেন্স

Content added By

Firebase Cloud Messaging (FCM) এর মাধ্যমে Push Notifications

430

Firebase Cloud Messaging (FCM) হল গুগলের একটি ফ্রি সার্ভিস যা ডেভেলপারদের মোবাইল এবং ওয়েব অ্যাপ্লিকেশনে Push Notifications পাঠাতে সহায়তা করে। FCM ব্যবহার করে আপনি ব্যবহারকারীদের কাছে রিয়েল-টাইমে তথ্য, নোটিফিকেশন বা ডেটা পাঠাতে পারেন, যা ইউজার অভিজ্ঞতা উন্নত করে এবং অ্যাপের অ্যাক্টিভিটি বাড়ায়।

কেন Push Notifications ব্যবহার করবেন?

  • ইউজার এনগেজমেন্ট বাড়ানো: গুরুত্বপূর্ণ আপডেট বা তথ্য সরাসরি ব্যবহারকারীর ডিভাইসে পৌঁছাতে পারে।
  • রিয়েল-টাইম যোগাযোগ: ব্যবহারকারীদের সাথে তৎক্ষণাত যোগাযোগ স্থাপন করা যায়।
  • ব্যবহারকারী রিটেনশন: নিয়মিত নোটিফিকেশন ব্যবহারকারীদের অ্যাপে ফিরতে উৎসাহিত করে।
  • ইউজার পার্সোনালাইজেশন: ব্যক্তিগতকৃত নোটিফিকেশন পাঠিয়ে ইউজার অভিজ্ঞতা উন্নত করা যায়।

Framework7 এর সাথে FCM একীভূত করার ধাপসমূহ

১. Firebase প্রজেক্ট তৈরি করা

  1. Firebase Console এ যান: Firebase Console এ লগইন করুন।
  2. নতুন প্রজেক্ট তৈরি করুন:
    • Add project ক্লিক করুন।
    • আপনার প্রজেক্টের নাম দিন এবং Continue ক্লিক করুন।
    • প্রজেক্ট সেটআপ সম্পন্ন করুন।
  3. অ্যাপ যোগ করুন:
    • Add app ক্লিক করুন এবং আপনার অ্যাপের প্ল্যাটফর্ম নির্বাচন করুন (যেমন: Android, iOS, Web)।
    • প্রতিটি প্ল্যাটফর্মের জন্য প্রয়োজনীয় কনফিগারেশন ফাইল (যেমন: google-services.json for Android, GoogleService-Info.plist for iOS) ডাউনলোড করুন।

২. Framework7 প্রজেক্টে Firebase SDK যুক্ত করা

মোবাইল অ্যাপ (Cordova/Capacitor) এর জন্য:

  1. Cordova প্লাগইন ইনস্টল করা:

    cordova plugin add cordova-plugin-firebase
    

    অথবা Capacitor ব্যবহার করলে:

    npm install @capacitor/firebase
    npx cap sync
    
  2. কনফিগারেশন ফাইল যোগ করা:
    • ডাউনলোড করা google-services.json ফাইলটি platforms/android/app ডিরেক্টরিতে রাখুন।
    • iOS এর জন্য GoogleService-Info.plist ফাইলটি Xcode প্রজেক্টে যোগ করুন।

ওয়েব অ্যাপের জন্য:

  1. Firebase SDK যোগ করা: আপনার index.html ফাইলে Firebase SDK লিঙ্ক যুক্ত করুন:

    <!-- Firebase App (required) -->
    <script src="https://www.gstatic.com/firebasejs/9.22.1/firebase-app-compat.js"></script>
    <!-- Firebase Messaging -->
    <script src="https://www.gstatic.com/firebasejs/9.22.1/firebase-messaging-compat.js"></script>
    
  2. Firebase কনফিগারেশন যুক্ত করা:

    <script>
      // Your web app's Firebase configuration
      var firebaseConfig = {
        apiKey: "YOUR_API_KEY",
        authDomain: "YOUR_PROJECT_ID.firebaseapp.com",
        projectId: "YOUR_PROJECT_ID",
        storageBucket: "YOUR_PROJECT_ID.appspot.com",
        messagingSenderId: "YOUR_SENDER_ID",
        appId: "YOUR_APP_ID",
      };
      // Initialize Firebase
      firebase.initializeApp(firebaseConfig);
    </script>
    

৩. Push Notifications সেটআপ করা

মোবাইল অ্যাপের জন্য:

  1. টোকেন রেজিস্টার করা:

    document.addEventListener('deviceready', function () {
      const firebase = cordova.plugins.firebase;
    
      // Get FCM token
      firebase.getToken(function(token) {
        console.log("FCM Token:", token);
        // সার্ভারে টোকেন সংরক্ষণ করুন
      }, function(error) {
        console.error("Error getting token:", error);
      });
    
      // টোকেন রিফ্রেশ হ্যান্ডল করা
      firebase.onTokenRefresh(function(token) {
        console.log("Refreshed FCM Token:", token);
        // সার্ভারে নতুন টোকেন আপডেট করুন
      }, function(error) {
        console.error("Error on token refresh:", error);
      });
    
      // মেসেজ হ্যান্ডল করা
      firebase.onMessageReceived(function(message) {
        console.log("Received a notification:", message);
        // নোটিফিকেশন প্রদর্শন করুন
      }, function(error) {
        console.error("Error on message received:", error);
      });
    });
    

ওয়েব অ্যাপের জন্য:

  1. Service Worker তৈরি করা: আপনার প্রোজেক্টের মূল ডিরেক্টরিতে firebase-messaging-sw.js ফাইল তৈরি করুন:

    importScripts('https://www.gstatic.com/firebasejs/9.22.1/firebase-app-compat.js');
    importScripts('https://www.gstatic.com/firebasejs/9.22.1/firebase-messaging-compat.js');
    
    firebase.initializeApp({
      apiKey: "YOUR_API_KEY",
      authDomain: "YOUR_PROJECT_ID.firebaseapp.com",
      projectId: "YOUR_PROJECT_ID",
      storageBucket: "YOUR_PROJECT_ID.appspot.com",
      messagingSenderId: "YOUR_SENDER_ID",
      appId: "YOUR_APP_ID",
    });
    
    const messaging = firebase.messaging();
    
    messaging.onBackgroundMessage(function(payload) {
      console.log('[firebase-messaging-sw.js] Received background message ', payload);
      const notificationTitle = payload.notification.title;
      const notificationOptions = {
        body: payload.notification.body,
        icon: '/firebase-logo.png'
      };
    
      self.registration.showNotification(notificationTitle, notificationOptions);
    });
    
  2. Service Worker নিবন্ধন করা: আপনার index.html বা মূল JavaScript ফাইলে:

    if ('serviceWorker' in navigator) {
      navigator.serviceWorker.register('/firebase-messaging-sw.js')
      .then(function(registration) {
        console.log('Service Worker registered with scope:', registration.scope);
        messaging.useServiceWorker(registration);
      }).catch(function(err) {
        console.error('Service Worker registration failed:', err);
      });
    }
    
    const messaging = firebase.messaging();
    
    // ইউজার থেকে অনুমতি নিন
    messaging.requestPermission()
      .then(function() {
        console.log('Notification permission granted.');
        return messaging.getToken();
      })
      .then(function(token) {
        console.log('FCM Token:', token);
        // সার্ভারে টোকেন সংরক্ষণ করুন
      })
      .catch(function(err) {
        console.error('Unable to get permission to notify.', err);
      });
    
    // মেসেজ হ্যান্ডল করা
    messaging.onMessage(function(payload) {
      console.log('Message received. ', payload);
      // নোটিফিকেশন প্রদর্শন করুন
    });
    

৪. Push Notifications পাঠানো

Firebase Console ব্যবহার করে:

  1. Firebase Console এ যান এবং আপনার প্রজেক্ট নির্বাচন করুন।
  2. Cloud Messaging সেকশনে যান।
  3. Send your first message ক্লিক করুন।
  4. Message details পূরণ করুন (টাইটেল, মেসেজ ইত্যাদি)।
  5. Target নির্বাচন করুন (সকল ইউজার বা নির্দিষ্ট ডিভাইস)।
  6. Send message ক্লিক করুন।

সার্ভার সাইড থেকে Push Notifications পাঠানো:

আপনি সার্ভার থেকে HTTP POST রিকোয়েস্ট ব্যবহার করে Push Notifications পাঠাতে পারেন।

  1. Server Key সংগ্রহ করা:
    • Firebase Console এ Project Settings > Cloud Messaging সেকশনে যান।
    • Server key কপি করুন।
  2. HTTP POST রিকোয়েস্ট উদাহরণ (Node.js ব্যবহার করে):

    const fetch = require('node-fetch');
    
    const sendPushNotification = async (token, title, body) => {
      const message = {
        notification: {
          title: title,
          body: body,
        },
        to: token,
      };
    
      const response = await fetch('https://fcm.googleapis.com/fcm/send', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'Authorization': 'key=YOUR_SERVER_KEY',
        },
        body: JSON.stringify(message),
      });
    
      const data = await response.json();
      console.log('Push notification sent:', data);
    };
    
    // ব্যবহার:
    sendPushNotification('USER_FCM_TOKEN', 'Hello!', 'This is a push notification.');
    

৫. নিরাপত্তা এবং সঠিকতা

  • টোকেন সুরক্ষিত রাখা: FCM টোকেনগুলি সংরক্ষণ এবং ব্যবহার করার সময় সুরক্ষিত থাকতে হবে।
  • ডাইনামিক টোকেন আপডেট: ব্যবহারকারীর টোকেন পরিবর্তন হলে সার্ভারকে আপডেট করতে হবে।
  • নোটিফিকেশন সঠিকভাবে হ্যান্ডল করা: ক্লায়েন্ট সাইডে নোটিফিকেশন গ্রহণ এবং প্রদর্শনের সঠিক নিয়ম মেনে চলুন।

সারাংশ

Firebase Cloud Messaging (FCM) ব্যবহার করে Framework7 অ্যাপ্লিকেশনে Push Notifications ইন্টিগ্র

Full Example: Framework7 Web App with FCM Integration

index.html

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Framework7 FCM Example</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/framework7/framework7-bundle.min.css">
</head>
<body>
  <div id="app">
    <div class="view view-main">
      <div class="page">
        <div class="navbar">
          <div class="navbar-inner">
            <div class="title">FCM Example</div>
          </div>
        </div>
        <div class="page-content">
          <p>Welcome to Framework7 with FCM!</p>
        </div>
      </div>
    </div>
  </div>
  
  <script src="https://cdn.jsdelivr.net/npm/framework7/framework7-bundle.min.js"></script>
  <script src="https://www.gstatic.com/firebasejs/9.22.1/firebase-app-compat.js"></script>
  <script src="https://www.gstatic.com/firebasejs/9.22.1/firebase-messaging-compat.js"></script>
  <script>
    // Firebase configuration
    var firebaseConfig = {
      apiKey: "YOUR_API_KEY",
      authDomain: "YOUR_PROJECT_ID.firebaseapp.com",
      projectId: "YOUR_PROJECT_ID",
      storageBucket: "YOUR_PROJECT_ID.appspot.com",
      messagingSenderId: "YOUR_SENDER_ID",
      appId: "YOUR_APP_ID",
    };
    // Initialize Firebase
    firebase.initializeApp(firebaseConfig);
    const messaging = firebase.messaging();

    // Initialize Framework7
    var app = new Framework7({
      root: '#app',
      name: 'FCM Example',
      theme: 'auto',
      // other parameters
    });

    // Register Service Worker
    if ('serviceWorker' in navigator) {
      navigator.serviceWorker.register('/firebase-messaging-sw.js')
      .then(function(registration) {
        console.log('Service Worker registered with scope:', registration.scope);
        messaging.useServiceWorker(registration);
      }).catch(function(err) {
        console.error('Service Worker registration failed:', err);
      });
    }

    // Request Notification Permission and Get Token
    messaging.requestPermission()
      .then(function() {
        console.log('Notification permission granted.');
        return messaging.getToken();
      })
      .then(function(token) {
        console.log('FCM Token:', token);
        // Send token to your server and update the UI if necessary
      })
      .catch(function(err) {
        console.error('Unable to get permission to notify.', err);
      });

    // Handle incoming messages
    messaging.onMessage(function(payload) {
      console.log('Message received. ', payload);
      // Customize notification here
      app.dialog.alert(payload.notification.body, payload.notification.title);
    });
  </script>
</body>
</html>

firebase-messaging-sw.js

importScripts('https://www.gstatic.com/firebasejs/9.22.1/firebase-app-compat.js');
importScripts('https://www.gstatic.com/firebasejs/9.22.1/firebase-messaging-compat.js');

firebase.initializeApp({
  apiKey: "YOUR_API_KEY",
  authDomain: "YOUR_PROJECT_ID.firebaseapp.com",
  projectId: "YOUR_PROJECT_ID",
  storageBucket: "YOUR_PROJECT_ID.appspot.com",
  messagingSenderId: "YOUR_SENDER_ID",
  appId: "YOUR_APP_ID",
});

const messaging = firebase.messaging();

messaging.onBackgroundMessage(function(payload) {
  console.log('[firebase-messaging-sw.js] Received background message ', payload);
  const notificationTitle = payload.notification.title;
  const notificationOptions = {
    body: payload.notification.body,
    icon: '/firebase-logo.png'
  };

  self.registration.showNotification(notificationTitle, notificationOptions);
});

Firebase Cloud Messaging (FCM) ব্যবহার করে Framework7 অ্যাপ্লিকেশনে Push Notifications ইন্টিগ্রেট করা বেশ সহজ। এটি আপনাকে ব্যবহারকারীদের সাথে কার্যকর যোগাযোগ স্থাপন করতে এবং অ্যাপের ইউজার অভিজ্ঞতা উন্নত করতে সহায়তা করে। উপরোক্ত ধাপগুলো অনুসরণ করে আপনি আপনার Framework7 অ্যাপে FCM সফলভাবে ইন্টিগ্রেট করতে পারবেন।

Content added By

Notifications এর জন্য Permissions এবং User Preferences ব্যবস্থাপনা

248

Framework7 একটি শক্তিশালী ফ্রন্ট-এন্ড ফ্রেমওয়ার্ক যা মোবাইল এবং ওয়েব অ্যাপ্লিকেশন তৈরি করার জন্য ব্যবহৃত হয়। Notifications এর মাধ্যমে অ্যাপ ব্যবহারকারীদের সাথে ইন্টারঅ্যাকশন বাড়ানো যায়, যেমন নতুন আপডেট, বার্তা, বা অন্যান্য গুরুত্বপূর্ণ তথ্য জানানো। তবে, Notifications ব্যবহারের আগে ব্যবহারকারীর অনুমতি (Permissions) নেওয়া এবং তাদের পছন্দসমূহ (User Preferences) সঠিকভাবে পরিচালনা করা অত্যন্ত গুরুত্বপূর্ণ। নিচে Framework7 এ Notifications Permissions এবং User Preferences ব্যবস্থাপনার বিস্তারিত ব্যাখ্যা দেওয়া হলো।


১. Notifications Permissions

কেন Permissions প্রয়োজন?

ব্রাউজার এবং মোবাইল অপারেটিং সিস্টেমগুলি ব্যবহারকারীর গোপনীয়তা রক্ষা করতে Notifications পাঠানোর আগে অনুমতি চায়। এটি ব্যবহারকারীর নিয়ন্ত্রণে Notifications ব্যবহারের অনুমতি দেয় এবং অনিচ্ছাকৃত Notifications থেকে রক্ষা করে।

Permissions কিভাবে চাওয়া যায়?

JavaScript এর Notification API ব্যবহার করে Notifications এর জন্য অনুমতি চাওয়া যায়। Framework7 এর সাথে এটি সহজেই ইন্টিগ্রেট করা যায়। নিচে একটি উদাহরণ দেওয়া হলো:

উদাহরণ: Notifications Permissions চাওয়া
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Framework7 Notifications Example</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/framework7/framework7-bundle.min.css">
</head>
<body>
  <div id="app">
    <div class="view view-main">
      <div class="page">
        <div class="navbar">
          <div class="navbar-inner">
            <div class="title">Notifications Example</div>
          </div>
        </div>
        <div class="page-content">
          <div class="block">
            <button id="enable-notifications" class="button button-fill">Enable Notifications</button>
            <button id="send-notification" class="button button-fill">Send Notification</button>
          </div>
        </div>
      </div>
    </div>
  </div>
  
  <script src="https://cdn.jsdelivr.net/npm/framework7/framework7-bundle.min.js"></script>
  <script>
    var app = new Framework7({
      root: '#app',
    });

    // চেক করুন ব্রাউজার Notifications সাপোর্ট করে কিনা
    if (!("Notification" in window)) {
      app.dialog.alert("This browser does not support desktop notification");
    }

    // Enable Notifications বাটন ক্লিক ইভেন্ট
    document.getElementById('enable-notifications').addEventListener('click', function () {
      Notification.requestPermission().then(function (permission) {
        if (permission === "granted") {
          app.dialog.alert("Notifications enabled!");
        } else if (permission === "denied") {
          app.dialog.alert("Notifications denied.");
        }
      });
    });

    // Send Notification বাটন ক্লিক ইভেন্ট
    document.getElementById('send-notification').addEventListener('click', function () {
      if (Notification.permission === "granted") {
        new Notification("Hello from Framework7!", {
          body: "This is a test notification.",
          icon: "https://framework7.io/img/icons/ios-icon.png"
        });
      } else {
        app.dialog.alert("Please enable notifications first.");
      }
    });
  </script>
</body>
</html>
ব্যাখ্যা:
  1. Permissions চেক: প্রথমে যাচাই করা হয় যে ব্রাউজার Notifications সাপোর্ট করে কিনা।
  2. Permissions চাওয়া: ব্যবহারকারী Enable Notifications বাটন ক্লিক করলে Notification.requestPermission() কল করা হয়।
  3. Notifications পাঠানো: যদি অনুমতি পাওয়া যায়, Send Notification বাটন ক্লিক করলে একটি নতুন Notification তৈরি হয়।

২. Push Notifications (PWA এবং মোবাইল অ্যাপের জন্য)

PWA (Progressive Web App) Notifications

PWA এ Push Notifications পাঠানোর জন্য Service Workers ব্যবহার করা হয়। এটি Framework7 এর সাথে ইন্টিগ্রেট করা যায়।

উদাহরণ: PWA Push Notifications সেটআপ
  1. Service Worker রেজিস্টার করা:
if ('serviceWorker' in navigator) {
  navigator.serviceWorker.register('/service-worker.js')
  .then(function(registration) {
    console.log('Service Worker Registered with scope:', registration.scope);
  }).catch(function(error) {
    console.log('Service Worker registration failed:', error);
  });
}
  1. Service Worker ফাইল (service-worker.js):
self.addEventListener('push', function(event) {
  const data = event.data.json();
  
  const options = {
    body: data.body,
    icon: data.icon,
    badge: data.badge
  };
  
  event.waitUntil(
    self.registration.showNotification(data.title, options)
  );
});
  1. Push Subscription ও Notification পাঠানো:

Push সার্ভার সেটআপ করতে হবে (যেমন Firebase Cloud Messaging) এবং ব্যবহারকারীর Push Subscription সংগ্রহ করতে হবে।


মোবাইল অ্যাপে Push Notifications

Framework7 মোবাইল অ্যাপ তৈরি করতে Cordova বা Capacitor ব্যবহার করে Push Notifications ইন্টিগ্রেট করা যায়।

উদাহরণ: Cordova Push Notifications
  1. Cordova প্লাগইন ইনস্টল করা:
cordova plugin add phonegap-plugin-push
  1. Push Notifications সেটআপ:
var push = PushNotification.init({
    android: {
        senderID: "YOUR_SENDER_ID"
    },
    ios: {
        alert: "true",
        badge: "true",
        sound: "true"
    },
    windows: {}
});

push.on('registration', function(data) {
    console.log("Device registered with ID:", data.registrationId);
});

push.on('notification', function(data) {
    console.log("Notification received:", data);
    // Handle the notification here
});

push.on('error', function(e) {
    console.log("Push notification error:", e.message);
});

৩. User Preferences ব্যবস্থাপনা

ব্যবহারকারীর পছন্দ অনুযায়ী Notifications চালু বা বন্ধ রাখা অত্যন্ত গুরুত্বপূর্ণ। এটি ব্যবহারকারীর অভিজ্ঞতা উন্নত করে এবং তাদের নিয়ন্ত্রণে Notifications পাঠানোর সুযোগ দেয়।

Preferences UI তৈরি করা

Framework7 এর ফর্ম কম্পোনেন্ট ব্যবহার করে একটি Preferences পৃষ্ঠা তৈরি করা যেতে পারে যেখানে ব্যবহারকারী Notifications চালু বা বন্ধ করতে পারবেন।

উদাহরণ: Preferences পৃষ্ঠা
<div class="page">
  <div class="page-content">
    <div class="list">
      <ul>
        <li>
          <div class="item-content item-input">
            <div class="item-inner">
              <div class="item-title item-label">Enable Notifications</div>
              <div class="item-input-wrap">
                <label class="toggle toggle-init">
                  <input type="checkbox" id="notification-toggle">
                  <span class="toggle-icon"></span>
                </label>
              </div>
            </div>
          </div>
        </li>
      </ul>
    </div>
  </div>
</div>
JavaScript দিয়ে Preferences হ্যান্ডল করা
var app = new Framework7({
  root: '#app',
});

// টগল পরিবর্তন হ্যান্ডল করা
document.getElementById('notification-toggle').addEventListener('change', function(e) {
  if (e.target.checked) {
    Notification.requestPermission().then(function(permission) {
      if (permission === "granted") {
        localStorage.setItem('notificationsEnabled', 'true');
        app.dialog.alert("Notifications enabled!");
      } else {
        e.target.checked = false;
        app.dialog.alert("Notifications permission denied.");
      }
    });
  } else {
    localStorage.setItem('notificationsEnabled', 'false');
    app.dialog.alert("Notifications disabled.");
  }
});

// পৃষ্ঠা লোড হলে টগল স্টেট সেট করা
var notificationsEnabled = localStorage.getItem('notificationsEnabled');
if (notificationsEnabled === 'true') {
  document.getElementById('notification-toggle').checked = true;
} else {
  document.getElementById('notification-toggle').checked = false;
}
ব্যাখ্যা:
  1. টগল হ্যান্ডলার: ব্যবহারকারী টগল করলে Notification.requestPermission() কল করা হয়।
  2. Permissions হ্যান্ডল করা: যদি অনুমতি দেয়া হয়, notificationsEnabled স্টোর করা হয়, অন্যথায় টগল বন্ধ করা হয়।
  3. স্টেট সেট করা: পৃষ্ঠা লোড হলে localStorage থেকে স্টেট রিড করে টগল স্টেট সেট করা হয়।

৪. In-App Notifications

Framework7 এ ইন-অ্যাপ Notifications তৈরি এবং প্রদর্শন করা যায় Framework7 এর ডায়ালগ কম্পোনেন্ট ব্যবহার করে।

উদাহরণ: ইন-অ্যাপ Notification
// ইন-অ্যাপ Notification প্রদর্শন করা
app.dialog.alert('This is an in-app notification!', 'Notification');
উদাহরণ: কাস্টম ইন-অ্যাপ Notification
app.dialog.create({
  title: 'Custom Notification',
  text: 'You have a new message!',
  buttons: [
    {
      text: 'OK',
      onClick: function () {
        console.log('Notification closed');
      }
    }
  ]
}).open();

৫. Best Practices

  • ব্যবহারকারীর সম্মতি: Notifications পাঠানোর আগে অবশ্যই ব্যবহারকারীর অনুমতি নিন।
  • পছন্দসমূহ সম্মান: ব্যবহারকারী যদি Notifications বন্ধ করে দেন, তবে সেগুলো পুনরায় অনুরোধ করবেন না।
  • সুস্পষ্ট বার্তা: Notifications এর বার্তা সংক্ষেপে এবং স্পষ্টভাবে লিখুন যাতে ব্যবহারকারী সহজে বুঝতে পারেন।
  • সময়সীমা: অত্যধিক Notifications পাঠাবেন না যা ব্যবহারকারীকে বিরক্ত করতে পারে।
  • গোপনীয়তা: ব্যবহারকারীর তথ্য সুরক্ষিত রাখুন এবং গোপনীয়তা রক্ষা করুন।

সারাংশ

Framework7 এ Notifications Permissions এবং User Preferences ব্যবস্থাপনা অত্যন্ত গুরুত্বপূর্ণ এবং সহজ। এটি ব্যবহার করে আপনি ব্যবহারকারীদের সাথে কার্যকরীভাবে ইন্টারঅ্যাকশন করতে পারেন এবং তাদের অভিজ্ঞতা উন্নত করতে পারেন। নিচের ধাপগুলো অনুসরণ করে আপনি সহজেই Notifications সেটআপ এবং পরিচালনা করতে পারেন:

  1. Permissions চাওয়া: Notification.requestPermission() ব্যবহার করে।
  2. Notifications পাঠানো: new Notification() বা Framework7 এর ডায়ালগ কম্পোনেন্ট ব্যবহার করে।
  3. User Preferences পরিচালনা: ফর্ম কম্পোনেন্ট এবং localStorage ব্যবহার করে।
  4. Push Notifications: PWA বা মোবাইল অ্যাপের জন্য Service Workers বা Cordova প্লাগইন ব্যবহার করে।
  5. Best Practices মেনে চলা: ব্যবহারকারীর সম্মতি এবং পছন্দসমূহ সম্মান করা।

Framework7 এর মাধ্যমে Notifications ব্যবস্থাপনা করলে আপনার অ্যাপ ব্যবহারকারীদের সাথে আরও কার্যকরীভাবে যোগাযোগ করতে সক্ষম হবে এবং তাদের অভিজ্ঞতা উন্নত হবে।

Content added By
Promotion

Are you sure to start over?

Loading...