অ্যাপল প্ল্যাটফর্ম অ্যাপগুলিতে বার্তা গ্রহণ করুন

প্ল্যাটফর্ম নির্বাচন করুন: iOS+ অ্যান্ড্রয়েড ওয়েব ফ্লাটার ইউনিটি C++


আপনার ক্লায়েন্ট অ্যাপটি কোনো ডিভাইসে ইনস্টল হয়ে গেলে, সেটি FCM APNs ইন্টারফেসের মাধ্যমে বার্তা গ্রহণ করতে পারে। আপনি নোটিফিকেশন কম্পোজার ব্যবহার করে অথবা আপনার অ্যাপ্লিকেশন সার্ভারে তৈরি করা বার্তাগুলির মাধ্যমে অবিলম্বে ব্যবহারকারী সেগমেন্টগুলিতে নোটিফিকেশন পাঠানো শুরু করতে পারেন।

সতর্কতা বিজ্ঞপ্তিগুলি পরিচালনা করুন

FCM APNs-এর মাধ্যমে Apple অ্যাপগুলোকে লক্ষ্য করে পাঠানো সমস্ত বার্তা সরবরাহ করে। UNUserNotificationCenter ব্যবহার করে APNs নোটিফিকেশন গ্রহণ করার বিষয়ে আরও জানতে, Apple-এর 'Handling Notifications' এবং 'Notification-Related Actions' সংক্রান্ত ডকুমেন্টেশন দেখুন।

FCM থেকে ডিসপ্লে নোটিফিকেশন পেতে হলে আপনাকে অবশ্যই UNUserNotificationCenter ডেলিগেট সেট করতে হবে এবং উপযুক্ত ডেলিগেট মেথডগুলো ইমপ্লিমেন্ট করতে হবে।

সুইফট

// Receive displayed notifications for iOS 10+ devices.
func userNotificationCenter(_ center: UNUserNotificationCenter,
                            willPresent notification: UNNotification) async
  -> UNNotificationPresentationOptions {
  let userInfo = notification.request.content.userInfo

  // With swizzling disabled you must let Messaging know about the message, for Analytics
  // Messaging.messaging().appDidReceiveMessage(userInfo)

  // ...

  // Print full message.
  print(userInfo)

  // Change this to your preferred presentation option
  // Note: UNNotificationPresentationOptions.alert has been deprecated.
  return [.list, .banner, .sound]
}

func userNotificationCenter(_ center: UNUserNotificationCenter,
                            didReceive response: UNNotificationResponse) async {
  let userInfo = response.notification.request.content.userInfo

  // ...

  // Print full message.
  print(userInfo)
}

উদ্দেশ্য-সি

// Receive displayed notifications for iOS 10+ devices.
// Handle incoming notification messages while app is in the foreground.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
       willPresentNotification:(UNNotification *)notification
         withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
  NSDictionary *userInfo = notification.request.content.userInfo;

  // With swizzling disabled you must let Messaging know about the message, for Analytics
  // [[FIRMessaging messaging] appDidReceiveMessage:userInfo];

  // Print full message.
  NSLog(@"%@", userInfo);

  // Change this to your preferred presentation option
  completionHandler(UNNotificationPresentationOptionList |
                    UNNotificationPresentationOptionBanner |
                    UNNotificationPresentationOptionSound);
}

- (void)messaging:(FIRMessaging *)messaging didReceiveRegistrationToken:(NSString *)fcmToken {
    NSLog(@"FCM registration token: %@", fcmToken);
    // Notify about received token.
    NSDictionary *dataDict = [NSDictionary dictionaryWithObject:fcmToken forKey:@"token"];
    [[NSNotificationCenter defaultCenter] postNotificationName:
     @"FCMToken" object:nil userInfo:dataDict];
    // TODO: If necessary send token to application server.
    // Note: This callback is fired at each app startup and whenever a new token is generated.
}

- (void)logFCMToken {
  NSString *fcmToken = [FIRMessaging messaging].FCMToken;
  NSLog(@"Local FCM registration token: %@", fcmToken);

  NSString* displayToken = [NSString stringWithFormat:@"Logged FCM token: %@", fcmToken];

  [[FIRMessaging messaging] tokenWithCompletion:^(NSString * _Nullable token, NSError * _Nullable error) {
    if (error != nil) {
      NSLog(@"Error fetching the remote FCM registration token: %@", error);
    } else {
      NSLog(@"Remote FCM registration token: %@", token);
      NSString* message =
        [NSString stringWithFormat:@"FCM registration token: %@", token];
      // display message
      NSLog(@"%@", message);
    }
  }];
  NSLog(@"%@", displayToken);
}

- (void)subsribeToTopic {
  [[FIRMessaging messaging] subscribeToTopic:@"weather"
                                  completion:^(NSError * _Nullable error) {
    NSLog(@"Subscribed to weather topic");
  }];
}

@end

আপনি যদি আপনার নোটিফিকেশনে কাস্টম অ্যাকশন যোগ করতে চান, তাহলে নোটিফিকেশন পেলোডে click_action প্যারামিটারটি সেট করুন। APNs পেলোডে category কী-এর জন্য আপনি যে মানটি ব্যবহার করতেন, সেটিই ব্যবহার করুন। কাস্টম অ্যাকশন ব্যবহার করার আগে অবশ্যই রেজিস্টার করতে হবে। আরও তথ্যের জন্য, Apple-এর `Local and Remote Notification Programming Guide` দেখুন।

আপনার অ্যাপে মেসেজ ডেলিভারি সম্পর্কে বিস্তারিত জানতে, FCM রিপোর্টিং ড্যাশবোর্ডটি দেখুন, যেখানে অ্যাপল এবং অ্যান্ড্রয়েড ডিভাইসে পাঠানো ও খোলা মেসেজের সংখ্যার পাশাপাশি অ্যান্ড্রয়েড অ্যাপের 'ইম্প্রেশন' (ব্যবহারকারীদের দেখা নোটিফিকেশন) ডেটাও রেকর্ড করা থাকে।

নীরব পুশ নোটিফিকেশন পরিচালনা করুন

content-available কী (যা APNs-এর content-available সমতুল্য) ব্যবহার করে মেসেজ পাঠানোর সময়, বার্তাগুলি সাইলেন্ট নোটিফিকেশন হিসাবে ডেলিভার হবে, যা ব্যাকগ্রাউন্ডে ডেটা রিফ্রেশের মতো কাজের জন্য আপনার অ্যাপকে জাগিয়ে তুলবে। ফোরগ্রাউন্ড নোটিফিকেশনের মতো নয়, এই নোটিফিকেশনগুলি অবশ্যই application(_:didReceiveRemoteNotification:fetchCompletionHandler:) মেথড ব্যবহার করে হ্যান্ডেল করতে হবে।

দেখানো অনুযায়ী application(_:didReceiveRemoteNotification:fetchCompletionHandler:) প্রয়োগ করুন:

সুইফট

@MainActor
func application(_ application: UIApplication,
                 didReceiveRemoteNotification userInfo: [AnyHashable: Any]) async
  -> UIBackgroundFetchResult {
  // If you are receiving a notification message while your app is in the background,
  // this callback will not be fired till the user taps on the notification launching the application.
  // TODO: Handle data of notification

  // With swizzling disabled you must let Messaging know about the message, for Analytics
  // Messaging.messaging().appDidReceiveMessage(userInfo)

  // ...

  // Print full message.
  print(userInfo)
  print("Call exportDeliveryMetricsToBigQuery() from AppDelegate")
  Messaging.serviceExtension().exportDeliveryMetricsToBigQuery(withMessageInfo: userInfo)
  return UIBackgroundFetchResult.newData
}

উদ্দেশ্য-সি

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
    fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  // If you are receiving a notification message while your app is in the background,
  // this callback will not be fired until the user taps on the notification launching the application.
  // TODO: Handle data of notification

  // With swizzling disabled you must let Messaging know about the message, for Analytics
  // [[FIRMessaging messaging] appDidReceiveMessage:userInfo];

  // Print full message.
  NSLog(@"%@", userInfo);

  completionHandler(UIBackgroundFetchResultNewData);
}

অ্যাপল প্ল্যাটফর্মগুলো ব্যাকগ্রাউন্ড নোটিফিকেশন ডেলিভারির নিশ্চয়তা দেয় না। যেসব কারণে ব্যাকগ্রাউন্ড নোটিফিকেশন ব্যর্থ হতে পারে, সে সম্পর্কে জানতে অ্যাপলের ‘ আপনার অ্যাপে ব্যাকগ্রাউন্ড আপডেট পুশ করা’ বিষয়ক ডকুমেন্টেশন দেখুন।

নোটিফিকেশন মেসেজ পেলোড ব্যাখ্যা করুন

নোটিফিকেশন মেসেজের পেলোড হলো কী এবং ভ্যালু সমন্বিত একটি ডিকশনারি। APNs-এর মাধ্যমে পাঠানো নোটিফিকেশন মেসেজগুলোর পেলোড ফরম্যাটটি নিম্নরূপ:

  {
    "aps" : {
      "alert" : {
        "body" : "great match!",
        "title" : "Portugal vs. Denmark",
      },
      "badge" : 1,
    },
    "customKey" : "customValue"
  }

মেথড সুইজলিং নিষ্ক্রিয় থাকা অবস্থায় মেসেজগুলি পরিচালনা করুন

ডিফল্টরূপে, আপনি যদি আপনার অ্যাপের অ্যাপ ডেলিগেট ক্লাসকে UNUserNotificationCenter এবং Messaging ডেলিগেট প্রপার্টিতে অ্যাসাইন করেন, তাহলে FCM আপনার অ্যাপ ডেলিগেট ক্লাসকে সুইজল করে স্বয়ংক্রিয়ভাবে আপনার FCM টোকেনকে ডিভাইসের APNs টোকেনের সাথে যুক্ত করবে এবং নোটিফিকেশন-প্রাপ্ত ইভেন্টগুলোকে Analytics পাঠাবে। আপনি যদি স্পষ্টভাবে মেথড সুইজলিং নিষ্ক্রিয় করেন, যদি আপনি একটি SwiftUI অ্যাপ তৈরি করেন, অথবা যদি আপনি যেকোনো একটি ডেলিগেটের জন্য আলাদা ক্লাস ব্যবহার করেন, তাহলে আপনাকে এই দুটি কাজই ম্যানুয়ালি করতে হবে।

FCM টোকেনটিকে ডিভাইস APNs টোকেনের সাথে যুক্ত করতে, আপনার অ্যাপ ডেলিগেটের টোকেন রিফ্রেশ হ্যান্ডলারে apnsToken প্রপার্টি ব্যবহার করে APNs টোকেনটি Messaging ক্লাসে পাস করুন।

সুইফট

func application(_ application: UIApplication,
    didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  Messaging.messaging().apnsToken = deviceToken;
}

উদ্দেশ্য-সি

- (void)application:(UIApplication *)application
    didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  [FIRMessaging messaging].APNSToken = deviceToken;
}

Analytics এ নোটিফিকেশন প্রাপ্তির তথ্য পাঠাতে appDidReceiveMessage(_:) মেথডটি ব্যবহার করুন।

সুইফট

func userNotificationCenter(_ center: UNUserNotificationCenter,
                            willPresent notification: UNNotification,
  withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
  let userInfo = notification.request.content.userInfo

  Messaging.messaging().appDidReceiveMessage(userInfo)

  // Change this to your preferred presentation option
  completionHandler([[.alert, .sound]])
}

func userNotificationCenter(_ center: UNUserNotificationCenter,
                            didReceive response: UNNotificationResponse,
                            withCompletionHandler completionHandler: @escaping () -> Void) {
  let userInfo = response.notification.request.content.userInfo

  Messaging.messaging().appDidReceiveMessage(userInfo)

  completionHandler()
}

func application(_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable : Any],
  fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
  Messaging.messaging().appDidReceiveMessage(userInfo)
  completionHandler(.noData)
}

উদ্দেশ্য-সি

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
      willPresentNotification:(UNNotification *)notification
        withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
  NSDictionary *userInfo = notification.request.content.userInfo;

  [[FIRMessaging messaging] appDidReceiveMessage:userInfo];

  // Change this to your preferred presentation option
  completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionAlert);
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
        withCompletionHandler:(void(^)(void))completionHandler {
  NSDictionary *userInfo = response.notification.request.content.userInfo;

  [[FIRMessaging messaging] appDidReceiveMessage:userInfo];

  completionHandler();
}

- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler {
  [[FIRMessaging messaging] appDidReceiveMessage:userInfo];
  completionHandler(UIBackgroundFetchResultNoData);
}